我正在构建一个社交应用,它将保存并从firebase中检索数据。但是我收到了这个错误
com.google.firebase.database.DatabaseException:无法转换对象 类型java.lang.String以键入com.test.consti.last.QuestionEntry 在com.google.android.gms.internal.zzear.zzb(未知来源) 在com.google.android.gms.internal.zzear.zza(未知来源) 在com.google.firebase.database.DataSnapshot.getValue(未知来源) 在 com.test.consti.last.FirebaseHelper.fetchData(FirebaseHelper.java:28) 在 com.test.consti.last.FirebaseHelper.access $ 000(FirebaseHelper.java:17) 在 com.test.consti.last.FirebaseHelper $ 1.onChildAdded(FirebaseHelper.java:36) 在com.google.android.gms.internal.zzdri.zza(未知来源) 在com.google.android.gms.internal.zzdwu.zzbvb(未知来源) 在com.google.android.gms.internal.zzdxa.run(未知来源) 在android.os.Handler.handleCallback(Handler.java:730) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:176) 在android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 在dalvik.system.NativeStart.main(本地方法)
这是我的FirebaseHelper。我可以从firebase数据库中检索数据(仅限#34;问题"孩子的内容)
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;
}
然后这是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 这是我的Acticity
这是我的活动
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; }
}
我已将内容发布到firebase
答案 0 :(得分:3)
将fetchData
方法更改为:
private void fetchData (DataSnapshot dataSnapshot){
questionEntries.clear();
QuestionEntry questionEntry = dataSnapshot.getValue(QuestionEntry.class);
questionEntries.add(questionEntry);
}
for
循环遍历节点questions
的子节点,并且由于子节点包含字符串,您将收到该错误。