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

OrderListView.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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>