當 ListView 可以加上許多元件時
試著加入按鈕一定是理所當然的
不過 ListView 跟 Button 都有 Click 事件
再加上要彈出帶 ID 的對話框
可就不單純了
問題一
在 ListView 自訂項目畫面的 XML 檔裡加上按鈕
要注意按鈕的 focusable 要設定 false
android:focusable=”false”
避免加上按鈕後
ListView 的 ItemClick 事件無法觸發
問題二
ListView 中按鈕的 OnClick 監聽事件要在 BaseAdapter 中綁定
必須自訂監聽事件類別並 implements OnClickListener
這時點下按鈕就可以取得對應的 ID
另一個問題是如果要彈出對話框
在監聽事件類別並不能直接建立 AlertDialog.Builder(this)
要抓到 MainActivity 的實體
去呼叫函式並傳入 ID
呼叫流程
MainActivity 傳入本身實體給 BaseAdapter
BaseAdapter 在綁定按鈕的監聽事件時將 MainActivity 實體及 ID 傳入自訂監聽事件類別
當點擊按鈕時觸發自訂監聽事件類別的 MainActivity 實體執行函式
結果是在 MainActivity 實體建立 AlertDialog.Builder(this)
最後彈出對話框
程式碼
MainActivity.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
|
/** 綁定列表資料 */ private void BindData() { ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); ListView listView = (ListView) findViewById(R.id.listView1); HashMap<String, String> item = new HashMap<String, String>(); item.put( "id" , "15" ); item.put( "name" , "用戶A" ); list.add(item); listView.setAdapter( new MyBaseAdapter( this , list)); // 點擊項目 listView.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { Log.d( "allenj" , "ListViewItem = " + id); } }); } /** 彈出對話框 */ public void myDialog( final int id) { Log.d( "allenj" , "myDialog = " + id); AlertDialog.Builder builder = new AlertDialog.Builder( this ); builder.setMessage( "確定刪除" ) .setPositiveButton( "是" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.d( "allenj" , "刪除成功 " + id); } }) .setNegativeButton( "否" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.d( "allenj" , "不要刪除 " + id); } }); AlertDialog ad = builder.create(); ad.show(); } |
MyBaseAdapter.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
|
private MainActivity mainActivity; private LayoutInflater myInflater; private ArrayList<HashMap<String, String>> list = null ; private ViewTag viewTag; public MyBaseAdapter(MainActivity context, ArrayList<HashMap<String, String>> list) { //取得 MainActivity 實體 this .mainActivity = context; this .myInflater = LayoutInflater.from(context); this .list = list; } public int getCount() { return list.size(); } public Object getItem( int position) { return list.get(position); } public long getItemId( int position) { return Long.valueOf(list.get(position).get( "id" )); } public View getView( int position, View convertView, ViewGroup parent) { if (convertView == null ) { // 取得listItem容器 view convertView = myInflater.inflate(R.layout.mylistview, null ); // 建構listItem內容view viewTag = new ViewTag( (TextView) convertView.findViewById(R.id.textView1), (Button) convertView.findViewById(R.id.button1)); // 設置容器內容 convertView.setTag(viewTag); } else { viewTag = (ViewTag) convertView.getTag(); } viewTag.text1.setText(list.get(position).get( "name" )); //設定按鈕監聽事件及傳入 MainActivity 實體 viewTag.btn1.setOnClickListener( new ItemButton_Click( this .mainActivity, position)); return convertView; } public class ViewTag { TextView text1; TextView btn1; public ViewTag(TextView textview1, Button button1) { this .text1 = textview1; this .btn1 = button1; } } //自訂按鈕監聽事件 class ItemButton_Click implements OnClickListener { private int position; private MainActivity mainActivity; ItemButton_Click(MainActivity context, int pos) { this .mainActivity = context; position = pos; } public void onClick(View v) { Log.d( "allenj" , "ItemButton = " + list.get(position).get( "id" )); this .mainActivity.myDialog(Integer.valueOf(list.get(position).get( "id" ))); } } |
mylistview.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginLeft = "6dip" android:layout_marginTop = "6dip" android:layout_weight = "1" android:text = "TextView" android:textAppearance = "?android:attr/textAppearanceLarge" /> < Button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:focusable = "false" android:text = "刪除" /> |
結果畫面
refer: http://www.allenj.net/archives/2751
ListView 裡加上 Button 分別監聽 Click 動作