Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何访问firebase实时数据库中的子值_Android_Firebase Realtime Database - Fatal编程技术网

Android 如何访问firebase实时数据库中的子值

Android 如何访问firebase实时数据库中的子值,android,firebase-realtime-database,Android,Firebase Realtime Database,我有一个存储用户地址的实时firebase数据库。我想获取这些值并将它们分配给android布局中的textview。我尝试过这样访问数据: String city = (mDatabase.getDatabase().getReference().child(user_data.getString("uidkey", null)).child("city")).toString(); _cityText.setText(city); 但这只给出了街道的firebase URL

我有一个存储用户地址的实时firebase数据库。我想获取这些值并将它们分配给android布局中的
textview
。我尝试过这样访问数据:

String city = (mDatabase.getDatabase().getReference().child(user_data.getString("uidkey", null)).child("city")).toString();
        _cityText.setText(city);
但这只给出了街道的firebase URL,而并没有给出实际值。我如何检索这些数据


在firebase中获取条目数据的推荐方法是创建表示要从DB中提取的对象的类,确保该类的所有成员与要提取的数据的键完全匹配(例如,您将有一个包含5个字符串的对象,城市、国家、州、街道、邮政编码)并确保所有这些对象都有setter和getter(只需自动生成它们)

然后,您可以为数据库引用添加一个侦听器,如下所示:

mDatabase.child("Addresses").child(user_data.getString("uidkey", null)).addListenerForSingleValueEvent(
        new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Get the address object
                Address address = dataSnapshot.getValue(Address.class);
                // get the string here, using address.getCity() or whatever you called the getter

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
您还可以使用相同的地址类向firebase添加新的地址对象


此外,如果您正在填充列表视图,那么如果您使用firebaseListAdapter,这可能会更简单、更干净。这就是我所写的:我已经编写了相同的代码,并将其放入onStart()中;我使用字符串street=addess.getStreet()。但是,这会导致应用程序因空异常而崩溃。这是我正在使用的地址类,上面的地址类是我如何获取它的。请参阅对我答案的编辑。您需要在用户ID子项之前添加“地址”作为DB引用的子项。欢迎使用。请随意选择正确的答案!可能重复的
public class Address {

    public String street;
    public String city;
    public String state;
    public String zip_postal_code;
    public String country;

    public Address (){};

    public Address(String street, String city, String state, String zip_postal_code, String country ){
        this.street = street;
        this.city = city;
        this.state = state;
        this.zip_postal_code = zip_postal_code;
        this.country = country;
    }

    public String getStreet (){return street;}
    public String getCity (){return city;}
    public String getState (){return state;}
    public String getZipPostalCode (){return zip_postal_code;}
    public String getCountry (){return country;}

}
mDatabase.child("Addresses").child(user_data.getString("uidkey", null)).addListenerForSingleValueEvent(
        new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Get the address object
                Address address = dataSnapshot.getValue(Address.class);
                // get the string here, using address.getCity() or whatever you called the getter

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });