To set a different item you need to rewrite two methods in adapter
Getitemviewtype () need to be aware that this type is 0 start
Getviewtypecount ();
1 Packagecom.example.listview_01;2 3 Importjava.util.List;4 5 ImportAndroid.content.Context;6 ImportAndroid.view.LayoutInflater;7 ImportAndroid.view.View;8 ImportAndroid.view.ViewGroup;9 ImportAndroid.widget.BaseAdapter;Ten ImportAndroid.widget.TextView; One A Public classMyadapterextendsBaseadapter { - - Privatecontext context; the PrivateList<viewitem>list; - PrivateLayoutinflater Inflater; - - PublicMyadapter (context context, list<viewitem>list) { + This. Context =context; - This. List =list; +Inflater =Layoutinflater.from (context); A } at - @Override - Public intGetCount () { - returnlist.size (); - } - in @Override - PublicObject GetItem (intposition) { to returnList.get (position); + } - the @Override * Public LongGetitemid (intposition) { $ returnposition;Panax Notoginseng } - the @Override + Public intGetitemviewtype (intposition) { A returnlist.get (position). GetType (); the } + - @Override $ Public intGetviewtypecount () { $ return2; - } - the @Override - PublicView GetView (intposition, View Convertview, ViewGroup parent) {WuyiHolderType1 HolderType1 =NULL; theHolderType2 holderType2 =NULL; - intType =Getitemviewtype (position); Wu if(Convertview = =NULL) { - //set different layouts based on different type About Switch(type) { $ Case0: -Convertview = Inflater.inflate (R.layout.activity_item,NULL); -HolderType1 =NewHolderType1 (convertview); - Convertview.settag (holderType1); A Break; + the Case1: -Convertview = Inflater.inflate (r.layout.activity_item2,NULL); $HolderType2 =NewHolderType2 (convertview); the Convertview.settag (holderType2); the Break; the } the}Else { - //set different holder depending on the layout in Switch(type) { the Case0: theHolderType1 =(HolderType1) Convertview.gettag (); About Break; the Case1: theHolderType2 =(HolderType2) Convertview.gettag (); the Break; + } - } the BayiViewItem item =List.get (position); the //populating a control with data based on a different layout the Switch(type) { - Case0: - HolderType1.addressTxt.setText (Item.getaddress ()); the HolderType1.nameTxt.setText (Item.getname ()); the HolderType1.ageTxt.setText (Item.getage ()); the Break; the Case1: - HolderType2.titleTxt.setText (Item.gettitle ()); the Break; the } the returnConvertview;94 } the the Private classHolderType1 { the PublicTextView nametxt;98 PublicTextView agetxt; About PublicTextView addresstxt; - 101 PublicHolderType1 (View convertview) {102Nametxt =(TextView) Convertview.findviewbyid (r.id.name);103Agetxt =(TextView) Convertview.findviewbyid (r.id.age);104Addresstxt =(TextView) Convertview.findviewbyid (r.id.address); the }106 }107 108 Private classHolderType2 {109 PublicTextView titletxt; the 111 PublicHolderType2 (View convertview) { theTitletxt =(TextView) Convertview.findviewbyid (r.id.title);113 } the } the the}
Listview sets different types of item