I'm building a social app that will save and retrieve data from firebase. But I'm getting this error
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.test.consti.last.QuestionEntry at com.google.android.gms.internal.zzear.zzb(Unknown Source) at com.google.android.gms.internal.zzear.zza(Unknown Source) at com.google.firebase.database.DataSnapshot.getValue(Unknown Source) at com.test.consti.last.FirebaseHelper.fetchData(FirebaseHelper.java:28) at com.test.consti.last.FirebaseHelper.access$000(FirebaseHelper.java:17) at com.test.consti.last.FirebaseHelper$1.onChildAdded(FirebaseHelper.java:36) at com.google.android.gms.internal.zzdri.zza(Unknown Source) at com.google.android.gms.internal.zzdwu.zzbvb(Unknown Source) at com.google.android.gms.internal.zzdxa.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at dalvik.system.NativeStart.main(Native Method)
This is my FirebaseHelper. There i'm to retrieve data from firebase database ( only "Questions" childs contents)
public class FirebaseHelper {
DatabaseReference db;
Boolean saved=null;
ArrayList<QuestionEntry> questionEntries =new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db=db;
}
private void fetchData (DataSnapshot dataSnapshot){
questionEntries.clear();
for (DataSnapshot ds :dataSnapshot.getChildren()){
//There is the error
QuestionEntry questionEntry = ds.getValue(QuestionEntry.class);
questionEntries.add(questionEntry);
}
}
public ArrayList<QuestionEntry> retreive (){
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return questionEntries;
}
Then this is QuestionEntry
public class QuestionEntry {
public String question_title;
public String question_id;
public String tag_id;
public String tag_name;
public String question_content;
public String question_date;
public String question_username;
public String interet;
public QuestionEntry(){
}
public void setQuestion_title(String question_title) {
this.question_title = question_title;
}
public void setQuestion_id(String question_id) {
this.question_id = question_id;
}
public void setTag_id(String tag_id) {
this.tag_id = tag_id;
}
public void setTag_name(String tag_name) {
this.tag_name = tag_name;
}
public void setQuestion_content(String question_content) {
this.question_content = question_content;
}
public void setQuestion_date(String question_date) {
this.question_date = question_date;
}
public void setQuestion_username(String question_username) {
this.question_username = question_username;
}
public void setInteret(String interet) {
this.interet = interet;
}
public String getQuestion_title() {
return question_title;
}
public String getQuestion_id() {
return question_id;
}
public String getTag_id() {
return tag_id;
}
public String getTag_name() {
return tag_name;
}
public String getQuestion_content() {
return question_content;
}
public String getQuestion_username() {
return question_username;
}
public String getInteret() {
return interet;
}
public String getQuestion_date() {
return question_date;
}
}
this is my firebase database structure this my Acticity
this is my Activity
public class Actu extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_actu, container, false);
lv = (ListView) v.findViewById(R.id.actu_rv);
db = FirebaseDatabase.getInstance().getReference("Questions");
helper = new FirebaseHelper(db);
adapter = new QuestionAdapter(getContext(),helper.retreive());
lv.setAdapter(adapter);
return v; }
}
I've already post the content to firebase