android - 按索引或类删除 firebase 元素

标签 android firebase

firebase 的 Java API似乎不太清楚。我将对象添加到我的 firebase 中,例如:

Food foodObj=new Food();
foodObj.setValue("name",food);//food is a string
foodObj.setValue("color",color);
myFirebaseRef.push().setValue(foodObj.getKeysValues());

我在 listView 中显示 firebase 中的每个元素。长按我想删除所选项目。我可以从前端arrayList获取索引和对象。

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

         final int temp = position;
         new AlertDialog.Builder(FridgeActivity.this)
              .setMessage("Do you want to delete this item?")
              .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       Log.d(tag, "should delete:" + Integer.toString(temp));
                       Food mess = fridge.get(temp);

                    }
                 })
            .setNegativeButton("No", null)
            .show();
        return true;
    }
});

我还没弄清楚如何删除困惑。我尝试过 myFirebaseRef.removeVal() 但这会从数据库中删除所有内容。

最佳答案

如果我理解正确的话,您的数据集如下所示(在您的 Firebase 仪表板中):

yourApp:
    food -
        FiReBaSeGenErAtEdKeY   - // Mapped to object Food 
                               - name: "food"
                               - color: "color"
        MoReGenErAtEdKeYs2     +
        MoReGenErAtEdKeYs3     +
        MoReGenErAtEdKeYs4     +

只能使用键删除值,更具体地说,使用所有键来创建对象的“路径”。例如,如果您想删除 Food 对象 FiReBaSeGenErAtEdKeY你需要知道它的路径。在这种情况下,您的路径将是 <your_ref>/food/FiReBaSeGenErAtEdKeY 。为此:

 myFirebaseRef
     .child("food") //the food branch
     .child("FiReBaSeGenErAtEdKeY") // the key of the object
     .removeValue();  // delete

或者如果您想删除 color值,你的路径将是 <your_ref>/food/FiReBaSeGenErAtEdKeY/color

myFirebaseRef
         .child("food") //the food branch
         .child("FiReBaSeGenErAtEdKeY") // the key of the object
         .child("color") //actually using "color" because that is the name of the key. This will remove the value, and because there are no more values, the key is empty, therefore it will be deleted too. 
         .removeValue();  // delete

事后很难获得 key 。有多种方法可以从 DataSnapshot 获取它,但我喜欢像这样设置我的对象:

public class Food {
     private String key;      // add the key here
     private String name;
     private String color;
     //other fields 

     public Food(){ /* empty constructor needed by Firebase */ }

     //Add all of your accessors 

}

然后,当您想要创建/保存食物对象时:

String key = myFireBaseRef.push().getKey();
Food food = new Food();
food.setKey(key);
food.setColor(color);
food.setName(name);

//Then to save the object:
myFirebaseRef
    .child("food") //move to the "food" branch
    .child(food.getKey())
    .setValue(food);

引用文献:

关于android - 按索引或类删除 firebase 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36138580/

相关文章:

java - 使用 AsyncTask 无法正确执行多部分实体

android - 在 Android 上制作交互式触摸对象

javascript - 单击按钮时对 Firebase 数据进行排序

ios - Firebase Crashlytics iOS : Cannot receive events even though the script seems running

firebase - 使用 Google AppS 脚本在 Firebase 中创建新用户

Angular2 karma 测试失败 : No provider for Token FirebaseUrl

android - Paho MQTT Android 服务唤醒 Activity

android - net::ERR_FILE_NOT_FOUND file:///android_asset/www/index.html 找不到

android - 跨平台移动框架背后的逻辑

javascript - 如何制定 Firebase 规则以确保您只能在字段中输入某些内容?