删除项目仍然存在于回收视图

时间:2018-06-02 08:13:08

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

这是否意味着我没有刷新我的适配器或什么?

@覆盖     public void onDeleteClick(int position)     {         FoodInfo selectedItem;

    if(!searchView.getQuery().toString().isEmpty())
    {
        selectedItem = newlist.get(position);
    }
    else
    {
        selectedItem = mfoodinfo.get(position);
    }

    final String selectedKey = selectedItem.getFoodname();

    StorageReference imageRef = firebaseStorage.getReferenceFromUrl(selectedItem.getImageUrl());
    imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            databaseReference.child(selectedKey).removeValue();
            foodAdapter.notifyDataSetChanged();
            Toast.makeText(Recycleview.this, "Item deleted", Toast.LENGTH_SHORT).show();

        }
    });

}

食物适配器

@Override
public int getItemCount() {
    return mfoodinfo.size();
}

public void setFilter(List<FoodInfo> newlist) {

    mfoodinfo = new ArrayList<>();
    mfoodinfo.addAll(newlist);
    notifyDataSetChanged();
}

     @Override
    public void onClick(View v) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                mListener.onItemClick(position);
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select Action");
        MenuItem delete = menu.add(Menu.NONE, 1, 1, "Delete");
        MenuItem update = menu.add(Menu.NONE, 2, 2, "Update ");


        update.setOnMenuItemClickListener(this);
        delete.setOnMenuItemClickListener(this);
    }
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {

                switch (item.getItemId()) {

                    case 1:
                        mListener.onDeleteClick(position);
                        return true;
                    case 2:
                        mListener.UpdateFood(position);
                        return true;

                }
            }
        }
        return false;
    }

}

public interface OnItemClickListener {
    void onItemClick(int position);

    void onDeleteClick(int position);

    void UpdateFood(int position);


}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

}

这是我的删除方法,我删除它仍然存在于我的回收视图中的项目bt在firebase中消失了

我必须离开此页面并再次访问,然后它只显示与firebase完全相同的项目

有人帮忙吗?

1 个答案:

答案 0 :(得分:2)

您只是从数据库中删除它,而不是从适配器中删除它。

databaseReference.child(selectedKey).removeValue();
foodAdapter.remove(item); // this is what you need 
foodAdapter.notifyDataSetChanged();

所以你应该在适配器中有一个remove方法。

<强>更新

这是适配器上需要的删除方法:

 private void remove(int position) {
    itemList.remove(position);
    notifyItemRemoved(position);
}