ListView中嵌入Button,并响应Button点击事件

  1. public class OrderListView extends Activity {  
  2.         private ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();  
  3.         private Map<String, Object> map;  
  4.         MyAdapter adapter = null;  
  5.         public static List<Order> order = new ArrayList();  
  6.   
  7.         /** Called when the activity is first created. */  
  8.         @Override  
  9.         public void onCreate(Bundle savedInstanceState) {  
  10.                 super.onCreate(savedInstanceState);  
  11.                 setContentView(R.layout.order_list);  
  12.                 initDate();  
  13.                 adapter = new MyAdapter(this);  
  14.                 ListView list = (ListView) findViewById(R.id.order_list);  
  15.                 list.setAdapter(adapter);  
  16.                   
  17.                   
  18.         }  
  19.         private void initDate() {  
  20.                 arrayList.clear();  
  21.                 for (int i = 0; i < 10; i++) {  
  22.                         map = new HashMap<String, Object>();  
  23.                         map.put("序号""" + (i + 1));  
  24.                         map.put("菜号""000");  
  25.                         map.put("菜名""ZZZ");  
  26.                         map.put("份数""1");  
  27.                         map.put("单价""¥100");  
  28.                         map.put("总价""¥100");  
  29.                         arrayList.add(map);  
  30.                 }  
  31.         }  
  32.   
  33.         public Handler handler = new Handler() {  
  34.                 public void handleMessage(Message msg) {  
  35.                         switch (msg.what) {  
  36.                         case 1:  
  37.                                 arrayList.remove(msg.arg1);  
  38.                                 adapter.notifyDataSetChanged();  
  39.                                 break;  
  40.                         }  
  41.                         super.handleMessage(msg);  
  42.                 }  
  43.         };  
  44.   
  45.         public class MyAdapter extends BaseAdapter {  
  46.                 private LayoutInflater inflater;  
  47.   
  48.                 public MyAdapter(Context c) {  
  49.                         this.inflater = LayoutInflater.from(c);  
  50.                 }  
  51.   
  52.                 @Override  
  53.                 public int getCount() {  
  54.                         return arrayList.size();  
  55.                 }  
  56.   
  57.                 @Override  
  58.                 public Object getItem(int arg0) {  
  59.                         // TODO Auto-generated method stub  
  60.                         return null;  
  61.                 }  
  62.   
  63.                 @Override  
  64.                 public long getItemId(int arg0) {  
  65.                         // TODO Auto-generated method stub  
  66.                         return 0;  
  67.                 }  
  68.   
  69.                 /** 
  70.                  * 设置数据源与行View关联 设置行中个组件的事件响应 返回设置好的View 
  71.                  */  
  72.                 @Override  
  73.                 public View getView(int arg0, View arg1, ViewGroup arg2) {  
  74.                         // 取得要显示的行View  
  75.                         View myView = inflater.inflate(R.layout.order_list_item, null);  
  76.                         LinearLayout layout = (LinearLayout) myView  
  77.                                         .findViewById(R.id.LinearLayoutOLI);  
  78.                         if (arg0 % 2 == 0) {  
  79.                                 layout.setBackgroundDrawable(getResources().getDrawable(  
  80.                                                 R.drawable.bg1));  
  81.                         } else {  
  82.                                 layout.setBackgroundDrawable(getResources().getDrawable(  
  83.                                                 R.drawable.bg2));  
  84.                         }  
  85.                         TextView textView1 = (TextView) myView.findViewById(R.id.textView1);  
  86.                         TextView textView2 = (TextView) myView.findViewById(R.id.textView2);  
  87.                         TextView textView3 = (TextView) myView.findViewById(R.id.textView3);  
  88.                         TextView textView4 = (TextView) myView.findViewById(R.id.textView4);  
  89.                         TextView textView5 = (TextView) myView.findViewById(R.id.textView5);  
  90.                         Button button = (Button) myView.findViewById(R.id.button6);  
  91.                         // 让行View的每个组件与数据源相关联  
  92.                         textView1.setText((String) arrayList.get(arg0).get("序号"));  
  93.                         textView2.setText((String) arrayList.get(arg0).get("菜名"));  
  94.                         textView3.setText((String) arrayList.get(arg0).get("份数"));  
  95.                         textView4.setText((String) arrayList.get(arg0).get("单价"));  
  96.                         textView5.setText((String) arrayList.get(arg0).get("总价"));  
  97.                         button.setFocusable(false);  
  98.                         final int arg = arg0;  
  99.                         // 添加事件响应  
  100.                         button.setOnClickListener(new OnClickListener() {  
  101.                                 @Override  
  102.                                 public void onClick(View v) {  
  103.                                         // TODO Auto-generated method stub  
  104.                                         new AlertDialog.Builder(OrderListView.this).setTitle(  
  105.                                                         "您确定删除吗?").setPositiveButton("确定",  
  106.                                                         new DialogInterface.OnClickListener() {  
  107.                                                                 public void onClick(DialogInterface dialog,  
  108.                                                                                 int whichButton) {  
  109.                                                                         Message message = new Message();  
  110.                                                                         message.what = 1;  
  111.                                                                         message.arg1 = arg;  
  112.                                                                         handler.sendMessage(message);  
  113.                                                                 }  
  114.                                                         }).setNegativeButton("取消",  
  115.                                                         new DialogInterface.OnClickListener() {  
  116.                                                                 public void onClick(DialogInterface dialog,  
  117.                                                                                 int whichButton) {  
  118.                                                                 }  
  119.                                                         }).show();  
  120.                                 }  
  121.                         });  
  122.                         return myView;  
  123.                 }  
  124.         }  
  125.   
  126. }  

