-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCustomAdapter.txt
More file actions
executable file
·72 lines (62 loc) · 2.43 KB
/
CustomAdapter.txt
File metadata and controls
executable file
·72 lines (62 loc) · 2.43 KB
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
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
/**
* Constructor này dùng d? kh?i t?o các giá tr?
* t? CustomListViewActivity truy?n vào
*
* @param context : là Activity t? CustomListView
* @param imageId: Là danh sách image c?a list item truy?n t? Main
* @param result : Danh sách n?i dung c?a list item truy?n t? Main
*/
public CustomAdapter(Context context, String[] result, int[] imageId) {
this.context = context;
this.result = result;
this.imageId = imageId;
}
//Tr? v? d? dài c?a m?ng ch?a n?i dung list item
@Override
public int getCount() {
return result.length;
}
//Tr? v? v? trí c?a m?ng ch?a n?i dung list item
@Override
public Object getItem(int position) {
return position;
}
//Tr? v? v? trí c?a m?ng image list item
@Override
public long getItemId(int position) {
return position;
}
/**
* hàm dùng d? custom layout, ta ph?i override l?i hàm này
* t? CustomListViewActivity truy?n vào
*
* @param position : là v? trí c?a ph?n t? trong danh sách Item
* @param convertView: convertView, dùng nó d? x? lý Item
* @param parent : Danh sách truy?n t? Main
* @return View: tr? v? chính convertView
*/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.custom_list_item, parent, false);
TextView tvNoiDung = (TextView) rowView.findViewById(R.id.tvNoiDung);
ImageView imgAvatar = (ImageView) rowView.findViewById(R.id.imgAvatatr);
//l?y N?i dung c?a Item ra d? thi?t l?p n?i dung item cho dúng
tvNoiDung.setText(result[position]);
//l?y ImageView ra d? thi?t l?p hình ?nh cho dúng
imgAvatar.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
}
});
//tr? v? View này, t?c là tr? luôn v? các thông s? m?i mà ta v?a thay d?i
return rowView;
}
}