Reading from a simple FireBase Database

前端 未结 3 463
不知归路
不知归路 2021-01-24 06:40

I\'m having some issues reading from a Firebase Database.

I have a pretty simple layout

{
  \"lot\" : {
    \"lot1\" : \"low\",
    \"lot2\" : \"low\",
          


        
3条回答
  •  遥遥无期
    2021-01-24 07:25

    There's some tweak in your code. Your

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getInstance().getReference();
    

    should be write like this,

    DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference database = myRef.child("anyValueNameYouSpecifyInConsole");
    

    Those 2 lines should be declare outside onCreate method. The one that you need to specify with addValueEventListener is the 2nd DatabaseReference, not the first one. So, it should looks like this from my example,

    database.addValueEventListener (new ValueEventListener) 
    

    and it will import method.

    If you wish the data to be displayed in a particular TextView, then just findViewById the TextView you wanna use and include it in onDataChange method like so,

    String x = dataSnapshot.getValue(String.class);
    textViewNameDeclared.setText(x);
    

    And don't forget to change the security rule for reading.

提交回复
热议问题