trong bài này mình sẽ hướng dẫn các bạn customize một view thuộc nhóm list như: listview và expandablelistview bằng cách sử dụng các arrayadapter và dùng cursor.
listview
listview using arrayadapter
giả sử bây giờ listview của bạn sẽ dùng để hiển thị danh sách các người bạn và trạng thái của họ.nên mỗi item của listview lúc này sẽ phải hiển thị nhiều hơn 1 thông tin. nên việc sử dụng "android.r.layout.simple_list_item_1" chắc là không đc rồi. vì "android.r.layout.simple_list_item_1" sẽ chỉ cho hiện 1 thông tin trên một dòng text. ở hướng dẫn sau đây tôi sẽ tùy chỉnh để mỗi item của listview sẽ hiện lên 3 thông tin (tên - string, trạng thái - string, và accept - boolean) các đối tượng của lớp friend.
- lớp friend được mô tả như sau:
Mã nguồn PHP:
[color=#000000]
package com[/color][color=#007700].[/color][color=#0000bb]namnd[/color][color=#007700].[/color][color=#0000bb]gingerbread[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]serializable[/color][color=#007700]; public class [/color][color=#0000bb]friend [/color][color=#007700]implements [/color][color=#0000bb]serializable[/color][color=#007700]{ private [/color][color=#0000bb]string nickname[/color][color=#007700]; private [/color][color=#0000bb]string status[/color][color=#007700]; private [/color][color=#0000bb]boolean checked[/color][color=#007700]; public [/color][color=#0000bb]friend[/color][color=#007700]([/color][color=#0000bb]string nickname[/color][color=#007700]){ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]nickname [/color][color=#007700]= [/color][color=#0000bb]nickname[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]setchecked[/color][color=#007700]([/color][color=#0000bb]false[/color][color=#007700]); } public [/color][color=#0000bb]string getnickname[/color][color=#007700](){ return [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]nickname[/color][color=#007700]; } public [/color][color=#0000bb]void setstatus[/color][color=#007700]([/color][color=#0000bb]string status[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]status [/color][color=#007700]= [/color][color=#0000bb]status[/color][color=#007700]; } public [/color][color=#0000bb]string getstatus[/color][color=#007700]() { return [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]status[/color][color=#007700]; } public [/color][color=#0000bb]void setchecked[/color][color=#007700]([/color][color=#0000bb]boolean checked[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]checked [/color][color=#007700]= [/color][color=#0000bb]checked[/color][color=#007700]; } public [/color][color=#0000bb]boolean ischecked[/color][color=#007700]() { return [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]checked[/color][color=#007700]; } @[/color][color=#0000bb]override [/color][color=#007700]public [/color][color=#0000bb]boolean equals[/color][color=#007700]([/color][color=#0000bb]object o[/color][color=#007700]) { [/color][color=#ff8000]// todo auto-generated method stub [/color][color=#007700]if([/color][color=#0000bb]o [/color][color=#007700]instanceof [/color][color=#0000bb]friend[/color][color=#007700]) { [/color][color=#0000bb]friend f [/color][color=#007700]= ([/color][color=#0000bb]friend[/color][color=#007700])[/color][color=#0000bb]o[/color][color=#007700]; return [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]nickname[/color][color=#007700].[/color][color=#0000bb]equalsignorecase[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]getnickname[/color][color=#007700]()); } return [/color][color=#0000bb]false[/color][color=#007700]; } @[/color][color=#0000bb]override [/color][color=#007700]public [/color][color=#0000bb]int hashcode[/color][color=#007700]() { [/color][color=#ff8000]// todo auto-generated method stub [/color][color=#007700]return [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]nickname[/color][color=#007700].[/color][color=#0000bb]hashcode[/color][color=#007700](); }}[/color]
- sửa file main.xml như sau:
Mã nguồn PHP:
[color=#000000]
[/color][color=#007700]<?[/color][color=#0000bb]xml version[/color][color=#007700]=[/color][color=#dd0000]"1.0" [/color][color=#0000bb]encoding[/color][color=#007700]=[/color][color=#dd0000]"utf-8"[/color][color=#0000bb]?>[/color]<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <listview android:id="@+id/listfriend" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /></linearlayout>
- trong res/layout ta thêm 1 file tên là listview.xml với nội dung sau:
Mã nguồn PHP:
[color=#000000]
[/color][color=#007700]<?[/color][color=#0000bb]xml version[/color][color=#007700]=[/color][color=#dd0000]"1.0" [/color][color=#0000bb]encoding[/color][color=#007700]=[/color][color=#dd0000]"utf-8"[/color][color=#0000bb]?>[/color]<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <checkbox android:id="@+id/_check_nick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:paddingtop="20px" android:paddingright="10px" /> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation ="vertical" > <textview android:id="@+id/_nickname" android:textsize = "15px" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="1" android:textstyle="bold" android:textcolor="#ff0099" /> <textview android:id="@+id/_status" android:textsize="13px" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="1" android:textcolor="#99ff00" /> </linearlayout></linearlayout>
- file listview.xml sẽ tạo ra giao diện cách bố trí cho 1 item của listview
thêm lớp customviewfriend trong package com.namnd.gingerbread ở trên. lớp này dùng để load giao diện trong file listview.xml nội dung như sau:
Mã nguồn PHP:
[color=#000000]
package com[/color][color=#007700].[/color][color=#0000bb]namnd[/color][color=#007700].[/color][color=#0000bb]gingerbread[/color][color=#007700]; [/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]content[/color][color=#007700].[/color][color=#0000bb]context[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]view[/color][color=#007700].[/color][color=#0000bb]layoutinflater[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]widget[/color][color=#007700].[/color][color=#0000bb]checkbox[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]widget[/color][color=#007700].[/color][color=#0000bb]linearlayout[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]widget[/color][color=#007700].[/color][color=#0000bb]textview[/color][color=#007700]; public class [/color][color=#0000bb]customviewfriend [/color][color=#007700]extends [/color][color=#0000bb]linearlayout [/color][color=#007700]{ [/color][color=#0000bb]checkbox checkbox[/color][color=#007700]; [/color][color=#0000bb]textview tvnickname[/color][color=#007700]; [/color][color=#0000bb]textview tvstatus[/color][color=#007700]; [/color][color=#0000bb]context context[/color][color=#007700]; public [/color][color=#0000bb]customviewfriend[/color][color=#007700]([/color][color=#0000bb]context context[/color][color=#007700]) { [/color][color=#0000bb]super[/color][color=#007700]([/color][color=#0000bb]context[/color][color=#007700]); [/color][color=#ff8000]// todo auto-generated constructor stub // su dung layoutinflater de gan giao dien trong listview.xml [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]context [/color][color=#007700]= [/color][color=#0000bb]context[/color][color=#007700]; [/color][color=#0000bb]layoutinflater li [/color][color=#007700]= ([/color][color=#0000bb]layoutinflater[/color][color=#007700])[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getcontext[/color][color=#007700]() .[/color][color=#0000bb]getsystemservice[/color][color=#007700]([/color][color=#0000bb]context[/color][color=#007700].[/color][color=#0000bb]layout_inflater_service[/color][color=#007700]); [/color][color=#0000bb]li[/color][color=#007700].[/color][color=#0000bb]inflate[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]layout[/color][color=#007700].[/color][color=#0000bb]listview[/color][color=#007700], [/color][color=#0000bb]this[/color][color=#007700],[/color][color=#0000bb]true[/color][color=#007700]); [/color][color=#0000bb]checkbox [/color][color=#007700]= ([/color][color=#0000bb]checkbox[/color][color=#007700])[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]_check_nick[/color][color=#007700]); [/color][color=#0000bb]tvnickname[/color][color=#007700]= ([/color][color=#0000bb]textview[/color][color=#007700])[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]_nickname[/color][color=#007700]); [/color][color=#0000bb]tvstatus [/color][color=#007700]= ([/color][color=#0000bb]textview[/color][color=#007700])[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]_status[/color][color=#007700]); }}[/color]
- customviewfriend của chúng ta ở đây khá đơn giản, đó là 1 linearlayout chứa 2 thành phần: 1 checkbox và 1 linearlayout khác gồm 2 textview để hiển thị nickname và status. sau đó ta thêm 1 lớp có tên listfriendadapter cũng trong package trên, lớp này kế thừa từ lớp arrayadapter. listfriendadapter sẽ được sử dụng thay thế cho arrayadapter để bind với listview. thông thường arrayadapter chỉ cho hiển thị string bằng textview, nhưng với việc kế thừa và override phương thức getview, ta có thể định nghĩa lại hiển thị cho các thành phần của listview.
Mã nguồn PHP:
[color=#000000]
package com[/color][color=#007700].[/color][color=#0000bb]namnd[/color][color=#007700].[/color][color=#0000bb]gingerbread[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]arraylist[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]content[/color][color=#007700].[/color][color=#0000bb]context[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]view[/color][color=#007700].[/color][color=#0000bb]view[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]view[/color][color=#007700].[/color][color=#0000bb]viewgroup[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]widget[/color][color=#007700].[/color][color=#0000bb]arrayadapter[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]widget[/color][color=#007700].[/color][color=#0000bb]checkbox[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]widget[/color][color=#007700].[/color][color=#0000bb]textview[/color][color=#007700]; public class [/color][color=#0000bb]listfriendadapter [/color][color=#007700]extends [/color][color=#0000bb]arrayadapter[/color][color=#007700]<[/color][color=#0000bb]friend[/color][color=#007700]>{ [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]friend[/color][color=#007700]> array; [/color][color=#0000bb]int resource[/color][color=#007700]; [/color][color=#0000bb]checkbox check[/color][color=#007700]; [/color][color=#0000bb]textview tvnickname[/color][color=#007700]; [/color][color=#0000bb]textview tvstatus[/color][color=#007700]; [/color][color=#0000bb]context context[/color][color=#007700]; [/color][color=#0000bb]friend friend[/color][color=#007700]; public [/color][color=#0000bb]listfriendadapter[/color][color=#007700]([/color][color=#0000bb]context context[/color][color=#007700], [/color][color=#0000bb]int textviewresourceid[/color][color=#007700],[/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]friend[/color][color=#007700]> array) { [/color][color=#0000bb]super[/color][color=#007700]([/color][color=#0000bb]context[/color][color=#007700], [/color][color=#0000bb]textviewresourceid[/color][color=#007700],array); [/color][color=#ff8000]// todo auto-generated constructor stub [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]context [/color][color=#007700]= [/color][color=#0000bb]context[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]resource [/color][color=#007700]= [/color][color=#0000bb]textviewresourceid[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].array = array; } [/color][color=#ff8000]//phương thức xác định view mà adapter hiển thị, ở đây chính là customviewfriend [/color][color=#007700]public [/color][color=#0000bb]view getview[/color][color=#007700]([/color][color=#0000bb]int position[/color][color=#007700], [/color][color=#0000bb]view convertview[/color][color=#007700], [/color][color=#0000bb]viewgroup parent[/color][color=#007700]){ [/color][color=#0000bb]view friendview [/color][color=#007700]= [/color][color=#0000bb]convertview[/color][color=#007700]; if([/color][color=#0000bb]friendview [/color][color=#007700]==[/color][color=#0000bb]null [/color][color=#007700]){ [/color][color=#0000bb]friendview [/color][color=#007700]= new [/color][color=#0000bb]customviewfriend[/color][color=#007700]([/color][color=#0000bb]getcontext[/color][color=#007700]()); } [/color][color=#0000bb]friend [/color][color=#007700]= array.[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]position[/color][color=#007700]); if([/color][color=#0000bb]friend [/color][color=#007700]!=[/color][color=#0000bb]null[/color][color=#007700]){ [/color][color=#0000bb]tvnickname [/color][color=#007700]= (([/color][color=#0000bb]customviewfriend[/color][color=#007700])[/color][color=#0000bb]friendview[/color][color=#007700]).[/color][color=#0000bb]tvnickname[/color][color=#007700]; [/color][color=#0000bb]tvstatus [/color][color=#007700]= (([/color][color=#0000bb]customviewfriend[/color][color=#007700])[/color][color=#0000bb]friendview[/color][color=#007700]).[/color][color=#0000bb]tvstatus[/color][color=#007700]; [/color][color=#0000bb]check [/color][color=#007700]=(([/color][color=#0000bb]customviewfriend[/color][color=#007700])[/color][color=#0000bb]friendview[/color][color=#007700]).[/color][color=#0000bb]checkbox[/color][color=#007700]; [/color][color=#ff8000]// lay doi tuong friend va dua ra ui [/color][color=#0000bb]tvnickname[/color][color=#007700].[/color][color=#0000bb]settext[/color][color=#007700]([/color][color=#0000bb]friend[/color][color=#007700].[/color][color=#0000bb]getnickname[/color][color=#007700]()); [/color][color=#0000bb]tvstatus[/color][color=#007700].[/color][color=#0000bb]settext[/color][color=#007700]([/color][color=#0000bb]friend[/color][color=#007700].[/color][color=#0000bb]getstatus[/color][color=#007700]()); [/color][color=#0000bb]check[/color][color=#007700].[/color][color=#0000bb]setchecked[/color][color=#007700]([/color][color=#0000bb]friend[/color][color=#007700].[/color][color=#0000bb]ischecked[/color][color=#007700]()); } return [/color][color=#0000bb]friendview[/color][color=#007700]; } }[/color]
và việc cuối cùng cần làm là viết lại activity. tới listdemo.java (extends activity) và chỉnh sửa theo nội dung sau:
Mã nguồn PHP:
[color=#000000]
package com[/color][color=#007700].[/color][color=#0000bb]namnd[/color][color=#007700].[/color][color=#0000bb]gingerbread[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]arraylist[/color][color=#007700]; [/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]app[/color][color=#007700].[/color][color=#0000bb]activity[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]app[/color][color=#007700].[/color][color=#0000bb]progressdialog[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]content[/color][color=#007700].[/color][color=#0000bb]sharedpreferences[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]os[/color][color=#007700].[/color][color=#0000bb]bundle[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]view[/color][color=#007700].[/color][color=#0000bb]menu[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]view[/color][color=#007700].[/color][color=#0000bb]menuitem[/color][color=#007700];[/color][color=#0000bb]import android[/color][color=#007700].[/color][color=#0000bb]widget[/color][color=#007700].[/color][color=#0000bb]listview[/color][color=#007700]; public class [/color][color=#0000bb]listdemo [/color][color=#007700]extends [/color][color=#0000bb]activity [/color][color=#007700]{ private static final [/color][color=#0000bb]int logout [/color][color=#007700]= [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]first[/color][color=#007700]; private static final [/color][color=#0000bb]int change [/color][color=#007700]= [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]first[/color][color=#007700]+[/color][color=#0000bb]2[/color][color=#007700]; private static final [/color][color=#0000bb]int [/color][color=#007700]exit = [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]first[/color][color=#007700]+[/color][color=#0000bb]6[/color][color=#007700]; private static final [/color][color=#0000bb]int about [/color][color=#007700]= [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]first[/color][color=#007700]+[/color][color=#0000bb]4[/color][color=#007700]; [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]friend[/color][color=#007700]> array; [/color][color=#0000bb]listfriendadapter arrayadapter[/color][color=#007700]; [/color][color=#0000bb]listview [/color][color=#007700]list; public [/color][color=#0000bb]void oncreate[/color][color=#007700]([/color][color=#0000bb]bundle savedinstancestate[/color][color=#007700]){ [/color][color=#0000bb]super[/color][color=#007700].[/color][color=#0000bb]oncreate[/color][color=#007700]([/color][color=#0000bb]savedinstancestate[/color][color=#007700]); [/color][color=#0000bb]setcontentview[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]layout[/color][color=#007700].[/color][color=#0000bb]main[/color][color=#007700]); list = ([/color][color=#0000bb]listview[/color][color=#007700])[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]listfriend[/color][color=#007700]); array = new [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]friend[/color][color=#007700]>(); [/color][color=#0000bb]arrayadapter [/color][color=#007700]= new [/color][color=#0000bb]listfriendadapter[/color][color=#007700]([/color][color=#0000bb]this[/color][color=#007700], [/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]layout[/color][color=#007700].[/color][color=#0000bb]listview[/color][color=#007700], array); list.[/color][color=#0000bb]setadapter[/color][color=#007700]([/color][color=#0000bb]arrayadapter[/color][color=#007700]); [/color][color=#0000bb]friend f [/color][color=#007700]= new [/color][color=#0000bb]friend[/color][color=#007700]([/color][color=#dd0000]"namd"[/color][color=#007700]); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]setstatus[/color][color=#007700]([/color][color=#dd0000]"có ai thích tắm nước mắm không?"[/color][color=#007700]); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]setchecked[/color][color=#007700]([/color][color=#0000bb]true[/color][color=#007700]); [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]addnick[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]addnick[/color][color=#007700]( [/color][color=#0000bb]f[/color][color=#007700]= new [/color][color=#0000bb]friend[/color][color=#007700]([/color][color=#dd0000]"mungtv"[/color][color=#007700])); [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]addnick[/color][color=#007700](new [/color][color=#0000bb]friend[/color][color=#007700]([/color][color=#dd0000]"tuyenhl"[/color][color=#007700])); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]setstatus[/color][color=#007700]([/color][color=#dd0000]"nước mắm thơm quá đi mất"[/color][color=#007700]); [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]addnick[/color][color=#007700]([/color][color=#0000bb]f [/color][color=#007700]= new [/color][color=#0000bb]friend[/color][color=#007700]([/color][color=#dd0000]"quanglh"[/color][color=#007700])); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]setstatus[/color][color=#007700]([/color][color=#dd0000]"được tắm nước mắm thật sảng khoái quá đi mất! :d"[/color][color=#007700]); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]setchecked[/color][color=#007700]([/color][color=#0000bb]true[/color][color=#007700]); [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]addnick[/color][color=#007700]([/color][color=#0000bb]f [/color][color=#007700]= new [/color][color=#0000bb]friend[/color][color=#007700]([/color][color=#dd0000]"tuanpv"[/color][color=#007700])); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]setstatus[/color][color=#007700]([/color][color=#dd0000]"miễn bàn, tuy thích nhưng chắc không được"[/color][color=#007700]); } [/color][color=#ff8000]// tao option menu [/color][color=#007700]public [/color][color=#0000bb]boolean oncreateoptionsmenu[/color][color=#007700]([/color][color=#0000bb]menu menu[/color][color=#007700]){ [/color][color=#0000bb]super[/color][color=#007700].[/color][color=#0000bb]oncreateoptionsmenu[/color][color=#007700]([/color][color=#0000bb]menu[/color][color=#007700]); [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]logout[/color][color=#007700],[/color][color=#0000bb]0[/color][color=#007700],[/color][color=#dd0000]"logout"[/color][color=#007700]).[/color][color=#0000bb]seticon[/color][color=#007700]([/color][color=#0000bb]android[/color][color=#007700].[/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]drawable[/color][color=#007700].[/color][color=#0000bb]ic_menu_revert[/color][color=#007700]); [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]change[/color][color=#007700],[/color][color=#0000bb]0[/color][color=#007700],[/color][color=#dd0000]"change password"[/color][color=#007700]).[/color][color=#0000bb]seticon[/color][color=#007700]([/color][color=#0000bb]android[/color][color=#007700].[/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]drawable[/color][color=#007700].[/color][color=#0000bb]ic_menu_edit[/color][color=#007700]); [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]about[/color][color=#007700],[/color][color=#0000bb]0[/color][color=#007700],[/color][color=#dd0000]"about"[/color][color=#007700]).[/color][color=#0000bb]seticon[/color][color=#007700]([/color][color=#0000bb]android[/color][color=#007700].[/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]drawable[/color][color=#007700].[/color][color=#0000bb]ic_menu_info_details[/color][color=#007700]); [/color][color=#0000bb]menu[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],exit,[/color][color=#0000bb]0[/color][color=#007700],[/color][color=#dd0000]"exit"[/color][color=#007700]).[/color][color=#0000bb]seticon[/color][color=#007700]([/color][color=#0000bb]android[/color][color=#007700].[/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]drawable[/color][color=#007700].[/color][color=#0000bb]ic_menu_close_clear_cancel[/color][color=#007700]); return [/color][color=#0000bb]true[/color][color=#007700]; } [/color][color=#ff8000]// xu ly cac su kien cho menu [/color][color=#007700]public [/color][color=#0000bb]boolean onoptionsitemselected[/color][color=#007700]([/color][color=#0000bb]menuitem item[/color][color=#007700]){ return [/color][color=#0000bb]true[/color][color=#007700]; } public [/color][color=#0000bb]void addnick[/color][color=#007700]([/color][color=#0000bb]friend friend[/color][color=#007700]){ array.[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]friend[/color][color=#007700]); [/color][color=#0000bb]arrayadapter[/color][color=#007700].[/color][color=#0000bb]notifydatasetchanged[/color][color=#007700](); }}[/color]
các bạn có thể lưu ý là thay vì sử dụng arraylist<string> như trước mình đã thay bằng arraylist<friend> và trong khởi tạo đối tượng arrayadapter thì đối số thứ 2 là r.layout.listview thay vì android.r.layout.simple_list_item_1, nghĩa là chúng ta đã sử dụng layout do mình tự tạo thay vì layout android cung cấp sẵn cho hiển thị các thành phần của listview. kết quả khi chương trình chạy như sau:
Cách sử dụng thước lỗ ban trong xây mộ chuẩn theo phong thủy Cách sử dụng thước lỗ ban trong xây mộ để ngôi mộ của các bậc tổ tiên, những người đã khuất được xây dựng theo kích thước hợp phong thủy...
Hướng dẫn sử dụng thước lỗ ban trong xây mộ