SoFunction
Updated on 2025-03-01

Android's custom implementation of BaseAdapter (general adapter 2)

In the previous article, General Adapter One, we have extracted ViewHolder as a general holder class, which greatly reduces our writing of the code. Now we start extracting it on that basis to achieve better results. First review the code in the class in the previous article, and then we extract the encapsulation through this code again.

public class MyAdapter extends MyBaseAdapter {
 public MyAdapter(List<Student> data) {
  super(data);
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  //Get the holder hold object  ViewHolder holder = (convertView,parent,position, .list_item);
  //Get all controls in the holder object  TextView tvName = (.mTv1);
  //Operate the control and implement the corresponding method  TextView tvSex = (.mTv2);
  ((position).getName());
  ((position).getSex());
  //Return to the corresponding layout  return ();
 }
}

From the above, we can observe again, which codes are not changed in format, or are they all repetitive in form? The answer is, of course there is, if you write this MyAdapter, you will find that in getView, you will get the holder object first.ViewHolder holder = (convertView,parent,position, .list_item),Then, except for the different functions of the control operation each time you get the control (such as text setting text, picture control setting pictures, etc.), the final return statementreturn holdergetConvertView()Is it the same? So we can extract the same form of code in one go. According to object-oriented knowledge, we can extract the shared functions into the parent class, and then hand over the specific details needed to implement to the subclass to implement. That is, the template method design pattern is adopted. Let's start extracting the code into its parent class.

public abstract class MyBaseAdapter extends BaseAdapter {
 protected List<Student> data;
 public MyBaseAdapter (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;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // Extract the same implementation to the parent class, here is fixed  ViewHolder holder = (convertView,parent,position, .list_item);
  //Then provide a specific implementation method for the subclass  setData(holder,(position));
  //The same return code here is also fixed  return ();
 }
 //Set this method as abstract, so that its subclass must be implemented public abstract void setData(ViewHolder holder,T t);
}

Finally, let’s take a look at our code:

public class MyAdapter extends MyBaseAdapter {
 public MyAdapter(List<Student> data) {
  super(data);
 }
 @Override
 public void setData(ViewHolder holder, Student t) {
  TextView tvName = (.mTv1);
  (());
  TextView tvSex = (.mTv2);
  (());
 }
}

You will find that in the end, the MyAdapter you customized only needs to implement this code. Each time, you only need to put the already perfected parent class and the general Viewholder class into your corresponding package. In the future, MyAdapter without customization only needs to write this code. I believe that for beginners, I am already a little excited to improve the code. Finally, we can also improve the code, which is to optimize this code into a universal adapter. So what does this mean? Sometimes you will find that you write code in setData here. If there are 20 controls, and then each time you get the control and set the corresponding value, will you find that there are more codes in an instant? So in order to make the code here optimize more, the specific implementation will be completed in the next article.

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.