重新启动应用程序后,RecyclerView项目消失

时间:2019-07-07 02:52:19

标签: android android-recyclerview listadapter android-diffutils

我以前使用的是RecyclerView Adapter,因此决定切换到ListAdapter以便将DiffUtils用于动画。起初一切似乎都很顺利,直到我从任务切换器(内存)清除了我的应用程序。一旦我再次打开我的应用程序,所有项目都消失了,这是通过SQLite数据库实现的。原来,我必须在RecyclerView中添加一个新项才能显示以前的项。这是在我切换到ListAdpater之后发生的,我认为这是罪魁祸首。任何帮助,将不胜感激。预先感谢。

我的适配器类

public class CourseAdapter extends ListAdapter<Course, CourseAdapter.CourseHolder> {

    private OnItemClickListener listener;

    public CourseAdapter() {
        super(DIFF_CALLBACK);
    }

    private static final DiffUtil.ItemCallback<Course> DIFF_CALLBACK = new DiffUtil.ItemCallback<Course>() {
        @Override
        public boolean areItemsTheSame(@NonNull Course oldItem, @NonNull Course newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull Course oldItem, @NonNull Course newItem) {
            return oldItem.getName().equals(newItem.getName()) &&
                    oldItem.getBuilding().equals(newItem.getBuilding()) &&
                    oldItem.getRoom().equals(newItem.getRoom()) &&
                    oldItem.getDays().equals(newItem.getDays()) &&
                    oldItem.getStartTime().equals(newItem.getStartTime()) &&
                    oldItem.getEndTime().equals(newItem.getEndTime()) &&
                    oldItem.getProfName().equals(newItem.getProfName()) &&
                    oldItem.getEmail().equals(newItem.getEmail()) &&
                    oldItem.getProfBuilding().equals(newItem.getProfBuilding()) &&
                    oldItem.getProfRoom().equals(newItem.getProfRoom()) &&
                    oldItem.getProfDays().equals(newItem.getProfDays()) &&
                    oldItem.getProfStartTime().equals(newItem.getProfStartTime()) &&
                    oldItem.getProfEndTime().equals(newItem.getProfEndTime()) &&
                    oldItem.getColor() == newItem.getColor();
        }
    };

    @NonNull
    @Override
    public CourseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_item, parent, false);
        return new CourseHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CourseHolder holder, int position) {
        Course currentCourse = getItem(position);

        holder.card.setCardBackgroundColor(currentCourse.getColor());
        holder.textViewName.setText(currentCourse.getName());
        holder.textViewDays.setText(currentCourse.getDays());
        String startTime = currentCourse.getStartTime();
        String endTime = currentCourse.getEndTime();
        if (startTime.isEmpty() && endTime.isEmpty()) {
            holder.textViewTime.setText("");
        } else {
            holder.textViewTime.setText(startTime + "-" + endTime);
        }
    }

    public Course getCourseAt(int position) {
        return getItem(position);
    }

    class CourseHolder extends RecyclerView.ViewHolder {

        private TextView textViewName;
        private TextView textViewDays;
        private TextView textViewTime;
        private MaterialCardView card;

        public CourseHolder(@NonNull View itemView) {
            super(itemView);
            textViewName = itemView.findViewById(R.id.text_view_name);
            textViewDays = itemView.findViewById(R.id.text_view_days);
            textViewTime = itemView.findViewById(R.id.text_view_time);
            card = itemView.findViewById(R.id.card_view_courses);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (listener != null && position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(getItem(position), position, textViewName);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Course course, int position, TextView title);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}

在主要活动中

RecyclerView recyclerView = findViewById(R.id.recycler_view_courses);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);

CourseAdapter adapter = new CourseAdapter();
recyclerView.setAdapter(adapter);

viewModel = ViewModelProviders.of(this).get(ViewModel.class);

viewModel.getAllCourses().observe(this, new Observer<List<Course>>() {
    @Override
    public void onChanged(List<Course> courses) {
        adapter.submitList(courses);
    }
});

0 个答案:

没有答案