【问题标题】:How to show data in textview from firebase如何从firebase在textview中显示数据
【发布时间】:2020-01-14 09:56:44
【问题描述】:

我还在学习编程。我在将我的数据从 firebase 显示到 textview 时遇到问题。我的输出没有显示数据库中的值。这是我的实时数据库db realtime

这是我的java代码:

public class MainActivity extends AppCompatActivity {

    TextView status,water,steam,inlet,outlet;
    String istatus,iwater,isteam,iinlet,ioutlet;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // address the text
        status = findViewById(R.id.a);
        water = findViewById(R.id.b);
        steam = findViewById(R.id.c);
        inlet = findViewById(R.id.d);
        outlet = findViewById(R.id.e);

        final DatabaseReference ref =FirebaseDatabase.getInstance().getReference().child("ESP32_SADS");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                status.setText(istatus);
                water.setText(iwater);
                steam.setText(isteam);
                inlet.setText(iinlet);
                outlet.setText(ioutlet);

                istatus = dataSnapshot.child("Status").getValue().toString();
                iwater = dataSnapshot.child("Water").getValue().toString();
                isteam = dataSnapshot.child("Steam").getValue().toString();
                iinlet = dataSnapshot.child("Inlet").getValue().toString();
                ioutlet = dataSnapshot.child("Outlet").getValue().toString();
            }

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

            }
        });
    }

我的输出如下所示:

【问题讨论】:

  • 您好,您可以调试并检查您是否从数据库中获取数据吗?为 onDataChange() 和 onCancelled() 写入日志。看看哪个被调用了。
  • 我该怎么做?

标签: java android firebase-realtime-database textview


【解决方案1】:

只需从 db 中获取值,然后将其设置到您的 textView 中

  public class MainActivity extends AppCompatActivity {

    TextView status,water,steam,inlet,outlet;
    String istatus,iwater,isteam,iinlet,ioutlet;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // address the text
        status = findViewById(R.id.a);
        water = findViewById(R.id.b);
        steam = findViewById(R.id.c);
        inlet = findViewById(R.id.d);
        outlet = findViewById(R.id.e);

        final DatabaseReference ref 
        =FirebaseDatabase.getInstance().getReference().child("ESP32_SADS");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                // your variables become have value then put it to your text
                istatus = dataSnapshot.child("Status").getValue().toString();
                iwater = dataSnapshot.child("Water").getValue().toString();
                isteam = dataSnapshot.child("Steam").getValue().toString();
                iinlet = dataSnapshot.child("Inlet").getValue().toString();
                ioutlet = dataSnapshot.child("Outlet").getValue().toString();

                // set values in textViews
                status.setText(istatus);
                water.setText(iwater);
                steam.setText(isteam);
                inlet.setText(iinlet);
                outlet.setText(ioutlet);
            }

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


            }



        });
    }

你的代码写错了,你先写再读

【讨论】:

  • 感谢您的回答...我更改了代码但仍然相同
  • 测试它在firebase db中更改值以输入此方法 public void onDataChange(@NonNull DataSnapshot dataSnapshot) @syahmi ami
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 2017-01-06
  • 2017-01-31
  • 2014-06-19
  • 1970-01-01
相关资源
最近更新 更多