Usage analysis and understanding of Baseadapter in Android _android

Source: Internet
Author: User
Tags sqlite database

This article analyzes the usage of Baseadapter in Android. Share to everyone for your reference, specific as follows:

Recently to do a project, the project used Listview,listview most important is binding data, this data is provided by adapter, here I rewrite the Baseadapter class to implement their own Menuadapter code as follows:

Package org.leepood.lanorder;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;
Import Org.leepood.xmlparse.xmlParse;
Import Android.view.LayoutInflater;
Import Android.content.Context;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView;
 public class Menuadapter extends Baseadapter {private layoutinflaterminflater;
  private context;
  privatelist<menu> menus;
    Publicmenuadapter (list<menu> menus,context context) {This.menus=menus;
  This.minflater =layoutinflater.from (context);
  public int GetCount () {return menus.size ();
  Public Object GetItem (intposition) {return menus.get (position);
  Public long Getitemid (intposition) {return position; Public View GetView (intposition, view Convertview, ViewGroup parent) {Menuadapterviewholder = null;//a custom class used to cache C
 Onvertview if (Convertview ==null) {     Holder=newmenuadapterview ();
      Convertview =minflater.inflate (r.layout.menu, NULL);
      Holder.name = (TextView) Convertview.findviewbyid (r.id.txtname);
      Holder.price = (TextView) Convertview.findviewbyid (R.id.txtprice);
      Holder.description = (TextView) Convertview.findviewbyid (r.id.txtdescription);
      Convertview.settag (holder);
    LOG.I ("tag", "Run Once");
    }else {holder = (Menuadapterview) convertview.gettag ();
    } holder.name.setText (string.valueof (position));
    Holder.price.setText ((String) menus.get (position). GetPrice ());
    Holder.description.setText ((String) menus.get (position). GetDescription ());
  return convertview;

 }
}

Where Baseadapter needs to be rewritten:

GetCount (), getitem (int position), getitemid (int position), getview (int position, View Convertview, ViewGroup parent)

Process Analysis:

ListView at the beginning of the drawing, the system first calls the GetCount () function, based on the length of his return, which is worth the ListView, and then, based on that length, invokes GetView () to draw each row individually. If your GetCount () returns a value of 0, the list will not show the same return 1, showing only one row. When the system displays a list, it first instantiates an adapter (this will instantiate the custom adapter). When you manually complete the adaptation, you must manually map the data, which requires overriding the GetView () method. This method is called when the system draws each row of the list. GetView () has three parameters, position represents the row to be displayed, and Covertview is the layout inflate from the layout file. We use the Layoutinflater method to extract the defined Item.xml file into a view instance for display. The individual components in the XML file are then instantiated (a simple Findviewbyid () method). This allows the data to be mapped to individual components. But in response to the Click event, the button needs to add a click listener for it to capture the click event. Now that a custom ListView is done, let's go back and look at the process. System to draw ListView, he first obtained the length of the list to be drawn, and then began to draw the first line, how to draw it? Call the GetView () function. In this function you first get a view (actually a viewgroup), and then instantiate and set up each component to display it. All right, I've finished drawing this line. Then draw the next line until the painting is finished.

More interested readers of Android-related content can view the site: "Summary of Android Control usage", "Android View Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced course" and " Android Resource Operating Skills summary

I hope this article will help you with the Android program.

Related Article

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.