關(guān)于安卓listview獲得item中的控件問題-創(chuàng)新互聯(lián)

我的程序里面是用到了安卓的listview來顯示數(shù)據(jù),主要用SimpleAdapter適配器,然而怎么獲取listview的item中的控件呢?就如說我這里做了一個顯示地址管理。關(guān)于安卓listview獲得item中的控件問題

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),福建企業(yè)網(wǎng)站建設(shè),福建品牌網(wǎng)站建設(shè),網(wǎng)站定制,福建網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,福建網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

而讓我苦惱的是要怎么獲取里面的刪除控件呢?

一開始我想直接取出來然后給它設(shè)置監(jiān)聽器,運行奔潰了,后來想了想,也是,你的listview每個item的布局都是一樣的,我怎么知道你是點擊哪個的。

所這個還是要在listview被點擊時調(diào)用addressListView.setOnItemClickListener(),應(yīng)該在這方法中去給控件添加監(jiān)聽器。

這里非常有用,不就如說當(dāng)你叫外賣或者網(wǎng)購的時候,里面有一個+、-控件。獲得控件的原理跟這個是一樣的。

接下來我跟大家分享一下:

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="廣東省湛江市國際大廈" />

  </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>

上面兩個就是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è)置焦點響應(yīng)問題   同時要將 item 中的焦點 focusable 設(shè)置為 false

    addressListView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

    //設(shè)置listview中的哪個item被點擊,然后獲得刪除按鈕

    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、要是用到點擊事件,可以用TextView來代替。原因如下:

你的item中的布局文件最好不要用button或者check控件,因為你用了button或者check控件的話,item的focus就會失焦,因為button或者check控件優(yōu)先了。所以你會點擊listview的item沒有反應(yīng)。

3、清除item按下的背景顏色。

在listview的xml中加入: android:listSelector="#00000000"

4、獲得item的點擊事件

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代表這個item的布局。所以通過方法view.findViewById(xxx);來獲得你要的控件。

5、為你的控件設(shè)置監(jiān)聽器

如上:delButton.setOnClickListener(myListener);

6、你要做出什么響應(yīng)就自己寫啦。今天憂郁了很久,終于解決了。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文名稱:關(guān)于安卓listview獲得item中的控件問題-創(chuàng)新互聯(lián)
文章路徑:http://muchs.cn/article8/dchsop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、定制開發(fā)、網(wǎng)站營銷、標(biāo)簽優(yōu)化網(wǎng)站內(nèi)鏈、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司