I\'m trying to get the data from Firebase child node. But it throws Can\'t convert object of type java.lang.String to type uk.co.stableweb.geethika.model.DailyVerse
By this method
mRef.addChildEventListener(childEventListener);
you subscribe for
.child("daily_verse");
children. daily_verse has two children: verse_content and verse_title. The type of these children is String. So, you get two String datasnapshot in the onChildAdd listener.
Update: Try something like this:
mRef = FirebaseDatabase.getInstance().getReference();
mRef.keepSynced(true);
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d("Child Event,Verse", dataSnapshot.getKey());
if ("daily_verse".equal(dataSnapshot().getKey){
dailyVerse = dataSnapshot.getValue(DailyVerse.class);
Log.d("TEST","Object: "+dailyVerse);
if (dailyVerse!=null){
Log.d("TEST","values: "+dailyVerse.verse_title+", "+dailyVerse.verse_content);
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d("Child Event,Verse", "onChildChanged:" + dataSnapshot.getKey());
if ("daily_verse".equal(dataSnapshot().getKey){
dailyVerse = dataSnapshot.getValue(DailyVerse.class);
Log.d("TEST","Changed object: "+dailyVerse);
if (dailyVerse!=null){
Log.d("TEST","Changed values: "+dailyVerse.verse_title+", "+dailyVerse.verse_content);
}
}
}
@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);
You should subscribe to events from parent object and check the key at events listener.
ArrayList<EventsResponse> usersEventsList = new ArrayList<>();
dummyIdsArrayList = new ArrayList<>();
Iterator<DataSnapshot> eventsIterator = dataSnapshot.getChildren().iterator();
while (eventsIterator.hasNext()) {
DataSnapshot eventsSnapShot = eventsIterator.next();
EventsResponse eventsResponse = new EventsResponse();
eventsResponse.setEventName(eventsSnapShot.getKey());
Iterator<DataSnapshot> eventChildIterator = eventsSnapShot.getChildren().iterator();
while (eventChildIterator.hasNext()) {
DataSnapshot eventchildSnapshot = eventChildIterator.next();
if (eventchildSnapshot.getKey().equals("is_recursive")) {
eventsResponse.setRecursive((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("is_notif")) {
eventsResponse.setNotif((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("is_editable")) {
eventsResponse.setEditable((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("is_visible")) {
eventsResponse.setVisible((Boolean) eventchildSnapshot.getValue());
} else if (eventchildSnapshot.getKey().equals("name")) {
eventsResponse.setName(eventchildSnapshot.getValue().toString());
} else if (eventchildSnapshot.getKey().equals("date")) {
Iterator<DataSnapshot> eventChildDateIterator = eventchildSnapshot.getChildren().iterator();
while (eventChildDateIterator.hasNext()) {
DataSnapshot eventchildDateSnapshot = eventChildDateIterator.next();
if (eventchildDateSnapshot.getKey().equals("day")) {
eventsResponse.setDay(eventchildDateSnapshot.getValue().toString());
} else if (eventchildDateSnapshot.getKey().equals("month")) {
eventsResponse.setMonth(eventchildDateSnapshot.getValue().toString());
} else if (eventchildDateSnapshot.getKey().equals("year")) {
eventsResponse.setYear(eventchildDateSnapshot.getValue().toString());
}
}
} else {
eventsResponse.setNotif(true);
eventsResponse.setVisible(false);
}
}
if you are not able to fetch data,then you can fetch the data in that format.
When using a firebase model class, You have to include an empty constructor for firebase to work with.
Try adding it as
public class DailyVerse {
public DailyVerse(){
}
}
DataRef = FirebaseDatabase.getInstance().getReference().child("Users");
DataRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String acctname = (String)dataSnapshot.child("Name").getValue();
String acctmail = (String)dataSnapshot.child("Email").getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Pay careful attention to the names of your properties in the JSON and your fields/getters in the Java code. If they don't match, the Firebase client won't be able to match the values between them.
Your JSON has verse_title
and verse_content
, so your Java class must be either:
public class DailyVerse {
public String verse_title;
public String verse_content;
}
or:
public class DailyVerse {
private String verse_title;
private String verse_content;
public String getVerse_title() {
return verse_title;
}
public void setVerse_title(String verse_title) {
this.verse_title = verse_title;
}
public String getVerse_content() {
return verse_content;
}
public void setVerse_content(String verse_content) {
this.verse_content = verse_content;
}
}
Since Firebase Android SDK 9.2, you can also annotate your Java code to map to the correct JSON properties. So a third way to get the correct mapping is:
public class DailyVerse {
private String verse_title;
private String verse_content;
@PropertyName("verse_title")
public String getVerseTitle() {
return verse_title;
}
public void setVerseTitle(String verse_title) {
this.verse_title = verse_title;
}
@PropertyName("verse_content")
public String getVerseContent() {
return verse_content;
}
public void setVerseContent(String verse_content) {
this.verse_content = verse_content;
}
}
I haven't tried that latest variant though, so let me know if there are typos on the answer.