Android Firebase retrieve data from child node
<p>I'm trying to get the data from Firebase child node. But it throws <code>Can't convert object of type java.lang.String to type uk.co.stableweb.geethika.model.DailyVerse</code> exception.</p>

<p>This is the structure of my Firebase database. </p>

<p><a href="https://i.stack.imgur.com/wCXDx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wCXDx.png" alt="Firebase database structure."></a></p>

<p>Here is the code. I'm using Android Firebase new <a href="https://firebase.google.com/docs/database/android/retrieve-data" rel="nofollow noreferrer">documentation</a>.     </p>

<pre><code>mRef = FirebaseDatabase.getInstance().getReference().child("daily_verse");
mRef.keepSynced(true);

ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d("Child Event,Verse", dataSnapshot.getKey());

        dailyVerse = dataSnapshot.getValue(DailyVerse.class);

    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d("Child Event,Verse", "onChildChanged:" + dataSnapshot.getKey());

        dailyVerse = dataSnapshot.getValue(DailyVerse.class);
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w("Firebase", "onCancelled", databaseError.toException());
        Toast.makeText(context, "Failed to load verse",
                Toast.LENGTH_SHORT).show();
    }
};

mRef.addChildEventListener(childEventListener);
</code></pre>

<p>And Model class.</p>

<pre><code>public class DailyVerse {

    private String verse_title;
    private String verse_content;

    public String getVerseTitle() {
        return verse_title;
    }

    public void setVerseTitle(String verse_title) {
        this.verse_title = verse_title;
    }

    public String getVerseContent() {
        return verse_content;
    }

    public void setVerseContent(String verse_content) {
        this.verse_content = verse_content;
    }
}
</code></pre>

<p>And the LogCat.</p>

<pre><code>com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type uk.co.stableweb.geethika.model.DailyVerse
  at com.google.android.gms.internal.zzalq.zzd(Unknown Source)
  at com.google.android.gms.internal.zzalq.zzb(Unknown Source)
  at com.google.android.gms.internal.zzalq.zza(Unknown Source)
  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
  at uk.co.stableweb.geethika.VerseActivityFragment$1.onChildAdded(VerseActivityFragment.java:63)
  at com.google.android.gms.internal.zzahh.zza(Unknown Source)
  at com.google.android.gms.internal.zzajh.zzctc(Unknown Source)
  at com.google.android.gms.internal.zzajk$1.run(Unknown Source)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5461)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
</code></pre>

<p>But if I change the database reference as follows. And get the dataSnapshot value, it grabs the data.</p>

<pre><code>mRef = FirebaseDatabase.getInstance().getReference("daily_verse");

testVal = (String) dataSnapshot.getValue();
Log.d("TEST", testVal);

V/FA: Activity resumed, time: 278033062
D/Child Event,Verse added: verse_content
D/TEST: විශ්වාසකමේ මාර්ගය තෝරා ගතිමි. ඔබගේ විනිශ්චයන් මා ඉදිරියෙහි තබා ගතිමි.
D/Child Event,Verse added: verse_title
D/TEST: ගීතාවලිය 119:30
</code></pre>

<p>I think it is possible to get those values using HashMap. But there was a comment saying it is no longer recommended. </p>