使用唯一键迭代Firebase Data Android

时间:2021-12-05 08:43:38

Using Firebase with a simple data structure, I am missing how to read my data containing unique keys. Keys are generated using:

使用具有简单数据结构的Firebase,我错过了如何读取包含唯一键的数据。使用以下方法生成密钥

String key = myRef.child("users").push().getKey();

String key = myRef.child(“users”)。push()。getKey();

and adding my data works fine with:

并添加我的数据工作正常:

myRef.child("users").child(userID).child("favorites").child(key).setValue(newSite);

My data is structured simply:

我的数据结构简单:

Users
 - UserID
  - Favorites
   - KEY : VALUE

I am able to see my data like so showData(dataSnapshot):

我能够看到我的数据,如showData(dataSnapshot):

for(DataSnapshot ds : dataSnapshot.getChildren()){
   String data = ds.child(userID).child("favorites").getValue().toString();
   Log.v("DATA", "DATA: " + data);
}

data returns: {-L-DpQQCX8GYKPWclOX4=test} for example. Are we then to iterate over the object returned? or am i doing this wrong with Firebase and should be getting each value somehow? If there is documentation on this, please share the link. I am not finding it for the random key.

数据返回:例如{-L-DpQQCX8GYKPWclOX4 = test}。我们是否要迭代返回的对象?或者我是否对Firebase做错了,应该以某种方式得到每个值?如果有相关文档,请分享链接。我没有找到随机密钥。

EDIT:::

编辑:::

My listener is:

我的听众是:

 myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            showData(dataSnapshot);
        }

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

I was able to see the key data by parsing the object:

通过解析对象,我能够看到关键数据:

JSONObject json = null;
            try {
                json = new JSONObject(data);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            for(Iterator<String> iter = json.keys();iter.hasNext();) {
                String key = iter.next();
                Log.e("KEY",key);
                array.add(key);
            }

1 个解决方案

#1


0  

To get data from a Firebase database you don't need to use JSONObject. You can simply attach a listener and iterate over the dataSnapshot object using getChildren() like this:

要从Firebase数据库获取数据,您不需要使用JSONObject。你可以简单地附加一个监听器并使用getChildren()迭代dataSnapshot对象,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference favoritesRef = rootRef.child("users").child(userID).child("favorites");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String key = ds.getKey();
            String value = ds.getValue(String.class);
            Log.d("TAG", key + value);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
favoritesRef.addListenerForSingleValueEvent(eventListener);

Using this code, you can get all keys and values from favorites location.

使用此代码,您可以从收藏夹位置获取所有键和值。

#1


0  

To get data from a Firebase database you don't need to use JSONObject. You can simply attach a listener and iterate over the dataSnapshot object using getChildren() like this:

要从Firebase数据库获取数据,您不需要使用JSONObject。你可以简单地附加一个监听器并使用getChildren()迭代dataSnapshot对象,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference favoritesRef = rootRef.child("users").child(userID).child("favorites");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String key = ds.getKey();
            String value = ds.getValue(String.class);
            Log.d("TAG", key + value);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
favoritesRef.addListenerForSingleValueEvent(eventListener);

Using this code, you can get all keys and values from favorites location.

使用此代码,您可以从收藏夹位置获取所有键和值。