慕桂英4014372
您首先需要创建一个自定义布局xml,它将代表列表中的单个项目。您将在此布局中添加两个按钮,以及您要从列表中显示的其他任何项目。<?xml version="1.0" encoding="utf-8"?> 接下来,您将需要创建一个自定义ArrayAdapter类,该类将用于膨胀xml布局以及处理按钮和单击事件。public class MyCustomAdapter extends BaseAdapter implements ListAdapter { private ArrayList list = new ArrayList(); private Context context; public MyCustomAdapter(ArrayList list, Context context) { this.list = list; this.context = context; } @Overridepublic int getCount() { return list.size(); } @Overridepublic Object getItem(int pos) { return list.get(pos); } @Overridepublic long getItemId(int pos) { return list.get(pos).getId(); //just return 0 if your list items do not have an Id variable.} @Overridepublic 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 { @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_activity); //generate list ArrayList list = new ArrayList(); 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);}希望这可以帮助!