从FirebaseRecyclerAdapter中删除项目时Android应用程序崩溃

时间:2017-11-21 10:23:13

标签: android firebase-realtime-database recycler-adapter

我在populateViewHolder中创建了一个remove函数,如下所示:

viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        new AlertDialog.Builder(getContext())
                .setTitle("Are you sure?")
                .setMessage("Do you want to delete this note?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mNotesDatabase.child(list_id).removeValue();
                        notifyDataSetChanged();
                        notifyItemRemoved(position);
                    }
                })
                .setNegativeButton("No", null)
                .show();

        return true;
    }
});

此代码存在于:public void onDataChange(DataSnapshot dataSnapshot). 当我运行我的代码并尝试从列表中删除该项目时,我的应用程序崩溃了。但是,它确实删除了数据库本身中的项目。我认为我的应用没有正确更新FirebaseRecycleAdapter。我尝试使用notifydatasetchanged(),但却没有任何结果。我还试图覆盖onChildRemoved。但是onChildRemoved似乎不是FirebaseRecycleAdapter内的函数。我也试着打电话给FirebaseRecycleAdapter本身,它告诉我它没有被初始化。

这是logCat:

enter image description here

修改

在此文本下方,您可以按要求找到整个适配器的代码:

        final FirebaseRecyclerAdapter<Notes, NotesViewHolder> notesRecyclerViewAdapter = new FirebaseRecyclerAdapter<Notes, NotesViewHolder>(
            Notes.class,
            R.layout.feed_note_single_layout,
            NotesViewHolder.class,
            mNotesDatabase
    ) {

        @Override
        protected void populateViewHolder(final NotesViewHolder viewHolder, Notes model, final int position) {

            final String list_id = getRef(position).getKey();

            mNotesDatabase.child(list_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    final String title = dataSnapshot.child("title").getValue().toString();
                    final String message = dataSnapshot.child("message").getValue().toString();

                    viewHolder.setTitle(title);
                    viewHolder.setMessage(message);

                    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent noteIntent = new Intent(getContext(), NotesActivity.class);
                            noteIntent.putExtra("list_id", list_id);
                            noteIntent.putExtra("title", title);
                            noteIntent.putExtra("message", message);
                            startActivity(noteIntent);

                        }
                    });

                    viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            new AlertDialog.Builder(getContext())
                                    .setTitle("Are you sure?")
                                    .setMessage("Do you want to delete this note?")
                                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {



                                            mNotesDatabase.child(list_id).removeValue();
                                            notifyItemRemoved(viewHolder.getAdapterPosition());
                                            notifyDataSetChanged();


                                        }
                                    })
                                    .setNegativeButton("No", null)
                                    .show();

                            return true;
                        }
                    });

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    };

    mFeedList.setAdapter(notesRecyclerViewAdapter);

2 个答案:

答案 0 :(得分:1)

我认为可能会出现问题,因为当您删除RecyclerView中的数据并致电notifyDataSetChanged()时。 position中的populateViewHolder可能会引用已删除的position

因此,您应该使用viewHolder.getAdapterPosition()代替position

根据日志,问题也可能来自onDataChange NoteActivity,请仔细检查。

希望,这是有帮助的。

答案 1 :(得分:1)

删除下面的项目后调用notifyDataSetChanged()

 mNotesDatabase.child(list_id).removeValue();

 notifyItemRemoved(position);
 notifyDataSetChanged();