简体   繁体   English

无法从FirebaseDatabase获取密钥

[英]Can't get key from FirebaseDatabase

I'm pushing an object to FirebaseDatabase and trying to get the key of the object that I just pushed to Firebase . 我正在将对象推送到FirebaseDatabase并尝试获取刚推送到Firebase的对象的密钥。 I've looked at Get the pushed ID for specific value in firebase android and I'm trying to use that but it's not working. 我已经看过在Firebase android获取特定值的推送ID ,我正在尝试使用它,但是它不起作用。

Initialization of DatabaseReference in onCreate onCreate初始化DatabaseReference

mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("People");

The actual function- 实际功能

 private String pushPersonToFirebase() {

        Person newPerson = new Person();
        String firstName = mFirstNameTextView.getText().toString();
        String lastName = mLastNameTextView.getText().toString();

        if(!firstName.equals("") && !lastName.equals("")) {
            newPerson.setFirstName(firstName);
            newPerson.setLastName(lastName);
            newPerson.setImgPath(anonymousPeople.get(indexAnon).toString());
            mDatabaseRef.push().setValue(newPerson);
            return mDatabaseRef.getKey();  //This always returns "People" need it to return the key of the object that was just pushed
        } else {
            Toast.makeText(this, "You didn't enter all fields", Toast.LENGTH_SHORT).show();
            return "";
        }

    }

you can get key using this way.. 您可以使用这种方式获取密钥。

String key = mDatabaseRef.push().getKey(); // its your key
-- code---
newPerson.setFirstName(firstName);
newPerson.setLastName(lastName);
newPerson.setImgPath(anonymousPeople.get(indexAnon).toString());
--- code ---
mDatabaseRef..child(key).setValue(newPerson);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用removeValue()从FirebaseDatabase中删除节点 - Can't delete node from FirebaseDatabase using removeValue() 如何从当前用户获取FirebaseDataBase快照? - How to get FirebaseDataBase Snapshot from current user? 从Android上的FirebaseDatabase获取位图时出错 - Error to get Bitmap from FirebaseDatabase on Android 我无法从 FirebaseDatabase 获取数据并将其放入 TextView (Kotlin)?我是否使用了不正确的方法来执行此操作? [等候接听] - I can not get the data from FirebaseDatabase and put it into TextView (Kotlin)?Did i used the incorrect way to do this? [on hold] 无法将 java.lang.Long 类型的 object 转换为类型(模型类); 从 firebaseDatabase android kotlin 获取数据 - Can't convert object of type java.lang.Long to type(Model class); Fetching Data from firebaseDatabase android kotlin 从FirebaseDatabase中检索所有用户 - Retrieving all users from the FirebaseDatabase 如何从 firebaseDatabase 获取 Url 作为字符串文本传递给另一个活动作为 url? - How to get Url from firebaseDatabase as string text pass to another activity as url? 我无法获取Firebase数据库的密钥 - I can't get the key of firebase database 如何将 firebaseStorage 中的图像路径获取到 firebaseDatabase - how to get an image path in firebaseStorage to firebaseDatabase 无法获取Google Maps API密钥 - can't get google maps api key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM