在ListView的列表项中为Button分配onClick事件

时间:2014-02-26 10:10:00

标签: android listview android-listview

我创建了一个从数据库中填充的ListView。每行都有一个按钮,用于更新列表和数据库中的项值。我想为{{{item}项中使用的按钮添加onClick事件1}}。如何为列表项的按钮分配OnClickListener?

enter image description here

addtooutlet.xml

ListView

addtooutlet_list_item.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#ffffff">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

AddTooutlet.java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dip" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="46dp"
    android:background="#ffffff"
    android:padding="5dip"
    android:weightSum="100" >

    <TextView
        android:id="@+id/item_bname"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="25"
        android:padding="2dp"
        android:text="Hello"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="12dip" />

    <TextView
        android:id="@+id/item_bid"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="25"
        android:padding="2dp"
        android:text="Hello"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="12dip" />

    <Button
        android:id="@+id/item_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/item_bname"
        android:layout_marginRight="16dp"
        android:layout_weight="50"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:padding="2dp"
        android:text="Join"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
          android:textSize="12dip" />
   </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:6)

你有

 View row = inflater.inflate(R.layout.addtooutlet_list_item, parent,
            false);

但你回来了

return convertView;

应该是

View convertView = inflater.inflate(R.layout.addtooutlet_list_item, parent,
            false);

 Button buttonEdit = (Button) convertView.findViewById(R.id.item_button);

return convertView;

编辑:

正如blackbelt的评论所示。您没有使用Custom ArrayAdapter

你可能意味着

Myadapter adapter = new MyAdapter(ActivityName.this,R.layout.addtooutlet_list_item,mylist);
setListAdapter(adapter);

然后

 ArrayList<HashMap<String,String> map;
 LayoutInflater mInflater;
 public Myadapter(Context context, int resource, int textViewResourceId,
        ArrayLsit<HashMap<String,String> map, OnClickListener callback) {
    super(context, resource, textViewResourceId, map);
    this.map = map;
    mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if(convertView ==null)
    {
    holder = new ViewHolder();
    convertView = mInflater.inflate(R.layout.addtooutlet_list_item, parent,
            false);
    holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
    holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
    holder.b = (Button) convertView.findViewById(R.id.item_button);
    convertView.setTaf(holder); 
    }else{
           holder =(ViewHolder) convertView.getTag();
    }
    HashMap<String,String> hm = map.get(position);
    holder.tv1.setText(hm.get(postion).get("bus_name").toString());
    holder.tv2.setText(hm.get(postion).get("bus_id").toString());
    holder.b.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            Log.i("xx", "Button pressed!");
        }
    });

    return convertView;
}

ViewHodler

static class ViewHolder
{
     Button b;
     TextView tv1,tv2; 
} 

现在你应该在layout.addtooutlet_list_item.xml中有一个textview,并在getView中更新视图。

另请考虑使用ViewHolder模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html