简体   繁体   中英

Retrieve current user's children data : Firebase

In our app, we have to retrieve the names of the diaries under the current user and display them in a ListView .

We have referred to a number of questions similar to our one, but none of them came handy. Any help would be appreciated. Thanks in Advance!

In the Firebase structure shown in the image:

  1. A user can have any number of diaries (say user in this pic has 3 diaries, namely Hyderabad, Dubai and Toronto

  2. Under every diary, there are images along with experience of the user.

  3. Aim is to get just the diary names of the user under consideration

Under each of these diaries, there are images (we are able to retrieve the images into a listview ; a separate one displayed in a fragment)

The expected output is - A new ListView that should be populated with the diary names - Hyderabad, Chennai and Bangalore image

image

According to your last comment, to get all city names in a ListView , please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("image").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> cities = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String cityName = ds.getKey();
            cities.add(cityName);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, cities);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

The result in your ListView will be:

Dubai
Hyderabad
Toronto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM