删除的firebase值仍显示在datasnaphot中

时间:2018-01-14 14:10:56

标签: java android firebase firebase-realtime-database

我在firelerView中列出了Firebase数据,当我刷一个recyclerView项时,它的值将从FireBase中删除。一旦删除,我想将更新的值添加到arrayList(当然减去已删除的值)。我的问题是,即使删除了值,它仍会显示在更新的快照中。代码如下。

    ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT) {
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        final int position = viewHolder.getAdapterPosition();
        if(bidslist.get(position).getUsername().equals(userEmail)){
            deleteRef = bidslist.get(position).getMsgID();

            mRef.orderByChild("msgID").equalTo(deleteRef).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                        deleteKey = childSnapshot.getKey();
                        mRef.child(deleteKey).removeValue();
                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }

            });

            if(bidslist.get(position).getAmount().equals(biggest.toString())){
                /*removing data from the second referance. As soon as this is done I would like to update 
                the reference with the new snapshot, excluding the deleted one.*/

                highestRef.child(fbid).removeValue();

               mRef.addValueEventListener(new ValueEventListener() {
                   @Override
                   public void onDataChange(DataSnapshot dataSnapshot) {
                       /*This is supposed to be the new snapshot*/
                      BidFirebaseObject obj = dataSnapshot.getValue(BidFirebaseObject.class);
                       Log.d("dhat", obj.getAmount());
                   }

                   @Override
                   public void onCancelled(FirebaseError firebaseError) {

                   }
               });

            }

            //remove it from the adapter
            if (direction == ItemTouchHelper.LEFT){
                adapter.removeItem(position);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要使用notifyDataSetChanged()通知适配器任何更改:

 adapter.notifyDataSetChanged()
  

void notifyDataSetChanged()   通知附加的观察者基础数据已被更改,反映数据集的任何视图都应自行刷新。

此处有更多信息:https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()