Listview sets different types of item

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.