2015-09-15 android ListView中嵌入Button,并响应Button点击事件 OrderListView.java 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126public class OrderListView extends Activity { private ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>(); private Map<String, Object> map; MyAdapter adapter = null; public static List<Order> order = new ArrayList(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.order_list); initDate(); adapter = new MyAdapter(this); ListView list = (ListView) findViewById(R.id.order_list); list.setAdapter(adapter); } private void initDate() { arrayList.clear(); for (int i = 0; i < 10; i++) { map = new HashMap<String, Object>(); map.put("序号", "" + (i + 1)); map.put("菜号", "000"); map.put("菜名", "ZZZ"); map.put("份数", "1"); map.put("单价", "¥100"); map.put("总价", "¥100"); arrayList.add(map); } } public Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: arrayList.remove(msg.arg1); adapter.notifyDataSetChanged(); break; } super.handleMessage(msg); } }; public class MyAdapter extends BaseAdapter { private LayoutInflater inflater; public MyAdapter(Context c) { this.inflater = LayoutInflater.from(c); } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } /** * 设置数据源与行View关联 设置行中个组件的事件响应 返回设置好的View */ @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // 取得要显示的行View View myView = inflater.inflate(R.layout.order_list_item, null); LinearLayout layout = (LinearLayout) myView .findViewById(R.id.LinearLayoutOLI); if (arg0 % 2 == 0) { layout.setBackgroundDrawable(getResources().getDrawable( R.drawable.bg1)); } else { layout.setBackgroundDrawable(getResources().getDrawable( R.drawable.bg2)); } TextView textView1 = (TextView) myView.findViewById(R.id.textView1); TextView textView2 = (TextView) myView.findViewById(R.id.textView2); TextView textView3 = (TextView) myView.findViewById(R.id.textView3); TextView textView4 = (TextView) myView.findViewById(R.id.textView4); TextView textView5 = (TextView) myView.findViewById(R.id.textView5); Button button = (Button) myView.findViewById(R.id.button6); // 让行View的每个组件与数据源相关联 textView1.setText((String) arrayList.get(arg0).get("序号")); textView2.setText((String) arrayList.get(arg0).get("菜名")); textView3.setText((String) arrayList.get(arg0).get("份数")); textView4.setText((String) arrayList.get(arg0).get("单价")); textView5.setText((String) arrayList.get(arg0).get("总价")); button.setFocusable(false); final int arg = arg0; // 添加事件响应 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new AlertDialog.Builder(OrderListView.this).setTitle( "您确定删除吗?").setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Message message = new Message(); message.what = 1; message.arg1 = arg; handler.sendMessage(message); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }).show(); } }); return myView; } } } order_list.xml 1234567891011121314151617<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" androidrientation="vertical" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow> <ImageView android:layout_height="35px" android:layout_width="fill_parent" android:src="@drawable/cd" /> </TableRow> <TableRow> <ListView android:layout_width="fill_parent" android:layout_height="390px" android:id="@+id/order_list" /> </TableRow> </TableLayout> </LinearLayout> order_list_item.xml 12345678910111213141516171819202122232425262728<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayoutOLI" androidrientation="vertical" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:background="@drawable/bg1"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow> <TextView android:id="@+id/textView1" android:layout_width="72px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView2" android:layout_width="270px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView3" android:layout_width="85px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView4" android:layout_width="105px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView5" android:layout_width="106px" android:layout_height="wrap_content" android:textSize="25sp" /> <Button android:id="@+id/button6" android:text="删除" android:layout_width="164px" android:focusable="false" android:layout_height="wrap_content" android:textSize="25sp" /> </TableRow> </TableLayout> </LinearLayout> 前一篇 Android购物车demo 后一篇 遍历Map的两种方法:keySet()和entrySet()