在Android中的每行添加和删除buttonListView

我正在开发一个Android应用程序,我已经做了一个ListView。 我必须在ListView中的每一行添加2个button。 这2个button是“添加”和“删除”。 当用户select其中一个button时,应采取一些行动。 我该怎么做?

您首先需要创build一个自定义布局xml,它将代表您的列表中的单个项目。 你将把你的两个button和你想要从列表中显示的其他项目一起添加到这个布局中。

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/list_item_string" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:paddingLeft="8dp" android:textSize="18sp" android:textStyle="bold" /> <Button android:id="@+id/delete_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:text="Delete" /> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/delete_btn" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:text="Add" /> </RelativeLayout> 

接下来,您将需要创build一个自定义ArrayAdapter类,您将使用它来扩充xml布局,以及处理button和点击事件。

 public class MyCustomAdapter extends BaseAdapter implements ListAdapter { private ArrayList<String> list = new ArrayList<String>(); private Context context; public MyCustomAdapter(ArrayList<String> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int pos) { return list.get(pos); } @Override public long getItemId(int pos) { return list.get(pos).getId(); //just return 0 if your list items do not have an Id variable. } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.my_custom_list_layout, null); } //Handle TextView and display string from your list TextView listItemText = (TextView)view.findViewById(R.id.list_item_string); listItemText.setText(list.get(position)); //Handle buttons and add onClickListeners Button deleteBtn = (Button)view.findViewById(R.id.delete_btn); Button addBtn = (Button)view.findViewById(R.id.add_btn); deleteBtn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //do something list.remove(position); //or some other task notifyDataSetChanged(); } }); addBtn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //do something notifyDataSetChanged(); } }); return view; } } 

最后,在您的活动中,您可以实例化您的自定义ArrayAdapter类并将其设置为您的列表视图。

 public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_activity); //generate list ArrayList<String> list = new ArrayList<String>(); list.add("item1"); list.add("item2"); //instantiate custom adapter MyCustomAdapter adapter = new MyCustomAdapter(list, this); //handle listview and assign adapter ListView lView = (ListView)findViewById(R.id.my_listview); lView.setAdapter(adapter); } 

希望这可以帮助!

在删除button点击事件

 public void delete(View v){ ListView listview1; ArrayList<E> datalist; final int position = listview1.getPositionForView((View) v.getParent()); datalist.remove(position); myAdapter.notifyDataSetChanged(); } 
 public class UserCustomAdapter extends ArrayAdapter<User> { Context context; int layoutResourceId; ArrayList<User> data = new ArrayList<User>(); public UserCustomAdapter(Context context, int layoutResourceId, ArrayList<User> data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; UserHolder holder = null; if (row == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new UserHolder(); holder.textName = (TextView) row.findViewById(R.id.textView1); holder.textAddress = (TextView) row.findViewById(R.id.textView2); holder.textLocation = (TextView) row.findViewById(R.id.textView3); holder.btnEdit = (Button) row.findViewById(R.id.button1); holder.btnDelete = (Button) row.findViewById(R.id.button2); row.setTag(holder); } else { holder = (UserHolder) row.getTag(); } User user = data.get(position); holder.textName.setText(user.getName()); holder.textAddress.setText(user.getAddress()); holder.textLocation.setText(user.getLocation()); holder.btnEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("Edit Button Clicked", "**********"); Toast.makeText(context, "Edit button Clicked", Toast.LENGTH_LONG).show(); } }); holder.btnDelete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("Delete Button Clicked", "**********"); Toast.makeText(context, "Delete button Clicked", Toast.LENGTH_LONG).show(); } }); return row; } static class UserHolder { TextView textName; TextView textAddress; TextView textLocation; Button btnEdit; Button btnDelete; } } 

嘿,请看看这里 –

我的博客上有同样的答案