删除项目后,Android Firebase RecycleView无法更新

时间:2018-01-13 12:02:10

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

我有一个FirebaseRecycleAdapter。当我填充Viewholder时,它应该检查是否有任何条目。如果没有显示,那么TextView会在"中显示某些内容;您没有任何条目"。我的问题是,一旦我删除了一个项目。它会快速闪烁“空白文本”。 TextView即使列表中还有其他条目。如果我从列表中删除最后一项,则只需切换到“空白文本”。 TextView,即使列表中仍有项目。 也许我没有正确更新视图?或者也许我正在翻转'视图不正确?

以下是我的代码,负责上述文本中的错误:

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

final String list_id = getRef(viewHolder.getAdapterPosition()).getKey();

mNotesDatabase.child(list_id).addValueEventListener(new ValueEventListener() 
{
   @Override
   public void onDataChange(final DataSnapshot dataSnapshot) {
       if (dataSnapshot.exists()) {
         if(dataSnapshot.getChildrenCount() != 0) {
            //'flip' view if dataSnapshot exists
           mFeedList.setVisibility(getItemCount() == 0 ? View.GONE : View.VISIBLE);
           mFapButton.setVisibility(getItemCount() == 0 ? View.GONE : View.VISIBLE);
           tanukisan.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
           headText.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
           subText.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
           seperator.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
           emptyNewBtn.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
         }
            //not important stuff here 

           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();
                               }
                           })
                           .setNegativeButton("No", null)
                           .show();

                   return true;
               }
           });
       }

        //'flip' view if dataSnapshot not exists
       else{
         if(dataSnapshot.getChildrenCount() == 0) {
           mFeedList.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
           mFapButton.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
           tanukisan.setVisibility(getItemCount() == 0 ? View.GONE : View.VISIBLE);
           headText.setVisibility(getItemCount() == 0 ? View.GONE : View.VISIBLE);
           subText.setVisibility(getItemCount() == 0 ? View.GONE : View.VISIBLE);
           seperator.setVisibility(getItemCount() == 0 ? View.GONE : View.VISIBLE);
           emptyNewBtn.setVisibility(getItemCount() == 0 ? View.GONE : View.VISIBLE);
         }
       }
   }        
    //not implemented functions here 
});

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您已将侦听器附加到单个列表项#include <iostream> int main() { char c[5]; c = "Hey!"; //ERROR: expression must be a modifiable lvalue. } 上,当您删除它时,显示TextView是有意义的。要解决这个问题,你需要将一个监听器连接到树中一步,循环遍历子项,然后使用以下代码行检查子项是否存在:

mNotesDatabase.child(list_id)

以下示例:

if(dataSnapshot.getChildrenCount() == 0) {
    //Display the TextView
} else {
    //Hide the TextView
}

如您所见,侦听器附加在DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference itemsRef = rootRef.child("items"); ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.exists() && dataSnapshot.getChildrenCount() != 0) { //Hide the TextView } else { //Display the TextView } } @Override public void onCancelled(DatabaseError databaseError) {} }; itemsRef.addListenerForSingleValueEvent(eventListener); 节点上,而不是items上。