簡體   English   中英

使用Firebase檢測RecyclerView項目中的刪除

[英]Detect delete in RecyclerView item with Firebase

在我的應用程序中,我有一個植物列表,用戶可以插入新植物,對其進行修改和刪除。

用戶可以長按一下列表中的一項以將其刪除,但是如果其他人從數據庫中刪除另一台設備或我的同一項,則列表不會像在編輯中那樣進行更新 (該項保留在列表中,您也可以再次刪除它沒有任何意義)。

這是我在數據更改時更新項目的方法:

PlantViewHolder(View itemView, Context context) {
        super(itemView);
        cardView = (CardView)itemView.findViewById(R.id.cardView);
        plantCommonName = (TextView)itemView.findViewById(R.id.plantCommonName);
        plantScientificName = (TextView)itemView.findViewById(R.id.plantScientificName);
        plantPhoto = (ImageView)itemView.findViewById(R.id.plantPhoto);
        plantCategory = (TextView)itemView.findViewById(R.id.plantCategory);
        plantDescription = (TextView)itemView.findViewById(R.id.plantDescription);
        plantPrice = (TextView)itemView.findViewById(R.id.plantPrice);
        plantStock = (TextView)itemView.findViewById(R.id.plantStock);
        this.context = context;

        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);

        database.getReference().child("plants").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot dss: dataSnapshot.getChildren()) {
                    Plant p0 = dss.getValue(Plant.class);
                    editAt(p0);
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
                Log.w("Failed to read value.", error.toException());
            }
        });
    }

方法如下:

private void editAt(Plant p0) {
    for(int i = 0; i < plants.size(); i++) {
        if(plants.get(i).getID() == p0.getID()) {
            plants.get(i).setCommonName(p0.getCommonName());
            plants.get(i).setScientificName(p0.getScientificName());
            plants.get(i).setPrice(p0.getPrice());
            plants.get(i).setStock(p0.getStock());
            plants.get(i).setSeason(p0.getSeason());
            plants.get(i).setDescription(p0.getDescription());

            notifyItemRangeChanged(i, plants.size());
        }
    }
}

如您所見,我只是修改列表中的特定項目,但不知道如何在數據庫偵聽器中區分或實現刪除。 如何檢測是否從數據庫中刪除了一個項目並使用ValueEventListener將其刪除?

您不需要使用ValueEventListener來刪除值。 只需找到您的reference然后使用removeValue()方法即可。 假設您有一個名為Plants的節點,請使用以下代碼:

yourReference.child("Plants").child(plantId).removeValue();

在這plantId是要刪除的particulat廠。

如果要驗證是否存在某項,請reference ,然后在SanpShop使用方法SanpShop exists()

希望能幫助到你。

我通過從onChildRemoved獲取快照並檢查列表中是否包含此ID來解決此問題。

database.getReference().child("plants").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                Plant p0 = dataSnapshot.getValue(Plant.class);
                editAt(p0);
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                Plant p0 = dataSnapshot.getValue(Plant.class);
                for(int i = 0; i < plants.size(); i++) {
                    if(plants.get(i).getID() == p0.getID()) {
                        plants.remove(i);
                        notifyItemRemoved(i);
                        notifyItemRangeChanged(i, plants.size());
                    }
                }
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM