如何从 firebase 检索数据到 android 文本视图?

How to retrieve data from fire base to android text view?

我正在做一个物联网项目,我想将数据从 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.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)

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) {

    }
} );

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

Failed to convert value of type java.lang.Long to String

在你的onDataChange()

里面
String value = datasnapshot.getValue(String.class)

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

试试这个:

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

或者直接使用toString().