简体   繁体   English

在Android中的Recycler视图中将Arraylist发送到适配器时出错

[英]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 ,其中数据被添加到VolleyonResponse方法中,从那里我想检查CastArrayList的大小是否大于7,另一个arraylist subCastArrayListCastArraylist从位置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.

相关问题 回收站视图:未连接适配器; 跳过布局(API 的回收站视图中的错误) - Recycler view: no adapter attached; skipping layout(Error in recycler view for API ) Android在Recycler View适配器中膨胀两个布局 - Android inflate two layouts in recycler view adapter Android Recycler 视图自定义适配器未执行 - Android Recycler view custom adapter not executing 这在 android 回收器视图适配器中不起作用 - this not working in android recycler view adapter Intent 如何将可拆分数组列表添加到部分回收器视图适配器 - How to add parcelable arraylist to section recycler view adapter 在回收站视图中未连接适配器 - No adapter attached in Recycler View 在回收站视图适配器中显示自定义弹出窗口 class - Android Java - Show customize pop up in recycler view adapter class - Android Java 在Recycler View Adapter中无法识别Android EXTRA_MESSAGE - Android EXTRA_MESSAGE not recognized inside Recycler View Adapter 如何将数据从活动传递到 android 中的回收器视图适配器 - How to pass data from a activity to a recycler view adapter in android 更新 Recycler 查看项目按钮在适配器外部的可见性时出错 - Error Updating Recycler View Item Button Visibility outside Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM