SoFunction
Updated on 2025-04-10

Detailed explanation of Android SimpleAdapter adapter usage

Android SimpleAdapter usage details

HolderAdapter Background

Android AdapterView uses a lot, ListView, GridView, Spinner, etc. The native BaseAdapter does not support ViewHolder. It is necessary to define internal classes, inflater root layout, set clicklistener for the internal view of the item and forward to the caller of the adapter. It feels very complicated to write more times, so I wrote a simple package to simplify the writing of Adapter.

Repository connection/phodev/SimpleAdapter.

Introduction to main documents

1. Inherited from BaseAdapter, add common functions, and agree on List data source through generics, implement the getCount function instead of subclasses, and add bindClick to implement Adapter's OnItemClickListener.

2. Combined with HolderAdapter to realize the view initialization method similar to Activity onCreate->setContentView->findViewById.

3. Inherited from CommonAdapter, mainly used to drive BaseViewHolder work.

How to use

1.Adapter definition

public class SampleHolderAdapter extends HolderAdapter<Object> {
  //Define the click event inside the Item, if not required, you can not define it  public static final int CLICK_ACTION_ADD = 1;
  public static final int CLICK_ACTION_UPDATE = 2;
  public static final int CLICK_ACTION_DELETE = 3;
  //Constructor configures the data source (optional, or updates later via setData)  public SampleHolderAdapter(List<Object> data) {
    super(data);
  }

  @Override
  public BaseViewHolder<?> createViewHolder(int position) {
    //Create the expected ViewHolder    return new ViewHolder();
  }

  class ViewHolder extends BaseViewHolder<Object> {
    Button btn1, btn2, btn3;

    @Override
    protected void onCreate(Context context, ViewGroup parent) {
      //OnCreate will be executed once after the ViewHolder is created      setContentView(.item_test);//setContent supports View or layout id      btn1 = (Button) findViewById(.btn1);//findViewById
      btn2 = (Button) findViewById(.btn2);
      btn3 = (Button) findViewById(.btn3);
    }

    @Override
    protected void onDataChanged(int position, Object data) {
      //OnDataChanged will be executed once when the adapter getView      ("add " + position);
      ("update " + position);
      ("delete " + position);
      //
      bindClick(btn1, position, CLICK_ACTION_ADD);//Bind ItemClick, you need to bind every time      bindClick(btn2, position, CLICK_ACTION_UPDATE);
      bindClick(btn3, position, CLICK_ACTION_DELETE);
    }
  }

}

2. Adapter use

 //Data source  private final ArrayList<Object> mData = new ArrayList<>();
  //Create Adapter and bind the data source  private final SampleHolderAdapter mAdapter = new SampleHolderAdapter(mData);

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    mListView = (ListView) findViewById();
    //Bind Adapter    (mAdapter);
    //Set click listener (due to CommonAdapter support and declared via bindClick)    (onItemClickListener);
  }

  //ClickListener implementation  private OnItemClickListener onItemClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(CommonAdapter<?> adapter, View v, int position, int action) {
      switch (action) {//action is the value defined in SampleHolderAdapte and bindClick        case SampleHolderAdapter.CLICK_ACTION_ADD:
          toast(getContext(), position + " add click");
          break;
        case SampleHolderAdapter.CLICK_ACTION_UPDATE:
          toast(getContext(),position + " update click");
          break;
        case SampleHolderAdapter.CLICK_ACTION_DELETE:
          toast(getContext(), position + " delete click");
          break;
      }
    }
  };

The code defined and used is completed here.

Thank you for reading, I hope it can help you. Thank you for your support for this site!