SoFunction
Updated on 2025-04-07

Analysis and understanding of BaseAdapter usage in Android

This article analyzes the usage of BaseAdapter in Android. Share it for your reference, as follows:

Recently I did a project and used ListView in the project. The most important thing about ListView is to bind data. This data is provided by Adapter. Here I rewrite the BaseAdapter class to implement my menuAdapter code as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class menuAdapter extends BaseAdapter {
 private LayoutInflatermInflater;
 private Context context;
  privateList<Menu> menus;
  publicmenuAdapter(List<Menu> menus,Context context)  {
    =menus;
     =(context);
  }
  public int getCount() {
    return ();
  }
  public Object getItem(intposition) {
    return (position);
  }
  public long getItemId(intposition) {
    return position;
  }
  public View getView(intposition, View convertView, ViewGroup parent) {
    menuAdapterViewholder = null;//A custom class is used to cache convertview    if (convertView ==null) {
      holder=newmenuAdapterView();
      convertView =(, null);
       =(TextView)();
       =(TextView)();
       =(TextView)();
      (holder);
      //("tag", "run once");
    }else {
      holder = (menuAdapterView)();
    }
    ((position));
    ((String)(position).getPrice());
    ((String)(position).getDescription());
    return convertView;
  }
}

The method that BaseAdapter needs to be rewrite:

getCount(),getItem(int position),getItemId(int position),getView(int position, View convertView, ViewGroup parent)

Process analysis:

When the listView starts drawing, the system first calls the getCount() function, and according to its return value, the length of the listView is called, and then according to this length, the getView() is called to draw each row one by one. If your getCount() return value is 0, the list will not display the same return 1, and only one row will be displayed. When the system displays the list, first instantiate an adapter (the custom adapter will be instantiated here). When the adaptation is done manually, the data must be mapped manually, which requires the rewrite of the getView() method. This method will be called when drawing each row of the list. getView() has three parameters. The position indicates which line it will be displayed. The coverView is the layout inflate from the layout file. We use LayoutInflater method to extract the defined files into View instances for display. Then instantiate each component in the xml file (simple findViewById() method). This way, the data can be corresponded to each component. However, in order to respond to click events, the button needs to add a click listener to it, so that the click events can be captured. At this point a custom listView is completed, and now let's look back at the process again. The system is going to draw a ListView. It first obtains the length of the list to be drawn, and then starts to draw the first line. How to draw it? Call the getView() function. In this function, first get a View (actually a ViewGroup), then instance and set up each component to display it. OK, I've finished drawing this line. Then draw the next line until it is finished.

For more information about Android related content, please check out the topic of this site:Android control usage summary》、《Android View View Tips Summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial"and"Android resource operation skills summary

I hope this article will be helpful to everyone's Android programming design.