Android ListView内的Button添加事件的两种方法

来源:互联网 发布:sql注入过安全狗 编辑:程序博客网 时间:2024/05/21 12:38

一、在layer配置文件中添加onClick属性,并在activity中实现该方法:

1、配置文件:

<?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:orientation="horizontal" >    <TextView        android:id="@+id/mySeq"        android:layout_width="0dip"        android:layout_height="40dp"        android:layout_weight="1"        android:background="@drawable/borderstyle"        android:textColor="#000000"        android:textSize="25sp" />    <TextView        android:id="@+id/myUsename"        android:layout_width="0dip"        android:layout_height="40dp"        android:layout_weight="2"        android:background="@drawable/borderstyle"        android:textColor="#000000"        android:textSize="25sp" />    <TextView        android:id="@+id/myPhone"        android:layout_width="0dip"        android:layout_height="40dp"        android:layout_weight="3"        android:background="@drawable/borderstyle"        android:textColor="#000000"        android:textSize="25sp" />    <Button        android:id="@+id/btnDel"        android:layout_width="0dip"        android:layout_height="40dp"        android:layout_weight="2"        android:background="@drawable/borderstyle"        android:text="@string/btnDel"        android:onClick="actionDel"////////此处添加Button关联事件!         /></LinearLayout>
2、activity中实现该方法:

public void actionDel(View view){//////方法主体}

3、Adapter使用SimpleAdapter即可:

SimpleAdapter adapter = new SimpleAdapter(this, data,R.layout.mylistview,new String[] { "id", "userName", "phone" }, new int[] {R.id.mySeq, R.id.myUsename, R.id.myPhone });listView.setAdapter(adapter);


二、在配置文件中,button不用关联事件,全部在activity中实现绑定,需要实现Adapter接口:

1、配置文件如上,不过不用配置【android:onClick="actionDel"】;

2、扩展Adapter抽象类:

public class MyAdapter extends BaseAdapter {public int getCount() {return data.size();}public Object getItem(int position) {return data.get(position);}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {LayoutInflater inflater = Contracts.this.getLayoutInflater();final View view = inflater.inflate(R.layout.mylistview, null);///加载listView布局文件final TextView textPhone = (TextView) view.findViewById(R.id.myPhone);final TextView textName = (TextView) view.findViewById(R.id.myUsename);final TextView textSeq = (TextView) view.findViewById(R.id.mySeq);Button button = (Button) view.findViewById(R.id.btnDel);textSeq.setText(data.get(position).get("id").toString());textName.setText(data.get(position).get("userName"));textPhone.setText(data.get(position).get("phone"));////绑定listview与listbutton.setTag(position);/////添加button事件button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {int seq = Integer.parseInt(v.getTag().toString());data.remove(seq);updateSeq(data);MyAdapter.this.notifyDataSetChanged();}});return view;}}
3、调用该adapter:

BaseAdapter adapter = new MyAdapter();listView.setAdapter(adapter);




0 0
原创粉丝点击