Android Firebase从列表中删除项目

时间:2016-12-31 05:08:25

标签: android firebase firebase-realtime-database

我正在尝试从数据库中的列表中删除项目。它添加了.push()

以下是一个例子:

enter image description here

例如,我想删除" 8mtuj28 ..."从列表中(我有关键顺便说一句)。

以下是我希望工作的一些代码示例:

//remove poll from author's list of polls
authorRef.child(getString(R.string.my_polls_ref)).child(pollIds.get(currentPoll)).removeValue();

感谢任何帮助!

修改

我意识到我需要使用整数作为键而不是字符串。我完全忽略了民意调查键前面的数字。我想我会将所有这些改为Map而不是列表。

5 个答案:

答案 0 :(得分:0)

//从密钥中你可以获得0,1,2,3,4,... firebase db的值。

如果您知道 8mtu 键的值<1> ....

ref.child("myPolls").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        // you can pass here that value instead of **1**
                        ref.child("myPolls).child("1").setValue(null);
                    }
                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                    }
                });

答案 1 :(得分:0)

for (DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
    final String key = childSnapShot.getKey();
}

答案 2 :(得分:0)

不确定是否得到了您需要的答案,但这应该从列表中删除一个项目。您最终只需深入查看要删除的节点。

firebase.database().ref('uft0jep4knqlnum...').remove();

从&#34;删除数据&#34;

部分

https://github.com/danpaquin/gdax-python/blob/master/gdax/gdax_auth.py https://firebase.google.com/docs/database/web/read-and-write

答案 3 :(得分:0)

这是一个古老的问题,但也许它将帮助某个人。 如果您正在将Firebase数据库中的项目查看到recyclerview / list上,并试图通过单击或滑动删除来删除每个项目,则此解决方案适合您: 首先,假设我们有一个List listOfApples = new ArrayList <>();。并且其中包含一些数据。

public void removeItems(int position, DatabaseReference ref){
String appleName = "";
double applePrice = 0.0;
for(int i = 0; i < listOfApples.size(); i++){
if(i == position){
appleName = listOfApples.get(i).getAppleName();
applePrice = listOfApples.get(i).getApplePrice();
listOfApples.remove(position);
notifyDataChanged();
notifyItemRangeChanged(position, getItemCount);
}
}
final String finalAppleName = appleName;
final double finalApplePrice = applePrice;
ValueEventListener removeListener = new ValueEventListener(){ 
@Override
public void onDataChanged(@NonNull DataSnapShot dataSnapshot(){
for(DataSnapshot snapShot: dataSnapShot().getChildren()){
if(snapShot != null){
Apple apple = snapShot.getValue(Apple.class);
if(apple != null){
if(apple.getAppleName().equals(finalAppleName) && apple.getApplePrice() == finalApplePrice){
if(ref != null && snapShot.getKey() != null){
ref.child(snapShot.getKey()).removeValue(new DatabaseReference.CompletionListener(){
@Override
 public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
// show a message or do whatever you feel like
});
      }
     }
    }
   }
  }
}
@Override
 public void onCancelled(@NonNull DatabaseError databaseError) {

        }
};
ref.addValueEventListener(removeListener);
}

仅此而已。现在,我想指出的是此解决方案的一个警告,我们首先从recyclerview中删除该项目,然后再从firebase数据库中删除它。如果用户在删除项目时断开网络连接怎么办?某些人可能对此表示满意,因为您可以向用户显示由于某些问题而无法删除的消息。对于那些对此不满意的用户,可以在onComplete(..)方法中删除该项目,通知适配器并刷新/重新创建活动。 希望对您有帮助,并祝您编程愉快:)

答案 4 :(得分:0)

这是我的解决方案,但我的列表中有一个jsobject

    String value = "valor";
    Sting key = "key";                                                          
    Query query = mDatabase.child(path).child(mFirebaseAuth.getUid());
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                for(DataSnapshot du : dataSnapshot.getChildren()){
                    if(value.contentEquals(du.child(key).getValue().toString())){
                        mDatabase.child(path).child(mFirebaseAuth.getUid()).child(du.getKey()).removeValue();
                    }
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
相关问题