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

时间:2019-02-18 09:25:56

标签: android

在我的SectionsListAdapter适配器中,我从Web服务获取日期并设置了新数据并为适配器调用Adapter方法,将嵌套的MonthLessonsList嵌套为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();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

df <- read.table(text = "ID month country count style 1 2012-02 UK 3 high 1 2012-02 US 10 high 1 2012-02 FR 5 high", header = T) 内声明子适配器,并从全局级别删除。 并且无需通知子适配器。因为这将在调用父级onBindViewHolder时自动填充。 喜欢

onBindViewHolder