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

OrderListView.java


public class OrderListView extends Activity {
        private ArrayList> arrayList = new ArrayList>();
        private Map map;
        MyAdapter adapter = null;
        public static List 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();
                        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



        
                
                        
                
                
                        
                
        

order_list_item.xml



        
        
        
        
        
        
        
        


你可能感兴趣的:(android)