Android: reusable adapter and another onclicklistener

I want to know if this is a good way to use the same adapter for multiple lists.

I have a lot of lists in my code and each contains the same UL components as the image and text view, so is it okay to use "MyAdapter extends BaseAdapter" for each one? or is it better to make an adapter for each of them?

if i need to use one adapter how to handle different button, image and textview onclick actions for each list?

class MyAdapter extends BaseAdapter {

    public MyAdapter() {
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return null;
    }

}

      

+3


source to share


3 answers


This will have nothing to do with resources anyway, since you will still have to create a new adapter instance for each list. But trying to incorporate the functions of two different adapters into one even sounds too difficult. I would say, for design clarity, just make two different adapters. It will make your life easier in the long run when it comes to debugging.



Keep in mind that when the behavior of each list is different, if the lists are supposed to work the same, use the same adapter for each.

0


source


Are you talking about reusing an instance of an adapter or its class? The class can be reused indefinitely.



However, it is safer not to reuse the instance. The reason for this is that you are likely to have collisions or artifacts from the previous AdapterView. The adapter creation is black, so why not just be safe and create a new one for each adapter?

0


source


This is a very good question that I come across a lot. It seems so unnecessary to duplicate so much adapter code just for different actions. I am still struggling with these questions as a design issue, so my answer is not intended to answer this question. However, in relation to the question of reusing an adapter or not, what I do if I want to reuse a list / adapter is:

For each list type, I create a global constant value to act as an identifier for that list type. When I create a new instance of the adapter, I supply the requestId / listTypeId for the adapter:

//first i create the constants somewhere globally
TYPE_ID_A = 0;
TYPE_ID_B = 1;
TYPE_ID_C = 2

//then i feed them to my adapter and set the clickListener on my list
 mList.setAdapter(new MyListAdapter(mContext, listData, TYPE_ID_A));
 mList.setOnItemClickListener(this);

      

In my adapter, I set this typeId as a member variable and then create a public function to return this id:

public class MyListAdapter extends ArrayAdapter<JSONArray> {

    private final Context mContext;
    private final JSONArray mItems;
    private final int mListType;

    //assign the values in the constructor of the adapter
    public SearchListAdapter(Context context, JSONArray items, int listType) {
        super(context, R.layout.item_filter_list);
        mItems = items;
        mContext = context;
        mListType = listType;
   }

    //function to return the list id
    public int getListType(){
        return mListType;
    }
}

      

Finally, inside my onClick listener, I call this function inside my adapter to return a listTypeId, which I then compare the global constants to determine what to do next:

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    MyListAdapter adapter = (MyListAdapter) adapterView.getAdapter();
    int listType = adapter.getListType(); //get the listTypeId now

    //now see which list type was clicked:
    switch(listType){
        case(TYPE_ID_A):
          //to action for list A
          break;
        case(TYPE_ID_B):
          //to action for list B
          break;
    }
}

      

This works for me, but I don't think it's great. If anyone has another correct design template, please let us know!

0


source







All Articles