Reputation: 17
public void retriveData(){
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.e("Count " ,""+dataSnapshot.getChildrenCount());
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Info post=new Info();
Log.e("EEEERRRORRR", "onChildAdded: "+postSnapshot.toString() );
Info post=postSnapshot.getValue(Info.class);
Log.e("Get Data", post.getQuestion());
}
}
This is the error I get:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sahilliolu.rqagame, PID: 3006
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.sahilliolu.rqagame.Info
Everything works fine until setQuestion(). I get the correctChildrenCont in log.e. How can I fix this? For more reference I am pasting my Info class which is a simple class that has string arguments in its constructor
public class Info {
public String answer,question,a,b,c,d;
public Info(String question,String answer,String a,String b,String c,String d){
this.question=question;
this.a=a;
this.b=b;
this.c=c;
this.d=d;
this.answer=answer;
}
public Info(){
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
}
Upvotes: 0
Views: 263
Reputation: 907
You should create object using datasnapshot of required class like
ClassName post = postSnapshot.getValue(ClassName.class);
and your class should contain same variables as they are in database
ex your database contains two columns Name and Age
then your Class should contain Name and Age as variable and you should create getter, setter and constructor
By getting data using this method your data(questions) will already be setted to your variables.
see this for more info
Upvotes: 1