3

I want to know if is it a good way to use the same adapter for more than one listview.

in my code i have many listviews and each one contains the same UL components like imageview and textview, so is it good to use `MyAdapter extends BaseAdapter` for each of them ? or it is better to make adapter for each one?

if i have to use one adapter, how to handle the different onclick actions for the button, imageview and textview for each listview ?

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;
    }

}
William Kinaan
  • 28,059
  • 20
  • 85
  • 118
  • Yes , It is possible to use the same adapter multiple times containing the same UI component and you can manage the onclick on your own. Just you need to think out on your own based on the functionality which you want to achieve for the different onclick actions for button,imageview etc. – GrIsHu Jan 23 '13 at 16:18
  • the onclick have to be inside the getview , so how can i made different on click ? – William Kinaan Jan 23 '13 at 22:17

3 Answers3

0

It will make no difference resource-wise either way, as you will have to create a new instance of the adapter for each listview anyway. But trying to incorporate the features of two different adapters into one even just sounds overly complex. I would say for clarity of design, just make two different adapters. It'll make your life so much easier in the long run when it comes to debugging as well.

Keep in mind this is when the behaviors of each list are different, if the lists are supposed to function the same go ahead and use the same adapter for each.

Chad Campbell
  • 321
  • 1
  • 9
0

Are you talking about reusing the instance of the adapter or its class? The class can be reused ad infinatum.

The instance, however, is safer not to be reused. The reason for this is you will likely have collsions or artifacts from the previous AdapterView. Adapter creation is menial, so why not just be safe and create a new one for each AdapterView?

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • i am talking about reusing the class, also i know it is easier to make a new adapter for each listview but i meant by the question to know the standard way – William Kinaan Jan 23 '13 at 16:10
0

This is a really good question I often struggle with. Seems so unnecessary duplicating so much adapter code just for different actions. I still struggle with this questions as a design issue, so my answer is not intended to provide an answer on that. However, for the part of the question about reusing the adapter or not, what I do if I wish to reuse a list/adapter is this:

For each type of list I create a global constant value to act as an identifier for that type of list. When I create a new instance of the adapter I supply the requestId/listTypeId to 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 further 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 the listTypeId which I then compare the global constants to identify what do to further:

@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 dont think its great. If any one has another proper design pattern please let us know!

Chris
  • 4,593
  • 1
  • 33
  • 37