4) Trường hợp 4: Sử dụng ArrayList và ListView nhưng từng phần tử trong ArrayList là các Object bất kỳ:
– Tôi có một ví dụ về hiển thị danh sách nhân viên theo mô hình sau:
– Có 2 loại nhân viên : Nhân viên chính thức (EmployeeFullTime ) và nhân viên thời vụ (EmployeePartime).
– Mỗi nhân viên sẽ có cách tính lương khác nhau (tên phương thức tính lương giống nhau)
– Mỗi nhân viên có phương thức toString để xuất thông tin, Nội dung xuất khác nhau. Thêm FullTime đằng sau Id và Name đối với nhân viên chính thức. Thêm Partime đằng sau Id và Name đối với nhân viên thời vụ.
– Xem giao diện chương trình:
-Tạo một Android Project tên: Vidu_ListView_ArrayList_Object, cấu trúc như bên dưới:
– Layout XML (activity_main.xml):
– Xem nội dung từng class:
– Bạn thấy là 2 class dẫn xuất từ Employee Tôi làm đơn giản là cách tính lương khác nhau. Đối với FullTime thì lương 500, Partime lương 150. và hàm Xuất toString() cũng khác nhau 1 xí.
– Ở đây ta sẽ áp dụng tính đa hình thông qua thừa kế, chỉ cần dùng một biến có kiểu Employee, nhưng nó có thể hiểu FullTime hoặc Partime và xuất ra thông tin đúng như mình mong đợi.
– Xem class 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
| package tranduythanh.com;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.RadioGroup;public class MainActivity extends Activity {EditText editId,editName; Button btnNhap; RadioGroup radgroup; ListView lvNhanvien; ArrayList<Employee>arrEmployee=new ArrayList<Employee>(); ArrayAdapter<Employee>adapter=null; //Khai báo 1 employee object Employee employee=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editId=(EditText) findViewById(R.id.editMa); editName=(EditText) findViewById(R.id.editTen); btnNhap=(Button) findViewById(R.id.btnnhap); radgroup=(RadioGroup) findViewById(R.id.radiogroud1); lvNhanvien=(ListView) findViewById(R.id.lvnhanvien); //đưa Data Source là các employee vào Adapter adapter=new ArrayAdapter<Employee>(this, android.R.layout.simple_list_item_1, arrEmployee); //đưa adapter vào ListView lvNhanvien.setAdapter(adapter); btnNhap.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub processNhap(); } }); } //Xử lý sự kiện nhập public void processNhap() { //Lấy ra đúng id của Radio Button được checked int radId=radgroup.getCheckedRadioButtonId(); String id=editId.getText()+""; String name=editName.getText()+""; if(radId==R.id.radChinhthuc) { //tạo instance là FullTime employee=new EmployeeFullTime(); } else { //Tạo instance là Partime employee=new EmployeePartTime(); } //FullTime hay Partime thì cũng là Employee //nên có các hàm này là hiển nhiên employee.setId(id); employee.setName(name); //Đưa employee vào ArrayList arrEmployee.add(employee); //Cập nhập giao diện adapter.notifyDataSetChanged(); }} |
– Chương trình sẽ tự động hiển thị đúng loại Employee mà ta chọn lựa trên giao diện, ở đây bạn lại được ôn tập thêm về tính đa hình trong java. Ứng với mỗi loại Employee nó sẽ tự động gọi hàm toString của loại đó.
Ví dụ: Nếu Employee đó là FullTime thì nó gọi toString của FullTime và ngược lại với PartTime cũng vậy nó sẽ lấy toString của PartTime.
– Đến đây bạn đã hiểu tương đối sâu về ListView, bạn có thể tải coding mẫu ở đây: http://www.mediafire.com/?o1we6e0mw8cpbba
5)Trường hợp 5: Sử dụng ListView nhưng dưới dạng ListActivity:
– Thay vì kế thừa từ Activity, ta sẽ cho kế thừa từ ListActivity.
– Và dĩ nhiên cách đặt Id cho ListView cũng có sự khác biệt.
– Bạn xem giao diện bên dưới:
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
| android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" ><TextView android:id="@+id/selection" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#008000" android:textColor="#FFFFFF" android:textSize="20sp" /><ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView><TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Không có gì cả" /></LinearLayout> |
– Bạn nhìn vào dòng lệnh thứ 18:
android:id=”@android:id/list“, bạn viết y xì như thế này. Bởi vì nó là id được định nghĩa sẵn bên trong Android.
– Tiếp tục nhìn vào dòng lệnh 24:
android:id=”@android:id/empty“ , cũng là có sẵn của Android. Nó có tác dụng tự động thông báo khi ListView của bạn không có bất kỳ một phần tử nào cả.
– Xem class 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
| package tranduythanh.com;import android.os.Bundle;import android.app.ListActivity;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends ListActivity {TextView selection; String arr[]={"Intel","SamSung", "Nokia","Simen","AMD", "KIC","ECD"}; ArrayAdapter<String >adapter=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Thiết lập Data Source cho Adapter adapter=new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, arr); //Gán Adapter vào ListView //Nhớ là phải đặt id cho ListView theo đúng quy tắc setListAdapter(adapter); selection=(TextView) findViewById(R.id.selection); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); String txt="postion = "+position +"; value ="+arr[position]; selection.setText(txt); }} |
– Bạn nhìn vào dòng lệnh số 10: bạn thấy đấy, Tôi cho kế thừa từ ListActivity chứ không phảiActivity. (Android đã viết class ListActivity kế thừa từ Activity rồi). Tức là ListActivity cũng chính là Activity.
– Bạn nhìn vào dòng lệnh số 28:
setListAdapter(adapter);
Ở đây ta hoàn toàn không “Lôi” control ListView ra. Mà ta chỉ cần gọi hàm setListAdapter thì ListView cũng tự động cập nhập dữ liệu. Bạn phải làm chính xác 2 nơi thì mới được như vậy:
1. Kết thừa từ ListActivity,
2. đặt id cho ListView theo đúng quy tắc android:id=”@android:id/list”
– Bạn load coding đầy đủ ở đây: http://www.mediafire.com/?mjxgs1yv0mvujuz
*** Như vậy bạn đã thực hành xong 5 trường hợp mà Tôi đã nêu. Tôi hi vọng các bạn hãy làm lại nhiều lần để có thể hiểu sâu hơn về nó.
Bài tập kế tiếp bạn sẽ được thực hành về cách Custom layout lại ListView theo ý thích của bạn.
Chúc bạn thành công.

No comments:
Post a Comment