order_list.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout android:id="@+id/LinearLayout01"  
  3.         android:layout_width="fill_parent" androidrientation="vertical"  
  4.         android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">  
  5.         <TableLayout android:layout_width="fill_parent"  
  6.                 android:layout_height="wrap_content">  
  7.                 <TableRow>  
  8.                         <ImageView android:layout_height="35px"  
  9.                                 android:layout_width="fill_parent"  
  10.                                 android:src="@drawable/cd" />  
  11.                 </TableRow>  
  12.                 <TableRow>  
  13.                         <ListView android:layout_width="fill_parent"  
  14.                                 android:layout_height="390px" android:id="@+id/order_list" />  
  15.                 </TableRow>  
  16.         </TableLayout>  
  17. </LinearLayout>  

order_list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:id="@+id/LinearLayoutOLI"  
  3.         androidrientation="vertical" android:layout_width="fill_parent"  
  4.         xmlns:android="http://schemas.android.com/apk/res/android"  
  5.         android:layout_height="wrap_content"  
  6.         android:background="@drawable/bg1">  
  7.         <TableLayout   
  8.    android:layout_width="fill_parent"   
  9.    android:layout_height="wrap_content">  
  10.         <TableRow>  
  11.         <TextView android:id="@+id/textView1" android:layout_width="72px"  
  12.                 android:layout_height="wrap_content" android:textSize="25sp" />  
  13.         <TextView android:id="@+id/textView2" android:layout_width="270px"  
  14.                 android:layout_height="wrap_content" android:textSize="25sp" />  
  15.         <TextView android:id="@+id/textView3" android:layout_width="85px"  
  16.                 android:layout_height="wrap_content" android:textSize="25sp" />  
  17.         <TextView android:id="@+id/textView4" android:layout_width="105px"  
  18.                 android:layout_height="wrap_content" android:textSize="25sp" />  
  19.         <TextView android:id="@+id/textView5" android:layout_width="106px"  
  20.                 android:layout_height="wrap_content" android:textSize="25sp" />  
  21.         <Button android:id="@+id/button6"  android:text="删除"  
  22.             android:layout_width="164px"  
  23.             android:focusable="false"  
  24.                 android:layout_height="wrap_content" android:textSize="25sp" />  
  25.         </TableRow>  
  26.         </TableLayout>  
  27.           
  28. </LinearLayout> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值