Android notifyDataSetChanged for nested Adapter不起作用

eh57zj3b  于 2023-10-14  发布在  Android
关注(0)|答案(2)|浏览(131)

在我的SectionsListAdapter适配器中,我嵌套了名为MonthLessonsListAdapter,在从web服务获取数据并设置新数据并为适配器调用notifyDataSetChanged方法后,主适配器(如SectionsListAdapter)可以刷新,但嵌套适配器不会刷新,并且在为此调用notifyDataSetChanged后,无法正常工作,也不会显示新数据
Fragment调用并设置新数据:

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

我的适配器与嵌套适配器:

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();
        }
    }

}
z4iuyo4d

z4iuyo4d1#

decompose onBindViewHolder内部的子适配器,并从全局级别删除。而且不需要通知子适配器。因为当父onBindViewHolder被调用时,它会自动填充。像

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

iyfamqjs2#

假设你有两个适配器AdapterA和AdapterB,现在AdapterB在AdapterA中设置,你需要更新AdapterB中的数据,只需在AdapterA中创建一个方法,如下所示:

fun setList(adapterAList:ArrayList<Object>,adapterBlist:ArrayList<Object>) {
      adapterAList.clear()
      adapterAList.addAll(daysList)
      adapterBlist.clear()
      adapterBlist.addAll(dayOfWeekList)
      notifyDataSetChanged()
}

现在从你的Activity/Fragment调用这个方法,并给予AdapterA和AdapterB的列表,这将改变你在Nested ClusterView中的数据。

相关问题