Bài 16: Kết hợp Spinner với ListView trong Android (P3)

Leave a Comment
- 4 – class SpinnerAndListViewActivity – xử lý nghiệp vụ trong Activity:
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
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.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

public class SpinnerAndListViewActivity extends Activity {
Spinner spinDm;
 EditText editma,editten;
 Button btnNhap;
 ListView lvSp;
 //cặp đối tượng dùng cho Spinner
 ArrayList<Catalog> arraySpinner=new ArrayList<Catalog>();
 ArrayAdapter<Catalog>adapterSpinner=null;
 //Cặp đối tượng dùng cho ListView
 ArrayList<Product>arrayListview=new ArrayList<Product>();
 ArrayAdapter<Product>adapterListview=null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 getWidgetsControl();
 fakeDataCatalog();
 addEventsForFormWidgets();
 }
 /**
 * Hàm lấy các control theo Id
 */
 private void getWidgetsControl()
 {
 spinDm=(Spinner) findViewById(R.id.spDanhmuc);
 editma=(EditText) findViewById(R.id.editId);
 editten=(EditText) findViewById(R.id.editName);
 btnNhap=(Button) findViewById(R.id.btnInput);
 lvSp=(ListView) findViewById(R.id.lvsanpham);
 //Cấu hình cho Spinner
 adapterSpinner=new ArrayAdapter<Catalog>(this,
 android.R.layout.simple_spinner_item,
 arraySpinner);
 adapterSpinner.setDropDownViewResource
 (android.R.layout.simple_spinner_dropdown_item);
 spinDm.setAdapter(adapterSpinner);
 //Cấu hình cho ListView
 adapterListview=new ArrayAdapter<Product>(this,
 android.R.layout.simple_list_item_1,
 arrayListview);
 lvSp.setAdapter(adapterListview);
 }
 /***
 * Hàm giả dữ liệu, tạo 3 danh mục mặc định cho Spinner
 */
 private void fakeDataCatalog()
 {
 Catalog cat1=new Catalog("1", "SamSung");
 Catalog cat2=new Catalog("2", "Iphone");
 Catalog cat3=new Catalog("3", "IPad");
 arraySpinner.add(cat1);
 arraySpinner.add(cat2);
 arraySpinner.add(cat3);
 adapterSpinner.notifyDataSetChanged();
 }
 /**
 * Hàm gán sự kiện cho Button và Spinner
 */
 private void addEventsForFormWidgets()
 {
 btnNhap.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View arg0) {
 addProductForCatalog();
 }
 });
 spinDm.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
 public void onItemSelected(AdapterView<?> arg0, View arg1,
 int arg2, long arg3) {
 //mỗi lần chọn danh mục trong Spinner thì cập nhập ListView
 loadListProductByCatalog(arraySpinner.get(arg2));
 }
@Override
 public void onNothingSelected(AdapterView<?> arg0) {
 // TODO Auto-generated method stub
 }
 });
 }
 /**
 * Hàm thêm một sản phẩm vào cho danh mục được chọn trong Spinner
 */
 private void addProductForCatalog()
 {
 Product p=new Product();
 p.setid(editma.getText()+"");
 p.setName(editten.getText()+"");
 Catalog c= (Catalog) spinDm.getSelectedItem();
 c.addProduct(p);
 //Mỗi lần thêm xong thì cập nhập lại ListView
 loadListProductByCatalog(c);
 }
 /**
 * Lọc danh sách sản phẩm theo danh mục và update lại ListView
 * @param c
 */
 private void loadListProductByCatalog(Catalog c)
 {
 //xóa danh sách cũ
 arrayListview.clear();
 //lấy danh sách mới từ Catalog chọn trong Spinner
 arrayListview.addAll(c.getListProduct());
 //cập nhật lại ListView
 adapterListview.notifyDataSetChanged();
 }
Bạn có thể tải code mẫu đầy đủ ở đây: http://www.mediafire.com/?i4lev5ek8944kop

0 nhận xét:

Đăng nhận xét