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

OrderListView.java


public 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

<?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

<?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>


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值