从Firebase数据库读取数据失败

时间:2018-02-04 16:58:47

标签: java android firebase firebase-realtime-database

我的Android应用程序有问题,我无法在Firebase数据库中设置带有字符串的textView。我想在我的JSON中用字符串设置TextView的文本。

mDatabaseReference.child("Profilo").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
           for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
                UserProfile mProfile = dataSnapshot1.getValue(UserProfile.class);
                nomeProfilo.setText(mProfile.getNome());
            }
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }@Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

这是我的Logcat

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.mysecurity.marco.mysecurty.UserProfile
                                                                                at com.google.android.gms.internal.zzbtg.zze(Unknown Source:89)
                                                                                at com.google.android.gms.internal.zzbtg.zzb(Unknown Source:145)
                                                                                at com.google.android.gms.internal.zzbtg.zza(Unknown Source:0)
                                                                                at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
                                                                                at com.mysecurity.marco.mysecurty.MaterialUserActivity$3.onChildAdded(MaterialUserActivity.java:194)
                                                                                at com.google.android.gms.internal.zzbox.zza(Unknown Source:33)
                                                                                at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source:2)
                                                                                at com.google.android.gms.internal.zzbra$1.run(Unknown Source:63)
                                                                                at android.os.Handler.handleCallback(Handler.java:790)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                at android.os.Looper.loop(Looper.java:164)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

 mDatabaseReference.child("Profilo").addListenerForSingleValueEvent(new ValueEventListener() {//to retrieve data
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
         for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
              String names=dataSnapshot1.child("nome").getValue().toString();
              nomeProfilo.setText(names);


                }
           }
         @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

或者您也可以这样做以防止使用循环迭代:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
mDatabaseReference.child("Profilo").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
      String names=dataSnapshot1.child("nome").getValue().toString();
              nomeProfilo.setText(names);


                }
           }
         @Override
        public void onCancelled(DatabaseError databaseError) {

        }
   });

答案 1 :(得分:1)

NoSQL数据库中的数据是JSON格式。所以从原始代码:

for (DataSnapshot child : dataSnapshot.getChildren()) {
    JSONObject json = new JSONObject(child.getValue(String.class));
    nomoProfilo.setText(json.getString("nome");
}

您可以从json的字段构建UserProfile对象。