SoFunction
Updated on 2025-03-11

Android custom implementation of BaseAdapter optimization layout

In the previous article, we introduced the common implementation layout of custom implementation BaseAdapter. However, the previous chapter also said that the ordinary implementation method will be very efficient and will be very expensive to the system. Therefore, such implementation is to let beginners know that it can be used in this way. It is impossible to use that method in actual projects. If you use the ordinary layout method when doing the project, I can guarantee that, but your boss will give you a plane ticket during the trial period. Okay, let’s talk less. This time I will explain the implementation of optimized layout. After reading the code, you will think that it is actually very simple.

public class MainActivity extends AppCompatActivity {
  private List<Student> data;
  private ListView mList;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    mList = (ListView)findViewById();
    data = new ArrayList<>();
    Student stu = null;
    for(int i = 0; i < 5; i ++){
      stu = new Student();
      ("Name"+ i);
      (i % 2 == 0 ? "male" : "female");
      (stu);
    }
    MyAdapter adapter = new MyAdapter(data);
    (adapter);
  }
}

public class MyAdapter extends BaseAdapter {

  private List<Student> data;

  public MyAdapter(List<Student> data) {
     = data;
  }

  @Override
  public int getCount() {
    return data == null ? 0 : ();
  }

  @Override
  public Object getItem(int position) {
    return (position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  /**
   *
   * @param position
   * @param convertView
   * @param parent
   * @return
   */
   @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder = null;
    if(convertView == null){
      //Analysis of layout      convertView = (()).inflate(.list_item,null);
      //Create ViewHolder Holding Class      holder = new ViewHolder();
      //Save the object of each control to the holding class       = (TextView)(.mTv1);
       = (TextView)(.mTv2);
      //Set this hold class object in each convertView object      (holder);
    }
    //Every time you need to use this holder class, you will get it    holder = (ViewHolder)();
    //Then you can directly use the controls in this class to operate on the controls without repeatedly searching the findViewById    ((position).getName());
    ((position).getSex());
    return convertView;
  }

  /**
    * Save all current control ids through this class
    */
  static class ViewHolder{
    TextView tvName;
    TextView tvSex;
  }
}

The above code is very simple to implement, just use a ViewHolder holder class to save the control ID in each layout. As we said in the second implementation method of ordinary BaseAdapter, although the number of parsings is minimized, we need to findViewById every time, and the optimization here happens to be optimized for that method. In this way, the number of parsings has reached the minimum, and the number of findViewById has also reached the minimum number of parsings. At least, but for adapters, there is no optimal, only better. That is to say, if we use this method to implement it in the future when we write ListView layout, it is very convenient and efficient. We have to customize BaseAdapter for each ListView to process different data. If one or two are fine, if there are 20 ListViews that process different data in your project, then do we need to write 20 custom BaseAdapters? Therefore, for the sake of development, we can create a general BaseAdapter. The following study notes will be introduced.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.