Android ListView Custom Baseadapter

Source: Internet
Author: User

First on

As can be seen from the above: Each item of the ListView in the page is composed of two TextView, when initialized, only show the first TextView, where we become the title, and the content part of the SetVisible property is "GONE". To add the onclick listener for ITE, the Content section is displayed at each click, and the Baseadapter notifydatasetchanged () is called to refresh the display.

Code section:

1 classMylayoutextendsLinearLayout {2 3     PrivateTextView Mtitle;4     PrivateTextView mwords;5 6      PublicMylayout (Context context, string title, string words,7             Booleanmvisible) {8         Super(context);9          This. SetOrientation (VERTICAL);Ten  OneMtitle =NewTextView (context); A Mtitle.settext (title); -AddView (Mtitle,NewLinearlayout.layoutparams ( - layoutparams.match_parent, layoutparams.wrap_content)); the  -Mwords =NewTextView (context); - Mwords.settext (words); -AddView (Mwords,NewLinearlayout.layoutparams ( + layoutparams.match_parent, layoutparams.wrap_content)); -Mwords.setvisibility (mvisible?View.VISIBLE:View.GONE); +     } A  at      Public voidSettitle (String title) { - Mtitle.settext (title); -     } -  -      Public voidsetwords (String words) { - Mwords.settext (words); in     } -  to      Public voidSetwordsvisible (Booleanmvisible) { +Mwords.setvisibility (mvisible?View.VISIBLE:View.GONE); -     } the  *}
Mylayout

* Inherit LinearLayout in which add two TextView, through mvisible? View.VISIBLE:View.GONE Controls whether the content is displayed. Tips: This does not use the Invisible property.

1 classMybaseadapterextendsBaseadapter {2     PrivateString[] titles = {"Title one", "Title II", "title three", "title Four", "title Five" };3     Privatestring[] Words = {"I am content 1, I am content 1", "I am content 2, I am content 2", "I am content 3, I am content 3",4"I am content 4, I am content 4", "I am content 5, I am content 5" };5     Private Boolean[] flags = {false,false,false,false,false };6     PrivateContext Mcontext;7 8      PublicMybaseadapter (Context context) {9Mcontext =context;Ten     } One  A @Override -      Public intGetCount () { -         //TODO auto-generated Method Stub the         returntitles.length; -     } -  - @Override +      PublicObject GetItem (intposition) { -         //TODO auto-generated Method Stub +         returnposition; A     } at  - @Override -      Public LongGetitemid (intposition) { -         //TODO auto-generated Method Stub -         returnposition; -     } in  - @Override to      PublicView GetView (intposition, View Convertview, ViewGroup parent) { +         //TODO auto-generated Method Stub - mylayout mlayout; the         if(Convertview = =NULL) { *Mlayout =Newmylayout (Mcontext, titles[position], words[position], $ flags[position]);Panax Notoginseng}Else{ -Mlayout =(mylayout) Convertview; the Mlayout.settitle (titles[position]); + mlayout.setwords (words[position]); A mlayout.setwordsvisible (flags[position]); the         } +         returnmlayout; -     } $      $      Public voidToggler (intposition) { -Flags[position] =!Flags[position]; - notifydatasetchanged (); the     } -     Wuyi      the      -}
Mybaseadapter

* Override GetView () method to complete initialization

1  Public classBaseadapteractivityextendslistactivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         //TODO auto-generated Method Stub6         Super. OnCreate (savedinstancestate);7          This. Setlistadapter (NewMybaseadapter ( This));8     }9 Ten @Override One     protected voidOnlistitemclick (ListView L, View V,intPositionLongID) { A ( (Mybaseadapter) Getlistadapter ()). Toggler (position); -     } -      the}
mainactivity

Android ListView Custom Baseadapter

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.