[英]Error in sending Arraylist to Adapter in Recycler view in Android
I have an arraylist
CastArrayList
in which data is added in the onResponse
method of Volley
and from there I want to check if the size of CastArrayList
is greater than 7, another arraylist subCastArrayList
copies the CastArraylist
from position 0 to 7 and send it to RecyclerView
but not getting any view in RecyclerView
. 我有一个
arraylist
CastArrayList
,其中数据被添加到Volley
的onResponse
方法中,从那里我想检查CastArrayList
的大小是否大于7,另一个arraylist subCastArrayList
将CastArraylist
从位置0复制到7并将其发送到RecyclerView
但是没有在RecyclerView
获得任何视图。 I want if CastArrayList
size is greater than 7 SubCastArrayList
should copy first 7 elements else copy the whole CastArrayList
and then send the SubCastArrayList
to adapter
. 我想如果
CastArrayList
大小大于7, SubCastArrayList
应该复制前7个元素,否则复制整个CastArrayList
,然后将SubCastArrayList
发送到adapter
。
ArrayList<Cast> castArrayList;
ArrayList<Cast> subCastArrayList;
castArrayList = new ArrayList<>();
subCastArrayList = new ArrayList<>();
castDetailAdapter = new
CastDetailAdapter(MovieView.this,castArrayList,subCastArrayList);
RecyclerView.LayoutManager mLayoutManager = new
StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL);
recycler_view.setLayoutManager(mLayoutManager);
recycler_view.setItemAnimator(new DefaultItemAnimator());
recycler_view.setAdapter(castDetailAdapter);
StringRequest stringRequest1 = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
int i;
for (i=0;i<movieDetailFull.getCredits().getCast().size();i++) {
Cast cast = new Cast();
cast.setName(movieDetailFull.getCredits().getCast().get(i).getName());
cast.setId(movieDetailFull.getCredits().getCast().get(i).getId());
castArrayList.add(i,cast);
castDetailAdapter.notifyDataSetChanged();
}
}
if (castArrayList.size() > 7) {
subCastArrayList = new ArrayList<Cast>
(castArrayList.subList(0,6));
castDetailAdapter.notifyDataSetChanged();
}
else {
subCastArrayList = new ArrayList<Cast>(castArrayList);
castDetailAdapter.notifyDataSetChanged();
}
You are changing the reference of subCastArrayList
list by creating new arraylist mean now subCastArrayList
and list in adapter are two different reference to different list 您正在通过创建新的arraylist来更改
subCastArrayList
列表的引用现在subCastArrayList
和适配器中的列表是对不同列表的两个不同引用
so just add elements to it 所以只需添加元素即可
subCastArrayList.clear();
// ^^^ clear the list instead of creating new one inside if or else
if (castArrayList.size() > 7)
{
//subCastArrayList.addAll(castArrayList.subList(0,6));
subCastArrayList.addAll(castArrayList.subList(0,7));
// upper range is exclusive so use ^
}else{
subCastArrayList.addAll(castArrayList);
}
castDetailAdapter.notifyDataSetChanged();
// move notify outside , avoid redundant statements
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.