【问题标题】:How to get this nested data from firebase database如何从 firebase 数据库中获取这些嵌套数据
【发布时间】:2019-08-23 17:55:13
【问题描述】:

我正在尝试从 firebase 数据库中获取这些嵌套数据。我制作了三个类 Company 、 Vehicle 和 Destination。我想检索此数据的任何建议。我只检索公司信息,而不是它的两个孩子 Vehicles 和 Destinations。

公共类 MainActivity 扩展 AppCompatActivity {

private DatabaseReference myRef;
private TextView tvID, tvCompanyName, tvCompanyAddress, tvCompanyContact, tvCompanyStatus, tvCarNumber;
private String startingPoint, endingPoint, fare, car, carModel, carNumber;
private Button btnGo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvID = findViewById(R.id.tvID);
    tvCompanyName = findViewById(R.id.tvCompanyName);
    tvCompanyAddress = findViewById(R.id.tvCompanyAddress);
    tvCompanyContact = findViewById(R.id.tvCompanyContact);
    tvCompanyStatus = findViewById(R.id.tvCompanyStatus);
    tvCarNumber = findViewById(R.id.tvCarNumber);

myRef=FirebaseDatabase.getInstance().getReferenceFromUrl("https://travel-cdae5.firebaseio.com/company/companies");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                try {
                        Company company = dataSnapshot.getValue(Company.class);
                        Destination destination = dataSnapshot.getValue(Destination.class);
                        tvID.setText(company.getId());
                        tvCompanyName.setText(company.getName());
                        tvCompanyAddress.setText(company.getAddress());
                        tvCompanyContact.setText(company.getContact());
                        tvCompanyStatus.setText(destination.getFare());
                } catch (Exception exp) {
                    Log.e("Message", exp.getMessage());
                    Toast.makeText(MainActivity.this, exp.getMessage()
                            , Toast.LENGTH_LONG).show();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            Toast.makeText(MainActivity.this, error.getMessage()
                    , Toast.LENGTH_SHORT).show();
        }
    });
}
}

【问题讨论】:

  • 分享你的数据库结构
  • 您想要获得的确切数据是什么?请给我们一个例子。

标签: android firebase


【解决方案1】:

Destinations 是 company 节点的子节点,这就是为什么您必须使用 DataSnapshot destinationsSnapshot = dataSnapshot.child("destinations").getValue(); 将其作为子节点获取的原因

然后你需要遍历返回的目的地。

代码:

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            try {
            Company company = dataSnapshot.getValue(Company.class);
            tvID.setText(company.getId());
            tvCompanyName.setText(company.getName());
            tvCompanyAddress.setText(company.getAddress());
            tvCompanyContact.setText(company.getContact());
            DataSnapshot destinationsSnapshot = dataSnapshot.child("destinations").getValue();
                    // Now iterate over the destinations 
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                if (snapshot.exists()) {
                    if (snapshot.getKey().equals(0)){
                        Destination destination = snapshot.getValue(Destination.class);
                        tvCompanyStatus.setText(destination.getFare());
                    }
                }
            }
            } catch (Exception exp) {
                Log.e("Message", exp.getMessage());
                Toast.makeText(MainActivity.this, exp.getMessage()
                        , Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {
        Toast.makeText(MainActivity.this, error.getMessage()
                , Toast.LENGTH_SHORT).show();
    }
});

【讨论】:

  • 我只想获取索引[0]的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 2020-02-26
  • 2023-02-02
  • 2017-01-18
  • 1970-01-01
相关资源
最近更新 更多