Xnkr
Xnkr

Reputation: 562

Retrieving nested data in Firebase - Android

I've just entered into the world of firebase and android. I followed online tutorials to set and get non-nested data and display those in a listview as follows:

Before Data:

Before Data

    mListView = (ListView) findViewById(R.id.ListView) ;
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStudents);
    mListView.setAdapter(arrayAdapter);
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    myRef = database.getReference().child("Student1");
    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String value = dataSnapshot.getValue(String.class);
            String key = dataSnapshot.getKey();
            String totl = key + ": " + value;
            mStudents.add(totl);
            arrayAdapter.notifyDataSetChanged();
        }
    });

I added a Grades field with sub-fields to the database. I want to display the entire student1 data along with the grades. When I try running the above code. I get the following error.

After Data:

After Data

FATAL EXCEPTION: main
              Process: com.adcpnmd.mystudents, PID: 3524
              com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
                  at com.google.android.gms.internal.zzbqi.zzaD(Unknown Source)
                  at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.adcpnmd.mystudents.Main$1.onChildAdded(Main.java:39)
                  at com.google.android.gms.internal.zzblz.zza(Unknown Source)
                  at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                  at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

What should I do for accessing the entire data and adding to my ListView ? Or Am I structuring the data incorrectly ?

Thanks in Advance.

Upvotes: 1

Views: 9046

Answers (1)

Valentino
Valentino

Reputation: 2135

Try to change your firebase code like this:

myRef = database.getReference().child("Student1").child("Grades");
myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        if (dataSnapshot.exists()) {
            String value = dataSnapshot.getValue(String.class);
            String key = dataSnapshot.getKey();
            String totl = key + ": " + value;
            mStudents.add(totl);
            arrayAdapter.notifyDataSetChanged();
        }
    }
});

Update

To access the entire node Student1, you can do this way:

First of all, I've renamed your Class field as Course, because Class could generate confusion and problems (it is used by Android itself). So create a Java file Student.java with the plain object that represents your data:

public class Student {

    private String Course;
    private String Name;
    private String Percentile;
    private HashMap<String,String> Grades;

    public Student() {

    }

    public Student(String course, String name, String percentile, HashMap<String, String> grades) {
        Course = course;
        Name = name;
        Percentile = percentile;
        Grades = grades;
    }

    public String getCourse() {
        return Course;
    }

    public String getName() {
        return Name;
    }

    public String getPercentile() {
        return Percentile;
    }

    public HashMap<String, String> getGrades() {
        return Grades;
    }
}

Next, to get the data, do this way:

ValueEventListener valueEventListener = new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Student student = dataSnapshot.getValue(Student.class);

                Log.d("TAG","name: "+student.getName());
                Log.d("TAG","percentile: "+student.getPercentile());
                Iterator it = student.getGrades().entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry pair = (Map.Entry)it.next();
                    Log.d("TAG","grade: "+pair.getKey() +  " = "  + pair.getValue());
                    it.remove(); // avoids a ConcurrentModificationException
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };

myRef = database.getReference().child("Student1");
myRef.addListenerForSingleValueEvent(valueEventListener);

Upvotes: 6

Related Questions