Về cơ bản tất cả các listview đều phải qua các bước khá giống nhau
- LoadData vào mảng dữ liệu để hiển thị lên listview
- Khởi tạo adapter
- Khởi tạo listview và setadapter
Sự khác nhau giữa Listview simple và ListView custom layout item nằm ở việc khởi tạo adapter
Trong ví dụ này mình sẽ giới thiệu một cách khá đơn giản để tạo listview custom layout item đó là sử dụng SimpleAdapter
Các bước làm và điểm cần lưu ý mình đã chỉ rõ ở trong code
Để làm ví dụ này các bạn thực hiện theo các bước sau
Bước 1: Mở file res-> values ->strings.xml
1
2
3
4
5
6
7
8
9
10
11
| <? xml version = "1.0" encoding = "utf-8" ?></ pre > < resources > < string name = "app_name" >ListViewCustom</ string > < string name = "action_settings" >Settings</ string > < string name = "hello_world" >Hello world!</ string > < string name = "content" >Image of national flag</ string > < string name = "VietNam" >Viet Nam</ string > < string name = "popVN" >82 000 000</ string > </ resources > |
Bước 2: Mở file res-> layout ->activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
| xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > < ListView android:id = "@+id/lvDemo" android:layout_width = "fill_parent" android:layout_height = "fill_parent" /> </ RelativeLayout > |
Bước 3: tạo file res-> layout ->layout_item.xml (đây là file giao diện của 1 item listview)
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
| <? xml version = "1.0" encoding = "utf-8" ?></ pre > android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:gravity = "center_vertical" android:orientation = "horizontal" android:paddingLeft = "10dp" android:paddingRight = "10dp" > < ImageView android:id = "@+id/ivIcon" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:contentDescription = "@string/content" android:src = "@drawable/vietnam" /> < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_marginLeft = "5dp" android:orientation = "vertical" > < TextView android:id = "@+id/tvName" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/VietNam" /> < TextView android:id = "@+id/tvPopular" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/popVN" /> </ LinearLayout > </ LinearLayout > |
Bước 4: Mở file src-> 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
| package com.example.listviewcustom;</pre> import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { // khai bao cac hang String can thiet cho HashMap public static final String IMAGE = "Image" ; public static final String NAME = "Name" ; public static final String POP = "Popular" ; // init cac thanh phan can thiet de hien thi listview private ListView lv; private ArrayList<HashMap<String, String>> array; private SimpleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // set giao dien cho Activity setContentView(R.layout.activity_main); // link lv den layout de dieu khien lv = (ListView) findViewById(R.id.lvDemo); // load data vao mang de hien thi ra list // voi listview su dung SimpleAdapter ban can phai su dung mang du lieu // kieu ArrayList<HashMap<String, String>> // HashMap<String, String> la kieu du lieu kha pho bien va hay dung // no la kieu du lieu co 2 thanh phan tag va value tuong tu nhu json // hay contentValue LoadData(); // init adapter // mang from chua cac tag se lay du lieu trong mang String[] from = { IMAGE, NAME, POP }; // mag to chua cac id cua view se duoc gan boi cac tag tren int [] to = { R.id.ivIcon, R.id.tvName, R.id.tvPopular }; // khi init SimpleAdapter cac param truyen vao gom co adapter = new SimpleAdapter(MainActivity. this , // context array, // mang du lieu se hien thi len listview R.layout.layout_item, // layout 1 item cua listview from, // lay du lieu trong mang theo cac tag to // lay du lieu theo cac tag gan cho cac view voi id tuong ung ); // trong vi du nay voi mang from va to nhu tren // gia tri cua tag IMAGE -> ivIcon // gia tri cua tag NAME -> tvName // gia tri cua tag POP -> tvPopular // set adapter cho listview lv.setAdapter(adapter); // bat su kien onclick vao item ListView lv.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int pos, long id) { // TODO Auto-generated method stub // khi click vao item listview se thuc hien cac lenh trong phuong thuc nay // trong vi du nay minh se show toast va log ra logcat HashMap<String, String> posItem = array.get(pos); //String id_IMAGE = (String) posItem.get(IMAGE); String nameItem = (String) posItem.get(NAME); String popItem = (String) posItem.get(POP); Toast.makeText(MainActivity. this , nameItem + " " + popItem, Toast.LENGTH_LONG).show(); Log.d( "debug" , "pos " + pos + " " + nameItem + " " + popItem); } }); } @Override protected void onStart() { // TODO Auto-generated method stub super .onStart(); } private void LoadData() { // TODO Auto-generated method stub // trong vi du nay minh tu khoi tao mot mang du lieu va hien thi len listview // thuc te mang data co the load tu server hoac lay tu cac provider khac array = new ArrayList<HashMap<String, String>>(); HashMap<String, String> temp1 = new HashMap<String, String>(); temp1.put(IMAGE, String.valueOf(R.drawable.canada)); temp1.put(NAME, "CANADA" ); temp1.put(POP, "100000000" ); array.add(temp1); HashMap<String, String> temp2 = new HashMap<String, String>(); temp2.put(IMAGE, String.valueOf(R.drawable.gb)); temp2.put(NAME, "GB" ); temp2.put(POP, "200000000" ); array.add(temp2); HashMap<String, String> temp3 = new HashMap<String, String>(); temp3.put(IMAGE, String.valueOf(R.drawable.south_korea)); temp3.put(NAME, "South Korea" ); temp3.put(POP, "300000000" ); array.add(temp3); HashMap<String, String> temp4 = new HashMap<String, String>(); temp4.put(IMAGE, String.valueOf(R.drawable.sweden)); temp4.put(NAME, "sweden" ); temp4.put(POP, "400000000" ); array.add(temp4); HashMap<String, String> temp5 = new HashMap<String, String>(); temp5.put(IMAGE, String.valueOf(R.drawable.usa)); temp5.put(NAME, "USA" ); temp5.put(POP, "500000000" ); array.add(temp5); HashMap<String, String> temp6 = new HashMap<String, String>(); temp6.put(IMAGE, String.valueOf(R.drawable.vietnam)); temp6.put(NAME, "vietnam" ); temp6.put(POP, "600000000" ); array.add(temp6); HashMap<String, String> temp7 = new HashMap<String, String>(); temp7.put(IMAGE, String.valueOf(R.drawable.canada)); temp7.put(NAME, "CANADA" ); temp7.put(POP, "100000000" ); array.add(temp7); HashMap<String, String> temp8 = new HashMap<String, String>(); temp8.put(IMAGE, String.valueOf(R.drawable.gb)); temp8.put(NAME, "GB" ); temp8.put(POP, "200000000" ); array.add(temp8); HashMap<String, String> temp9 = new HashMap<String, String>(); temp9.put(IMAGE, String.valueOf(R.drawable.south_korea)); temp9.put(NAME, "South Korea" ); temp9.put(POP, "300000000" ); array.add(temp9); HashMap<String, String> temp10 = new HashMap<String, String>(); temp10.put(IMAGE, String.valueOf(R.drawable.sweden)); temp10.put(NAME, "sweden" ); temp10.put(POP, "400000000" ); array.add(temp10); HashMap<String, String> temp11 = new HashMap<String, String>(); temp11.put(IMAGE, String.valueOf(R.drawable.usa)); temp11.put(NAME, "USA" ); temp11.put(POP, "500000000" ); array.add(temp11); HashMap<String, String> temp12 = new HashMap<String, String>(); temp12.put(IMAGE, String.valueOf(R.drawable.vietnam)); temp12.put(NAME, "vietnam" ); temp12.put(POP, "600000000" ); array.add(temp12); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
Kết quả thực hiện chương trình
0 nhận xét:
Đăng nhận xét