如何从Fire Base到Android文本视图检索数据?

时间:2018-10-06 19:48:20

标签: android firebase-realtime-database data-retrieval

我正在开发一个物联网项目,我想将数据从Firebase传输到我的android应用程序的textview中,以帮助我!!

这是我的代码

code

Firebase JSON

{
  "Water_level" : 10,
  "valve_1" : 0,
  "valve_2" : 0
}

这是我的logcat错误

10-07 13:49:26.433 25077-25077/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.blogspot.techyfruit360.watercontroller, PID: 25077
                                                   com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
                                                       at com.google.android.gms.internal.zzelw.zzb(Unknown Source:663)
                                                       at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
                                                       at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
                                                       at com.blogspot.techyfruit360.watercontroller.Main2Activity$1.onDataChange(Main2Activity.java:40)
                                                       at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
                                                       at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
                                                       at com.google.android.gms.internal.zzeig.run(Unknown Source:65)
                                                       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:6518)
                                                       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)

onDataChange中删除您尝试编写的所有内容

myRef.addListenerForSingleValueEvent( new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot){
        if(dataSnapshot.exists()){
            Map<String, Object> objectMap = (HashMap<String, Object>) dataSnapshot.getValue();
            String value = (String) objectMap.get( "Water_level" );
            Log.d("Water_level", "Value is: " + value);
            textView.setText(value);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
} );

这应该可以解决您的问题。确保您的数据库引用设置正确。

答案 1 :(得分:0)

  

无法将类型为java.lang.Long的值转换为String

在您的onDataChange()内,

String value = datasnapshot.getValue(String.class)

它将long的值转换为String,这就是您收到错误的原因。

请尝试以下操作:

String waterLevel = dataSnapshot.child("Water_level").getValue(String.class);
textView.setText(waterLevel)

或者只使用toString()