2

I am developing an app in Kotlin and have already set up my Firebase database. I am able to write data to my database, however, I am unable to read data from it. Here is an image of my database :

Database Image

I would like to retrieve a list of all the headings in either Basics, Functions or Loops. For more context, this is an app to teach people how to code in Kotlin and under the progress tab, I'm basically listing all the topics which the user has completed in my app. So, how can I query my database to retrieve a list of all the headings listed under say Basics ? I would want preferably juts a list of strings containing strings Condition and Datatype. I just need the names of the topics which they have completed for that particular section (in this case is Basics). So basically, I need the child names of Basics, Functions and Loops.

My progress class:

package nus.is3261.kotlinapp
import com.google.firebase.database.IgnoreExtraProperties

@IgnoreExtraProperties
class Progress {
    var completed: String? = null

    constructor() {
        // Default constructor required for calls to DataSnapshot.getValue(Post.class)
    }

    constructor(time: String) {
        this.completed = time
    }

}

How I write data to my db:

/**
 * For firebase database
 */
private fun submitProgress(str: String, chapter: Int) {
    val currentUser = mAuth!!.currentUser
    // if user is not logged in, don't write
    currentUser?:return
    val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().time)
    val progress = Progress(time)
    when(chapter){
        1 -> mProgressReference!!.child(currentUser.uid!!).child("Progress").child("Basics").child(str).setValue(progress)
        2 -> mProgressReference!!.child(currentUser.uid!!).child("Progress").child("Loops").child(str).setValue(progress)
        3 -> mProgressReference!!.child(currentUser.uid!!).child("Progress").child("Functions").child(str).setValue(progress)
    }
}
Rajdeep
  • 2,246
  • 6
  • 24
  • 51
  • 1
    To understand better, you only want to display: `Basics`, `Functions` and `Loops`, that exist under the `Progress` node? – Alex Mamo Nov 04 '18 at 07:09
  • @AlexMamo Hi, I need the names of all children under `Basics`, `Functions` and `Loops`. I intend to have a `Chart` section, which displays completed topics. So, I intend to query the database to get the names of all completed topics. – Rajdeep Nov 04 '18 at 07:13
  • 1
    Ok, so what is the expected result? What are the exact values that you want to display? – Alex Mamo Nov 04 '18 at 07:15
  • @AlexMamo So expected is, I query my database and get a list of strings of all completed topics which are: `Condition`, `Data Type`, `Recursion`, `When`, `While`. I then use this data to plot a chart, telling users which chapter they have completed and not yet completed. – Rajdeep Nov 04 '18 at 07:18
  • @AlexMamo It is somewhat similar to your response here, where I only want the child names: https://stackoverflow.com/questions/43586595/get-child-names-in-firebase – Rajdeep Nov 04 '18 at 07:20
  • 1
    I understand, I'll write you an answer right now. – Alex Mamo Nov 04 '18 at 07:21

1 Answers1

3

According to your comment, to display the children under Basics, Functions and Loops, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference progressRef = rootRef.child("Users").child(uid).child("Progress");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
            for(DataSnapshot ds : dSnapshot.getChildren()) {
                String key = ds.getKey();
                Log.d(TAG, key);
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
progressRef.addListenerForSingleValueEvent(valueEventListener);

The result in your logcat will be:

Condition
Data Type
Recursion
When
While
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193