Android使用适配器将两个不同类型的数组列表显示到一个无缝列表视图中

问题描述 投票:0回答:2

我有一个基类的自定义ArrayAdapter。我也有一个子类,我希望ArrayAdapter能够接收两种不同类型的数据,即(arrayList和arraylist),并在一个无缝listview中连续显示它们。

我之前从未做过此操作,因此,一般的评论和指南倍受赞赏。我试图使ArrayAdapter通用:

public class MyAdapter<T extends BaseClass> extends ArrayAdapter<T>

使用此构造函数:

public MyAdapter(Context context, ArrayList<T> items) {
        super(context, 0, items);
        fullList = items;  
    }

但是我不知道如何传递可以具有子类或基类类型的通用ArrayList。 (并且此方法稍后将涉及使用instanceOf的代码,这不是做事的最佳方式……正如我从SO中学到的)]

[而不是泛型,我正在考虑扩展此自定义arrayadapter本身。但是我不确定这将如何工作。

解决此问题的最佳方法是什么?除了arrayadapter以外,是否还有适合这种情况的更好的适配器。还是recyclerview可以解决此问题?

我最终希望拥有两个相似但不同类型的数据集(一个子集的另一个子集),以一个无缝列表视图的形式输出。我找不到执行此操作的一般方法。

java android android-listview android-arrayadapter android-adapter
2个回答
0
投票

您在ArrayList中使用什么数据类型?

例如,如果是Strings和Integers,您是否不能转换值并合并到单个ArrayList中?

for (int i=0; i<arraylist1.size(); i++) { //arraylist1 containing integers

arraylist2.add(Integer.toString(arraylist1.get(i));

}

然后您可以将一个ArrayList应用于标准ArrayAdapter。


0
投票

您可以使用视图类型解决此问题。

示例:

row_type_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtTypeOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

row_type_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="100dp"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/txtTypeOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

YourBaseObject.java

public abstract class YourBaseObject {

}

YourTypeOne.java

public class YourTypeOne extends YourBaseObject {
    public String title;
}

YourTypeTow.java

public class YourTypeTow extends YourBaseObject {
    public String title;
    public String imgUrl;
}

YourAdapter.java

public class YourAdapter extends RecyclerView.Adapter<YourAdapter.YourViewHolder> {

    ArrayList<YourBaseObject> objects = new ArrayList<>();

    public YourAdapter(ArrayList<YourTypeOne> typeOneArray, ArrayList<YourTypeTow> typeTowArray) {
        objects.addAll(typeOneArray);
        objects.addAll(typeTowArray);
    }

    @Override
    public int getItemViewType(int position) {
        return objects.get(position) instanceof YourTypeOne ? 1 : 2;
    }

    @NonNull
    @Override
    public YourViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v;
        if (viewType == 1) {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row_type_one, parent, false);
        } else {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row_type_two, parent, false);
        }
        return new YourViewHolder(v,viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull YourViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return objects.size();
    }

    class YourViewHolder extends RecyclerView.ViewHolder {

        private TextView txtTypeOne;

        private TextView txtDescription;
        private ImageView logo;


        public YourViewHolder(@NonNull View itemView, int vewType) {
            super(itemView);

            if (vewType==1){
                //find views by ides in .xml
                txtTypeOne =  itemView.findViewById(R.id.txtTypeOne);
            }else {
                //find views by ides in row_type_two.xml
                txtDescription =  itemView.findViewById(R.id.txtDescription);
                logo = itemView.findViewById(R.id.logo);
            }
        }


        public void bindeData(YourBaseObject object){

            if (object instanceof YourTypeOne){
                //set data to layout row_type_one
            }else {
                //set data to layout row_type_two
            }

        }


    }
}

祝你好运。 :)

© www.soinside.com 2019 - 2024. All rights reserved.