1. A brief introduction to the ListView
A ListView is a list of views that are displayed vertically in a project. is an adapter view (adapter view) that cannot be implemented to determine the content in the view. Data and view bindings need to be implemented through an adapter that inherits the Listviewadapter interface. Ensures that the contents of the view are dynamically refreshed when scrolling up or down. Usually we will customize a class that inherits from Baseadapter (inherited Listviewadapter), Arrayadapter (inherited from Baseadapter), Simpleadapter (inherited from Baseadapter), Rewrite the GetView () method to implement the functionality you want.
GetView Method Details
View GetView (int position, view Convertview, ViewGroup parent)
Get a View that displays the data on the specified position in the data set.
You can either create a View manually or inflate it from an XML layout file.
When the view is inflated, the parent view (GridView, ListView ...) would apply
Default layout parameters unless inflate(int, android.view.ViewGroup, boolean)
Specify a root view and to prevent attachment to the root
Note: use the adapter to build the layout .
If the content of the layout is dynamic or not predetermined, you can use a layout that runs at run time through subclasses AdapterViewFills the layout with a view.AdapterViewClass is used by subclasses of the AdapterBinds the data to its layout.
Adapteracts as a data source andAdapterViewThe middleman between layouts--AdapterRetrieving data (from sources such as arrays or database queries) and converting each entry to aAdapterViewThe view in the layout.
Common layouts supported by adapters include:
populating the adapter view with Data
AdapterView You can populate ListView Adapter GridView (for example, or) an instance with a binding, which retrieves data from an external source. AdapterView and create a representation of View each data entry.
Android provides several Adapter sub-classes for retrieving different kinds of data and building AdapterView views. Two of the most common adapters are:arrayadapter and simplecursoradapter.
2.ListView Use Steps
1). In the layout's XML file, add the following code:
<listview android:id= "@+id/list_view" android:layout_width= "match_parent" android:layout_ height= "Match_parent"/>
2) Create a new List_item layout file to determine the style of each view.
3). Create a new adapter class.
4). Bind the data to the view.
3. Usage Examples
1). Use Arrayadapter
Main layout file:
mylistview.xml File <?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "http://schemas.android.com/ Apk/res/android " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <listview android:id= "@+id/mylistview" android:layout_width= "Match_parent" android: layout_height= "Match_parent" > </ListView></LinearLayout>
XML file for List_item:
list_item.xml File <?xml version= "1.0" encoding= "UTF-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:textsize = "18SP" ></TextView>
Activity file:
Myactivity.java File Public classMyActivityextendsactivity{PrivateListView ListView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.mylistview); ListView=(ListView) Findviewbyid (R.id.mylistview); string[] Str= {"Shanghai", "Beijing", "Tianjin", "Jiangsu", "Henan", "Tibet", "Xinjiang", "Hunan", "Hubei"}; List<String> Listdata =NewArraylist<string>(); Listdata.add (Shanghai); Listdata.add (Beijing); Listdata.add (Tianjin); Listdata.add (Jiangsu); Arrayadapter<String> Arrayadapter =NewArrayadapter<string> ( This, R.layout.list_item,listdata);//both Listdata and STR can beListview.setadapter (Arrayadapter); } }
2. Using Simpleadapter
Construction Method:Simpleadapter (context context, list<? extends Map<string,?>> data, int resource, S Tring[] From, int[] to)
Simpleadapter can be hashmap, each view can display several different content.
1). The main layout file is the same as above
2). List_item Layout file
<?xml version= "1.0" encoding= "UTF-8"? ><relativelayout xmlns:android= " Http://schemas.android.com/apk/res/android "android:layout_width=" Match_parent "android:layout_height=" match _parent "> <imageview android:id=" @+id/iv1 "android:layout_width=" 30DP "android:layout_height=" 30DP " android:src= "@drawable/ic_launcher"/><textview android:id= "@+id/tv1" Android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "18sp" android:text= "Wgj" android:layout_torightof= "@id/iv1" ></textview><textview android:id= "@+id/tv2" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "18SP" android:text= "19 years old" Android oid:layout_below= "@id/tv1" android:layout_torightof= "@id/iv1" ></TextView></RelativeLayout>
3.activity file
Public classMyActivityextendsactivity{PrivateListView ListView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.mylistview); ListView=(ListView) Findviewbyid (R.id.mylistview); Simpleadapter Simpleadapter=NewSimpleadapter ( This, PutData (), R.layout.list_item,Newstring[]{"Name", "Age", "pic"},New int[]{r.id.tv1,r.id.tv2,r.id.iv1}]; Listview.setadapter (Simpleadapter); } PublicList<map<string,object>>PutData () {List<Map<String,Object>> list =NewArraylist<map<string,object>>(); Map<String,Object> Map1 =NewHashmap<string,object>(); Map1.put ("Name", "Zhang San"); Map1.put ("Age", "19 years old"); Map1.put ("Pic", R.drawable.friend); Map<String,Object> MAP2 =NewHashmap<string,object>(); Map2.put ("Name", "John Doe"); Map2.put ("Age", "17 years old"); Map2.put ("Pic", R.drawable.ic_launcher); Map<String,Object> MAP3 =NewHashmap<string,object>(); Map3.put ("Name", "Harry"); Map3.put ("Age", "17 years old"); Map3.put ("Pic", R.drawable.ic_launcher); List.add (MAP1); List.add (MAP2); List.add (MAP3); returnlist; } }
3. Using Baseadapter
Optimization method Convertview reusing objects
3.1. Implementation via Cache Convertview
-This method of caching Contentview can be used to determine if a view is created in the cache that does not exist, and if it already exists, it can improve performance by using the view in the cache.
3.2. Through Convertview+viewholder to achieve
-viewholder is a static class, and the key benefit of using Viewholder is to cache the view that displays the data, which speeds up the responsiveness of the UI.
-In code, when Convertview is empty, use the Settag () method to bind a Viewholder object that holds the control for each view. When Convertview is not empty, reusing a view that has already been created uses the Gettag () method to get the bound Viewholder object, which avoids the findviewbyid of a layer query on the control and quickly navigates to the control.
3.3adapter files
public class Myadapter extends baseadapter{private layoutinflater minflater; Private list<map<string,object>> List; Public Myadapter (context context, list<map<string,object>> List) {this.minflater = Layoutinfla Ter.from (context); This.list = list; } @Override public int getcount () {//TODO auto-generated Method stub return List.size (); } @Override public Object getItem (int position) {//TODO auto-generated Method stub return List.get ( position); } @Override public long getitemid (int position) {//TODO auto-generated method stub return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated Method stub Viewholder holder = null; if (Convertview = = null) {holder = new Viewholder (); Convertview = Minflater.inflaTE (R.layout.list_item, null); Holder.name = (TextView) Convertview.findviewbyid (R.ID.TV1); Holder.age = (TextView) Convertview.findviewbyid (R.ID.TV2); Holder.pic = (ImageView) Convertview.findviewbyid (R.ID.IV1); Convertview.settag (holder); }else{holder = (viewholder) convertview.gettag (); } holder.name.setText (String) list.get (position). Get ("name")); Holder.age.setText (String) list.get (position). Get ("age")); Holder.pic.setBackgroundResource ((Integer) list.get (position). Get ("pic")); return convertview; Public final class viewholder{public TextView name; Public TextView age; Public ImageView pic; }}
3.4 Activity File
Public classMyActivityextendsactivity{PrivateListView ListView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.mylistview); ListView=(ListView) Findviewbyid (R.id.mylistview); Myadapter Myadapter=NewMyadapter ( This, PutData ()); Listview.setadapter (Myadapter); } PublicList<map<string,object>>PutData () {List<Map<String,Object>> list =NewArraylist<map<string,object>>(); Map<String,Object> Map1 =NewHashmap<string,object>(); Map1.put ("Name", "Zhang San"); Map1.put ("Age", "19 years old"); Map1.put ("Pic", R.drawable.friend); Map<String,Object> MAP2 =NewHashmap<string,object>(); Map2.put ("Name", "John Doe"); Map2.put ("Age", "17 years old"); Map2.put ("Pic", R.drawable.ic_launcher); Map<String,Object> MAP3 =NewHashmap<string,object>(); Map3.put ("Name", "Harry"); Map3.put ("Age", "17 years old"); Map3.put ("Pic", R.drawable.ic_launcher); List.add (MAP1); List.add (MAP2); List.add (MAP3); returnlist; } }
4. Reference Articles
"1" 51442363
"2" 51088239
Use of the Android ListView