This case study from the _ Yang, the great God, I wrote it again, deepen the memory http://blog.csdn.net/lmj623565791/article/details/38902805
We used to write the ListView like this.
1 PackageCom.example.listviewdemo;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importandroid.app.Activity;7 ImportAndroid.os.Bundle;8 ImportAndroid.widget.ListView;9 Ten Public classMainactivityextendsActivity { One A PrivateListView LV; - Privatelist<string> list =NewArraylist<string>(); - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + InitData (); -LV =(ListView) Findviewbyid (R.ID.ID_MAIN_LV); +myadapter<string> adapter =NewMyadapter<string> (list, This); A Lv.setadapter (adapter); at } - - Private voidInitData () { - for(inti=0;i<30;i++){ -List.add ("QQQQQ" +i); - } in } - to +}
1 <relativelayout xmlns:android= "Http://schemas.android.com/apk/res /android " xmlns:tools=" Http://schemas.android.com/tools " 3 android:layout_width= "match_parent" 4 android:layout_height=" match _parent " 5 > 6 7 <listview 8 Android:id = "@+id/id_main_lv" android:layout_width=" wrap_content "10 android:layout_height= "wrap_content" 12
1 PackageCom.example.listviewdemo;2 3 Importjava.util.List;4 5 ImportAndroid.content.Context;6 ImportAndroid.view.View;7 ImportAndroid.view.ViewGroup;8 ImportAndroid.widget.BaseAdapter;9 ImportAndroid.widget.TextView;Ten One Public classMyadapter<t>extendsBaseadapter { A - PrivateList<t>list; - PrivateContext Mcontext; the PublicMyadapter (list<t>List,context Mcontext) { - Super(); - This. List =list; - This. Mcontext =Mcontext; + } - + A @Override at Public intGetCount () { - //TODO auto-generated Method Stub - returnlist.size (); - } - - @Override in PublicObject GetItem (intposition) { - //TODO auto-generated Method Stub to returnList.get (position); + } - the @Override * Public LongGetitemid (intposition) { $ //TODO auto-generated Method StubPanax Notoginseng returnposition; - } the + @Override A PublicView GetView (intposition, View Convertview, ViewGroup parent) { the +Viewholder holder =NULL; - if(Convertview = =NULL){ $Holder =NewViewholder (); $Convertview = View.inflate (Mcontext, R.layout.item_listview,NULL); -Holder.tv =(TextView) Convertview.findviewbyid (R.ID.ITEM_TV); - Convertview.settag (holder); the}Else{ -Holder =(Viewholder) Convertview.gettag ();Wuyi } the Holder.tv.setText (String) list.get (position)); - System.out.println (String) list.get (position)); Wu returnConvertview; - } About $ classviewholder{ - TextView TV; - } -}
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:layout_gravity= "Center"6android:orientation= "Vertical" >7 8<TextView9Android:id= "@+id/item_tv"TenAndroid:textsize= "15SP" OneAndroid:layout_width= "Wrap_content" Aandroid:layout_height= "Wrap_content"/> - -</LinearLayout>
Make a ListView high efficiency universal Adapter