About the use of Baseadapter in the ListView

Source: Internet
Author: User

Use Baseadapter first to rewrite the four methods inside:

public int GetCount ()

The number of items in your item, which is the column count of the ListView you need to show. Typically returns a container's size () directly

Public Object getItem (int position)

returns the data for each item

public long getitemid (int position)

returns the ID of each item, typically returned directly to position

Public View GetView (int position, view Convertview, ViewGroup parent)

The most important is: redraw the view, the number of calls is determined by the first method. Finally, the view is returned.

The parameter inside the position is the current item's Id,convertview is the layout of the current item.

Note: Some controls must be set to no profit focus with a click.

Cache processing:

If the ListView is too long when you need to reload the control each time it is wasted memory, you need to do cache processing, usually by writing an inner class Viewholder used to store the already loaded controls.

public class personadapter extends baseadapter {    private  list<person> persons;    private context context;     public personadapter (List<person> persons, context context)  {         super ();        this.persons  = persons;        this.context = context;     }     @Override     public int getcount ()  {         return persons.size ();    }      @Override     public object getitem (int position)  {         return persons.get (position);     }     @Override    public long getitemid (int position)  {         return position;    }     @Override     public view getview (int position, view convertview,  Viewgroup parent)  {        ViewHolder holder;         if  (convertview == null)  {             holder = new viewholder ();             /**              *  Get layouts               */            convertview =  view.inflate (context, r.laYout.avtivity_item, null);             // Find Controls             holder.name =  by holder ( TextView)  convertview.findviewbyid (r.id.item_textview_name);             holder.age =  (TextView)  convertview.findviewbyid (r.id.item_ Textview_age);             holder.button =   (Button)  convertview.findviewbyid (R.id.item_button);             //setting tag             Convertview.settag (Holder);        } else {             holder =  (Viewholder)   Convertview.gettag ();         }        //Setting spatial Properties          holder.name.settext (Persons.get (position). GetName ());         holder.age.settext ("" +persons.get (position). Getage ());         return convertview;    }    class viewholder {         public TextView name;         public textview age;        public button  button;    }}


About the use of Baseadapter in the ListView

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.