SoFunction
Updated on 2025-03-11

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

In the previous article, we said that if there are many controls in setData, we still need to write a lot of code into this method. In order to reduce the convenience of development, we will optimize it again on this basis. The implementation principle is like this: Every time I look up the control in setData, then setXXX() or something, we can put this writing implementation like in ViewHolder, and write a chain method in ViewHolder to help us implement the function (I will not repeat the code for the ViewHodler class, the code here:Android's custom implementation of BaseAdapter (general adapter one)), the chain method is as follows:

public ViewHolder setText(int viewId, String data){
  TextView tv = getView(viewId);
  (data);
  return this;
 }

Through this method, we can set the corresponding content very well. Just pass the id of the control that needs to be set, and then pass the corresponding data to achieve the effect of setting text. Let's take a look at the code in ours

/**
 * MyAdapter in the previous article
 */
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);
  (());
 }
}

/**
 * Optimized MyAdapter
 */
public class MyAdapter extends MyBaseAdapter {
 public MyAdapter(List<Student> data) {
  super(data);
 }
 @Override
 public void setData(ViewHolder holder, Student t) {
  (.mTv1, ()).setText(.mTv2, ());
 }
}

OK, compare the implementation in setData. We can replace the above implementation with only one code. Isn't this more convenient? In this way, our extension will be more convenient. If we say, we not only set text content, but set pictures through image controls? It's very simple. We just need to add the method we want to implement in the ViewHolder, such as setting up the picture, we can add the following code:

public ViewHolder setImageResource(int viewId,int resId){
  ImageView img = getView(viewId);
  (resId);
  return this;
 }
 public ViewHolder setImageBitmap(int viewId, Bitmap bm){
  ImageView img = getView(viewId);
  (bm);
  return this;
 }

After adding these two methods, it is very easy to set the picture. Just pass the corresponding parameters in setData.

@Override
 public void setData(ViewHolder holder, Student t) {
  (.mTv1, ()).setText(.mTv2, ());
  (.img1,resourceid).setImageBitmap(.img2,bm);
 }

OK, a general adapter has been completed, the overall framework and code have been implemented. I will summarize all the code and write it below for everyone to use, namely (the main interface class, responsible for passing parameters and setting the ListView data), (custom adapter), (general adapter class), (general hold class object), and entity class

public class MainActivity extends AppCompatActivity{

 private List<Student> data;
 private ListView mList;
 MyAdapter adapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  getData();
  mList = (ListView) findViewById();
  adapter = new MyAdapter(data);
  (adapter);
 }

 private void getData() {
  data = new ArrayList<>();
  Student stu = null;
  for (int i = 0; i < 20; i++) {
   stu = new Student();
   ("Name" + i);
   (i % 2 == 0 ? "male" : "female");
   (stu);
  }
 }

}

public class MyAdapter extends MyBaseAdapter<Student> {

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

 @Override
 public void setData(ViewHolder holder, Student t) {
  (.mTv1, ()).setText(.mTv2, ());

 }

}

public abstract class MyBaseAdapter<T> extends BaseAdapter {
 protected List<T> data;
 public MyBaseAdapter(List<T> 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) {
  ViewHolder holder = (convertView,parent,position, .list_item);
  setData(holder,(position));
  return ();
 }
 public abstract void setData(ViewHolder holder,T t);
}

public class ViewHolder {
 private int position;
 private SparseArray<View> array;
 private View convertView;
 private Context context;

 private ViewHolder(ViewGroup parent, int position, int layout) {
   = position;
   = ();
  convertView = (()).inflate(layout, null);
  (this);
  array = new SparseArray<>();
 }

 public static ViewHolder getHolder(View convertView, ViewGroup parent, int position, int layout) {
  if (convertView == null) {
   return new ViewHolder(parent, position, layout);
  } else {
   ViewHolder holder = (ViewHolder) ();
    = position;
   return holder;
  }
 }

 public <T extends View> T getView(int viewId) {
  View view = (viewId);
  if (view == null) {
   view = (viewId);
   (viewId, view);
  }
  return (T) view;
 }

 public View getConvertView() {
  return convertView;
 }

 public ViewHolder setText(int viewId, String data) {
  TextView tv = getView(viewId);
  (data);
  return this;
 }

 public ViewHolder setImageResource(int viewId, int resId) {
  ImageView img = getView(viewId);
  (resId);
  return this;
 }

 public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
  ImageView img = getView(viewId);
  (bm);
  return this;
 }
}

public class Student {
 private String name;
 private String sex;
 public String getName() {
  return name;
 }
 public void setName(String name) {
   = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
   = sex;
 }
}

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.