我的程序里面是用到了安卓的listview來顯示數(shù)據(jù),主要用SimpleAdapter適配器,然而怎么獲取listview的item中的控件呢?就如說我這里做了一個(gè)顯示地址管理。
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括長(zhǎng)清網(wǎng)站建設(shè)、長(zhǎng)清網(wǎng)站制作、長(zhǎng)清網(wǎng)頁(yè)制作以及長(zhǎng)清網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,長(zhǎng)清網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到長(zhǎng)清省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
而讓我苦惱的是要怎么獲取里面的刪除控件呢?
一開始我想直接取出來然后給它設(shè)置監(jiān)聽器,運(yùn)行奔潰了,后來想了想,也是,你的listview每個(gè)item的布局都是一樣的,我怎么知道你是點(diǎn)擊哪個(gè)的。
所這個(gè)還是要在listview被點(diǎn)擊時(shí)調(diào)用addressListView.setOnItemClickListener(),應(yīng)該在這方法中去給控件添加監(jiān)聽器。
這里非常有用,不就如說當(dāng)你叫外賣或者網(wǎng)購(gòu)的時(shí)候,里面有一個(gè)+、-控件。獲得控件的原理跟這個(gè)是一樣的。
接下來我跟大家分享一下:
item樣式文件:address_item.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textView0"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:background="@drawable/company" />
<TextView
android:id="@+id/CompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView0"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:text="沃爾瑪" />
<TextView
android:id="@+id/delAddress"
android:layout_width="25dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:background="@drawable/address_delete" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:background="@drawable/contact_person"
/>
<TextView
android:id="@+id/ContactPersonName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView1"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:text="徐董" />
<TextView
android:id="@+id/textView2"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ContactPersonName"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:background="@drawable/phone"/>
<TextView
android:id="@+id/PhoneNumer"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView2"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:text="15766228209" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" >
<TextView
android:id="@+id/textView3"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:background="@drawable/address"
/>
<TextView
android:id="@+id/AddressDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView3"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:text="廣東省湛江市國(guó)際大廈" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/p_w_picpathView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dv" />
</LinearLayout>
</LinearLayout>
listview布局文件:addressmanage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg">
<RelativeLayout
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/bg_navigation" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="5dip"
android:gravity="center"
android:text="地址管理"
android:textSize="20dip" />
<LinearLayout
android:id="@+id/AddressBack"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true" >
<TextView
android:layout_width="20dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/back"
/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/linearlayoutAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:id="@+id/addressListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:listSelector="#00000000"
>
</ListView>
</LinearLayout>
<Button
android:id="@+id/AddNewAddress"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_marginBottom="10dp"
android:text="新增"
android:background="@drawable/addressbuttonbg"/>
</LinearLayout>
上面兩個(gè)就是xml布局文件
接下來就是.java文件的部分主要代碼:
addressListView = (ListView)findViewById(R.id.addressListView);
List<Map<String, Object>> listitems = new ArrayList<Map<String,Object>>();
for (int i = 0; i < companys.length; i++) {
Map<String, Object> listitem = new HashMap<String, Object>();
listitem.put("company", companys[i]);
listitem.put("person",persons[i]);
listitem.put("phone", phones[i]);
listitem.put("address", address[i]);
listitems.add(listitem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(AdrressManageActivity.this, listitems, R.layout.address_item, new String[]{"company","person","phone","address"},new int[]{R.id.CompanyName,R.id.ContactPersonName,R.id.PhoneNumer,R.id.AddressDetail});
addressListView.setAdapter(simpleAdapter);
//設(shè)置焦點(diǎn)響應(yīng)問題 同時(shí)要將 item 中的焦點(diǎn) focusable 設(shè)置為 false
addressListView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
//設(shè)置listview中的哪個(gè)item被點(diǎn)擊,然后獲得刪除按鈕
addressListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView delButton = (TextView)view.findViewById(R.id.delAddress);
delButton.setOnClickListener(myListener);
}
});
上面加背景色的為最主要代碼。
1、item的布局文件最好不要出現(xiàn)button和check這樣的控件
2、要是用到點(diǎn)擊事件,可以用TextView來代替。原因如下:
你的item中的布局文件最好不要用button或者check控件,因?yàn)槟阌昧薭utton或者check控件的話,item的focus就會(huì)失焦,因?yàn)閎utton或者check控件優(yōu)先了。所以你會(huì)點(diǎn)擊listview的item沒有反應(yīng)。
3、清除item按下的背景顏色。
在listview的xml中加入: android:listSelector="#00000000"
4、獲得item的點(diǎn)擊事件
addressListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int arg2, long arg3) {
// TODO Auto-generated method stub
delButton = (TextView)view.findViewById(R.id.delAddress);
delButton.setOnClickListener(myListener);
}
});
其中view代表這個(gè)item的布局。所以通過方法view.findViewById(xxx);來獲得你要的控件。
5、為你的控件設(shè)置監(jiān)聽器
如上:delButton.setOnClickListener(myListener);
6、你要做出什么響應(yīng)就自己寫啦。今天憂郁了很久,終于解決了。
網(wǎng)站題目:關(guān)于安卓listview獲得item中的控件問題
網(wǎng)頁(yè)URL:http://aaarwkj.com/article26/phdsjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作、用戶體驗(yàn)、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)