【问题标题】:Android displaying two arraylists with different types into one seamless listview using adaptersAndroid 使用适配器将两个不同类型的数组列表显示到一个无缝列表视图中
【发布时间】:2019-11-05 18:53:50
【问题描述】:

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

我以前从未这样做过,因此非常感谢一般的 cmets 和指南。我试图使 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


    【解决方案1】:

    您在 ArrayLists 中使用了哪些数据类型?

    如果,例如,字符串和整数,你能不转换值并合并到单个 ArrayList 中吗?

    for (int i=0; i<arraylist1.size(); i++) { //arraylist1 containing integers
    
    arraylist2.add(Integer.toString(arraylist1.get(i));
    
    }
    

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

    【讨论】:

    • 不是真的没有。数据类型是我自己的自定义基类和另一个子类
    【解决方案2】:

    你可以使用视图类型来解决这个问题。

    示例:

    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>
    

    YourTypeOne.java

    public class YourTypeOne{
        public String title;
    }
    

    YourTypeTow.java

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

    YourAdapter.java

    public class YourAdapter extends RecyclerView.Adapter<YourAdapter.YourViewHolder> {
    
        ArrayList<Object> 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(Object object){
    
                if (object instanceof YourTypeOne){
                    //set data to layout row_type_one
                }else {
                    //set data to layout row_type_two
                }
    
            }
    
    
        }
    }
    

    祝你好运。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      相关资源
      最近更新 更多