简体   繁体   English

嵌套适配器的Android notifyDataSetChanged不起作用

[英]Android notifyDataSetChanged for nested Adapter doesn't work

In my SectionsListAdapter adapter i have nested Adapter named MonthLessonsList , after get date from web service and set new data and call notifyDataSetChanged method for adapter, main adapter as SectionsListAdapter can be refresh, but nested adapter dont refresh and after calling notifyDataSetChanged for that, doesn't work correctly and don't show new data 在我的SectionsListAdapter适配器中,我嵌套了名为MonthLessonsList Adapter ,在从Web服务获取日期并设置了新数据并为适配器调用notifyDataSetChanged方法之后,主适配器为SectionsListAdapter可以刷新,但是嵌套的适配器不刷新, notifyDataSetChanged调用了notifyDataSetChanged后,不会刷新无法正常工作且不显示新数据

call and set new data from Fragment : Fragment调用并设置新数据:

monthSectionsItems = SQLite.select().from(MonthSections.class).queryList();
adapter.setData(monthSectionsItems);

and my Adapter with nested adapter: 和我的带嵌套适配器的适配器:

public class SectionsListAdapter extends RecyclerView.Adapter<SectionsListAdapter.MyViewHolder> {

    private final OnItemSelected listener;
    private List<MonthSections> list;
    private Context context;
    private MonthLessonsList lessonsListAdapter;

    public SectionsListAdapter(List<MonthSections> followingsList, Context mContext, OnItemSelected listener) {
        this.list = followingsList;
        this.context = mContext;
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sections_list_row, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        holder.indicatorIcon.setText(list.get(position).getSection_month_name());
        ...

        List<SectionLesson> lessonsList = list.get(position).getLessons();
        lessonsListAdapter = new MonthLessonsList(lessonsList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
        holder.userChildCategories.setLayoutManager(mLayoutManager);
        holder.userChildCategories.setAdapter(lessonsListAdapter);

        ...
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setData(List<MonthSections> data) {
        list.clear();
        list.addAll(data);
        notifyDataSetChanged();
        lessonsListAdapter.notifyDataSetChanged();
    }

    public List<MonthSections> getData() {
        return list;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ...

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

            section_title.setGravity(Gravity.RIGHT);
        }
    }

    public class MonthLessonsList extends RecyclerView.Adapter<MonthLessonsList.LessonsViewHolder> {
        private List<SectionLesson> lessonItem;

        public class LessonsViewHolder extends RecyclerView.ViewHolder {
            public TextView title;

            public LessonsViewHolder(View view) {
                super(view);
                title = view.findViewById(R.id.title);
            }
        }

        public MonthLessonsList(List<SectionLesson> lists) {
            this.lessonItem = lists;
        }

        @Override
        public LessonsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.child_list_row, parent, false);

            return new LessonsViewHolder(itemView);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(@NonNull LessonsViewHolder holder, int position) {
            SectionLesson lesson = lessonItem.get(position);
            holder.title.setText("درس: " + (position + 1) + ") " + lesson.getTitle());
        }

        @Override
        public int getItemCount() {
            return lessonItem.size();
        }
    }

}

Declare child adapter inside onBindViewHolder , and remove from global level. onBindViewHolder声明子适配器,并从全局级别删除。 And no need to notify child adapter. 并且无需通知子适配器。 Because that will be automatically filled when parent onBindViewHolder is called. 因为这将在调用父onBindViewHolder时自动填充。 Like 喜欢

MonthLessonsList lessonsListAdapter = new MonthLessonsList(lessonsList);
holder.userChildCategories.setAdapter(lessonsListAdapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM