- Bây giờ ta qua package tranduythanh.com.adapter:
- 2 Adapter này dùng để custom layout cho danh sách Phòng ban và danh sách nhân viên.
- Chú ý là cả 2 Adapter này đều dùng chung 1 Layout tên “layout_item_custom.xml“:
- Bạn xem source XML của “layout_item_custom.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
29
30
31
32
| <?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" ><ImageView android:id="@+id/imgview" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:id="@+id/txtShortInfor" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout><TextView android:id="@+id/txtDetailInfor" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000080" android:textSize="10sp" android:textStyle="italic" /></LinearLayout> |
- Bây giờ ta xem cách custom layout thông qua ADapter (phần customlayout Tôi đã hướng dẫn ở các bài tập trước, bạn tự xem lại):
- Coding PhongBanAdapter.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
| package tranduythanh.com.adapter;import java.util.ArrayList;import tranduythanh.com.activity.R;import tranduythanh.com.model.NhanVien;import tranduythanh.com.model.PhongBan;import android.app.Activity;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;/** * Class này dùng để custom layout cho Danh sách phòng ban * @author drthanh * */public class PhongBanAdapter extends ArrayAdapter<PhongBan> { Activity context; int layoutId; ArrayList<PhongBan> arrPhongBan; public PhongBanAdapter(Activity context, int textViewResourceId, ArrayList<PhongBan> objects) { super(context, textViewResourceId, objects); this.context=context; this.layoutId=textViewResourceId; this.arrPhongBan=objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { //gán layout vào coding convertView=context.getLayoutInflater().inflate(layoutId, null); //lấy các control ra theo id TextView txtpb= (TextView) convertView.findViewById(R.id.txtShortInfor); TextView txtmotapb= (TextView) convertView.findViewById(R.id.txtDetailInfor); //Lấy phòng ban thứ position PhongBan pb=arrPhongBan.get(position); txtpb.setText(pb.toString()); /** * Các Dòng lệnh dưới này để kiểm tra Trưởng phòng, phó phòng */ String strMota=""; String tp="Trưởng Phòng: [Chưa có]"; NhanVien nv=pb.getTruongPhong(); if(nv!=null) { tp="Trưởng Phòng: ["+nv.getTen()+"]"; } ArrayList<NhanVien> dsPp=pb.getPhoPhong(); String pp="Phó phòng: [Chưa có]"; if(dsPp.size()>0) { pp="Phó phòng:\n"; for(int i=0;i<dsPp.size();i++) { pp+=(i+1)+". "+dsPp.get(i).getTen()+"\n"; } } strMota=tp+"\n"+pp; //gán thông tin cho phần chi tiết txtmotapb.setText(strMota); return convertView; }} |
0 nhận xét:
Đăng nhận xét