【问题标题】:Cannot make copy arraylist in RecyclerView_Adapter constructor无法在 RecyclerView_Adapter 构造函数中复制数组列表
【发布时间】:2020-09-04 19:37:27
【问题描述】:

我在理解如何复制ArrayList 方面遇到了一个小问题。有我的代码:

public class RecyclerView_Adapter extends RecyclerView.Adapter<RecyclerView_Adapter.recyclerViewHolder>{
    private List<Item> list1;
    private List<Item> list2;
    
    // with constructor:
    public RecyclerView_Adapter(Context context, Arraylist<Item> list1){
        this.context = context;
        this.list1 = list1;
        this.list2 = new Arraylist<>(list1)
    }
}

它应该将 list1 复制到 list2 对吗? 当我使用此代码时:

Log.i("TAG1", list1.toString());
Log.i("TAG2", list2.toString());

返回

TAG1 : []
TAG2 : []

稍后在另一个地方(例如在视图中) 相同的代码返回:

TAG1: [com.example.something1290, com.example.something1267, com.example.something1298] (some data)
TAG2: [] // <-- still returns empty array why?

当我使用此代码时,不是在构造函数中而是在其他地方,它会在两个数组中返回相同的数据。

所以问题是:

为什么在构造函数中我不能将 list1 复制到列表 2 但我必须以不同的方法执行此操作?
编辑:
它看起来像:

// with constructor:
    public RecyclerView_Adapter(Context context, Arraylist<Item> list1){
        this.context = context;
        Log.i("TAG1", list1.toString());
        this.list1 = list1;
        this.list2 = new Arraylist<>(list1)
        // It can return an empty array like:
        TAG1 : []
    }
}

但是在这种情况下:

// with constructor:
    public RecyclerView_Adapter(Context context, Arraylist<Item> list1){
        this.context = context;
        this.list1 = list1;
        Log.i("TAG1", list1.toString());
        this.list2 = new Arraylist<>(list1)
        // It should return an array with data, but return empty array again
        TAG1 : []
    }
}

【问题讨论】:

  • 您能否澄清一下:构造函数中的两个 ArrayList 都为空吗?记录时 list2 是否始终为空?
  • 是的,我认为如果 list1 不为空,复制任何具有相似类型的东西都很容易,例如,int a;诠释 b; a=5; b=a; (a 被复制到 b)。
  • 是的,两个数组都是空的。当我将数组传递给构造函数时, Line this.list1 = list1;返回 [] 但在另一种方法中返回带有数据的数组。看起来在构造函数中 array1 返回为空,但在下一个方法中 viewholder 或 onbindviewholder 返回数据,其中 list2 仍然返回 [] 空标签。我的问题是array1,因为它复制到array2空数据,然后用数据填充自己

标签: android android-recyclerview android-adapter


【解决方案1】:

首先,您要创建一个在另一个列表中传递的列表。这不会制作另一个副本,而是会在其中插入另一个列表而不是复制它。您可以使用ArrayList.clone()ArrayList.addAll()等方法来实现复制arraylist

// with constructor:
public RecyclerView_Adapter(Context context, Arraylist<Item> list1){
    this.context = context;
    Log.i("TAG1", list1.toString());
    this.list1 = list1;
    this.list2 = new Arraylist<>().addAll(list1) // or this.list2 = list1.clone();
    // It will return the contents of list1 copied to list2 array like:
    
}

【讨论】:

  • 不幸的是,无法正常工作。我认为列表的副本应该是这样的:this.list2 = new Arraylist&lt;&gt;(list1);累了你的sollutuion android studio给我一个错误:不兼容的类型:布尔不能转换为List&lt;Itemt&gt; list2 = new ArrayList&lt;&gt;().addAll(list1);克隆给我:不兼容的类型:对象不能转换为@987654326 @ 在同一行。在我做了list2 = (List&lt;Item&gt;) list1.clone(); 之后,第二个数组仍然是空的,没有错误
  • 有趣的东西。在 MainActivity 的 OnResume 方法中,我有一行:new BackgroundTask(RecyclerView recyclerView, Context context, ArrayList&lt;Item&gt; datalist, DataBaseHelper dbhelper) BackgroundTask extends AsyncTask。然后在 doInBackground 方法(BackgroundTask.class)中。我把数据从sqldatabase放到arraylist。此列表传递给 RecyclerView_Adapter。当我调用 Log.i("TAG" data_list.toString()); 时,在 OnResume 方法(MainActivity.class)结束时返回空标签[],但列表显示正确。也许这就是为什么我在构造函数方法中有 RecyclerView_Adapter 空标签
  • 第二件事最后。我认为....:D App 返回一个带有数据的数组,好像很喜欢一样。我在构造函数中添加了新的 Runnable 方法(使用 thread.sleep(1000 milis)。早些时候一切正常,但代码继续前进,不等待用数据填充数组。简而言之,它有效,但这个解决方案并没有让我高兴,是吗另一种解决方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多