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

Leave a Comment
ví dụ về quản lý sản phẩm, bạn xem hình:
16_combine_spin_lv_0
- Bạn quan sát hình Tôi chụp ứng dụng, phần trên cùng là Danh mục các sản phẩm được lưu vàoSpinner, Khi bạn chọn vào nó thì sẽ xổ ra danh sách như bên dưới. khi bạn chọn danh mục nào thì nó sẽ load các sản phẩm thuộc danh mục đó.
- Ví dụ bây giờ bạn chọn số 1 là SamSung, nó sẽ load toàn bộ sản phẩm là SamSung vào ListView bên dưới:
16_combine_spin_lv_1- Nếu bạn chọn 2- Iphone thì nó sẽ load toàn bộ sản phẩm là IPhone vào ListView bên dưới:
16_combine_spin_lv_2- Chương trình cung cấp nút “Nhập SP“, khi người sử dụng nhập thông tin cho sản phẩm và nhấn nút này thì chương trình sẽ lưu sản phẩm vào đúng với danh mục được chọn trong Spinnerđồng thời cập nhật vào ListView bên dưới.
- Bạn cần có ArrayList ArrayAdapter cho Spinner
- Và cần có ArrayList ArrayAdapter cho ListView
-> Tức là bạn phải có 2 cặp (4 đối tượng trên)
- Ví dụ này Tôi viết thuần hướng đối tượng, và có hơi phá cách một chút so với quy tắc hướng đối tượng thông thường, đó là trong lớp Sản phẩm Tôi cho phép nó tham chiếu trực tiếp tới đối tượng Danh mục chứa nó. Như vậy thì đứng tại danh mục nào cũng có thể lấy được toàn bộ danh sách sản phẩm của nó, và đứng tại một sản phẩm bất kỳ nào cũng biết được nó thuộc danh mục nào.
- Bạn xem cấu trúc chương trình:
16_combine_spin_lv_3- Ở trên bạn thấy có 3 class: GoodsProductCatalog: Product và Catalog sẽ kế thừa từ Goods,  Goods sẽ có Id và Name. Sản phẩm và danh mục cũng phải có Id và Name nên nó kế thừa từ Goods là đều hợp lý.
- Bạn xem mô hình lớp:
16_combine_spin_lv_4-Bạn xem cấu trúc XML cho phần giao diện (activity_main.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
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/LinearLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >
<TextView
 android:id="@+id/textView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#008000"
 android:gravity="center"
 android:text="Quản lý sản phẩm"
 android:textColor="#FFFFFF"
 android:textSize="20sp" />
<TableLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:stretchColumns="*"
 >
<TableRow
 android:id="@+id/tableRow1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Danh mục:" />
<Spinner
 android:id="@+id/spDanhmuc"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</TableRow>
<TableRow
 android:id="@+id/tableRow2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Mã Sp:" />
<EditText
 android:id="@+id/editId"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:ems="10" >
<requestFocus />
 </EditText>
</TableRow>
<TableRow
 android:id="@+id/tableRow3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Tên Sp:" />
<EditText
 android:id="@+id/editName"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:ems="10" />
</TableRow>
<TableRow
 android:id="@+id/tableRow4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
<Button
 android:id="@+id/btnInput"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_column="1"
 android:text="Nhập SP" />
</TableRow>
 </TableLayout>
<TextView
 android:id="@+id/textView5"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#008000"
 android:gravity="center"
 android:text="Danh sách sản phẩm theo danh mục"
 android:textColor="#FFFFFF"
 android:textSize="15sp" />
<ListView
 android:id="@+id/lvsanpham"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
 </ListView>
</LinearLayout>
Xem tiếp P2...

0 nhận xét:

Đăng nhận xét