Reputation: 293
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
. 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
.
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();
}
Upvotes: 1
Views: 162
Reputation: 37404
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
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
Upvotes: 3