Three key points
- XML Layout settings
- Data and simple adapters
- Custom Adapters
XML layout
- Main activity Layout
- XML layout for ListView entries
The main activity layout, only need to add a ListView control, especially attention to the individual controls Layout_width and Layout_height settings
1<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"2xmlns:tools="Http://schemas.android.com/tools"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6 7<LinearLayout8Android:layout_width="fill_parent"9android:layout_height="wrap_content"Tenandroid:orientation="Horizontal"> One<TextView Aandroid:text="@string/name" -Android:layout_width="1DP" -android:layout_height="wrap_content" theandroid:layout_weight="3"/> -<TextView -android:text="@string/phone" -Android:layout_width="1DP" +android:layout_height="wrap_content" -android:layout_weight="5"/> +<TextView Aandroid:text="@string/account" atAndroid:layout_width="1DP" -android:layout_height="wrap_content" -android:layout_weight="2"/> -</LinearLayout> - -<ListView inAndroid:id="@+id/listview" -Android:layout_width="fill_parent" toandroid:layout_height="fill_parent"> + -</ListView> the *</LinearLayout>
Entry XML layout
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"Android:layout_height="wrap_content"android:orientation="Horizontal"> <TextView Android:id="@+id/name"Android:layout_width="1DP"Android:layout_height="wrap_content"Android:layout_weight="3"/> <TextView Android:id="@+id/phone"Android:layout_width="1DP"Android:layout_height="wrap_content"Android:layout_weight="5"/> <TextView Android:id="@+id/account"Android:layout_width="1DP"Android:layout_height="wrap_content"Android:layout_weight="2"/> </LinearLayout>
Data and simple data adapters
- The data form is the cursor result set form and the corresponding Simplecursoradapter
- The data form is list<?extendsmap< String,? > > and the corresponding simpleadapter
Cursor and Simplecursoradapter
Private voidShowlistview (Uri uri) {contentresolver resolver= Mainactivity. This. Getcontentresolver (); Cursor= Resolver.query (URI,NULL,NULL,NULL, "_id desc limit 0,20"); //bind the adapter to the ListViewSimplecursoradapter adapter =NewSimplecursoradapter ( This, R.layout.item, cursor,Newstring[]{"name", "Phone", "account"}, New int[]{r.id.name, R.id.phone, r.id.account}, 1); Listview.setadapter (adapter); }list<?extendsmap< String,? > > and Simpleadapter
Private voidOthershowlistview (Uri uri) {contentresolver resolver= Mainactivity. This. Getcontentresolver (); Cursor= Resolver.query (URI,NULL,NULL,NULL, "_id desc limit 0,50"); //data stored as list<?extendsmap< String,? > >list< hashmap<string,object> > data =Newarraylist(); while(Cursor.movetonext ()) {HashMap<string, object> item =NewHashmap<string, object>(); Item.put ("Name", Cursor.getstring (Cursor.getcolumnindex ("name"))); Item.put ("Phone", Cursor.getstring (Cursor.getcolumnindex ("Phone"))); Item.put ("Account", Cursor.getstring (Cursor.getcolumnindex (the "account"))); Data.add (item); } Simpleadapter Adapter=NewSimpleadapter ( This, data, R.layout.item,Newstring[]{"name", "Phone", "account"}, New int[]{r.id.name, R.id.phone, r.id.account}]; Listview.setadapter (adapter); }
Custom Adapters
- The process of creating a ListView view
- First call the adapter's GetCount to get a total of n entries
- Then the page can hold Perpage entries based on the height of each entry
- Then create a Perpage view control for this perpage item by calling the GetView object
- A ListView is a cache feature that creates only perpage view, and when moved up and down, the new view uses a view control that is temporarily invisible (changing the data)
1 Private voidshowListView3 () {2 Finallist<person> dataList = pservice.getscrolldate1 (0, 30);3Listview.setadapter (NewBaseadapter () {4 @Override5 PublicView GetView (intposition, View Convertview, ViewGroup parent) {6 Viewcache VCache;7 //If the view control is empty, initialize the8 if(Convertview = =NULL){9Convertview = mainactivity. This. Getlayoutinflater (). Inflate (R.layout.item,NULL);TenVCache =NewViewcache (); One /** A * Each findview degrades performance, so you want to perform the find operation only the first time the View object is created - */ -Vcache.name =(TextView) Convertview.findviewbyid (r.id.name); theVcache.phone =(TextView) Convertview.findviewbyid (r.id.phone); -Vcache.account =(TextView) Convertview.findviewbyid (r.id.account); - Convertview.settag (vCache); - } +VCache =(Viewcache) Convertview.gettag (); - VCache.name.setText (datalist.get (position). GetName ()); + VCache.phone.setText (datalist.get (position). Getphone ()); AVCache.account.setText (Datalist.get (position). Getaccount () + ""); at - returnConvertview; - } - @Override - Public LongGetitemid (intposition) { - returnposition; in } - @Override to PublicObject GetItem (intposition) { + returnDatalist.get (position); - } the * @Override $ Public intGetCount () {Panax Notoginseng returndatalist.size (); - } the }); +}
Familiar with ANDROIDAPI series 15--listview