3

我想知道将同一个适配器用于多个列表视图是否是一种好方法。

在我的代码中,我有许多列表视图,每个列表视图都包含相同的 UL 组件,例如 imageview 和 textview,所以为它们中的每一个使用“MyAdapter extends BaseAdapter”是否很好?或者最好为每个适配器制作适配器?

如果我必须使用一个适配器,如何处理每个列表视图的按钮、图像视图和文本视图的不同 onclick 操作?

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

}
4

3 回答 3

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!

于 2014-12-09T13:28:47.420 回答
0

无论哪种方式,在资源方面都没有区别,因为无论如何您都必须为每个列表视图创建一个新的适配器实例。但是,试图将两种不同适配器的功能合并到一个中甚至听起来过于复杂。我会说为了设计的清晰,只需制作两个不同的适配器。从长远来看,在调试方面,它也会让您的生活变得更加轻松。

请记住,这是当每个列表的行为不同时,如果列表应该具有相同的功能,请继续并为每个列表使用相同的适配器。

于 2013-01-23T16:06:08.960 回答
0

您是在谈论重用适配器或其类的实例吗?该类可以无限重复使用。

然而,该实例更安全,不被重用。这样做的原因是您可能会遇到来自以前的 AdapterView 的冲突或工件。适配器的创建是微不足道的,那么为什么不只是安全并为每个 AdapterView 创建一个新的呢?

于 2013-01-23T16:06:16.707 回答