无法从firebase获取Child

时间:2017-10-03 14:19:56

标签: android firebase firebase-realtime-database

我正在尝试从特定子节点检索数据,但无法检索。 我从之前的活动中检索了listview中的条形码编号。它在代码中标记为“bc”。正如您所看到的,我能够在toast中从数据库中的先前活动中检索密钥。我无法在数据库的相应Textview中显示任何内容。

以下是我检索条形码编号的方法(来自之前的java活动):

mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

                 //   Object key= dataSnapshot.getKey();
                    String myKeys ;
                    myKeys = mList.getItemAtPosition(i).toString();
                    String bc = myKeys.split("Barcode:")[1];
                    Intent intent = new Intent(Inventory.this,Edit.class);
                    intent.putExtra("value",bc);
                    startActivity(intent);
                }
            });

Pic of the Toast

Here 是我数据库的照片:

这是我的活动:

 public class Edit extends AppCompatActivity {
        TextView Barcode,Pname,EntryDate,ExpiryDate,Quantity;
        DatabaseReference database;
        String bc = null;`



        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_edit);
            Barcode=(TextView) findViewById(R.id.Barcode);
            Pname=(TextView) findViewById(R.id.Pname);
            EntryDate=(TextView) findViewById(R.id.EntryDate);
            ExpiryDate=(TextView) findViewById(R.id.ExpiryDate);
            Quantity=(TextView) findViewById(R.id.Quantity);
            bc = getIntent().getExtras().getString("value");
            database = FirebaseDatabase.getInstance().getReference().child("All Barcodes");

            Toast.makeText(Edit.this,bc,Toast.LENGTH_SHORT).show();


            database.child(bc).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {


                      for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                          String barcode = (String) snapshot.child("barcode").getValue();
                      Log.d("TAG", barcode);

                          String pname = (String) snapshot.child("pname").getValue();
                          String date = (String) snapshot.child("date").getValue();
                          String expiration = (String) snapshot.child("expiration").getValue();
                          String quantity = (String) snapshot.child("quantity").getValue();
                          Barcode.setText(barcode);
                          Pname.setText(pname);
                          EntryDate.setText(date);
                          ExpiryDate.setText(expiration);
                          Quantity.setText(quantity);

                      }


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    System.out.println("The read failed: " + databaseError.getMessage());

                }
            });


        }
    }

2 个答案:

答案 0 :(得分:0)

要解决此问题,请在onDataChange方法中添加以下所有声明:

TextView Barcode=(TextView) findViewById(R.id.Barcode);
TextView Pname=(TextView) findViewById(R.id.Pname);
TextView EntryDate=(TextView) findViewById(R.id.EntryDate);
TextView ExpiryDate=(TextView) findViewById(R.id.ExpiryDate);
TextView Quantity=(TextView) findViewById(R.id.Quantity);

您的代码应如下所示:

database.child(bc).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        TextView Barcode=(TextView) findViewById(R.id.Barcode);
        TextView Pname=(TextView) findViewById(R.id.Pname);
        TextView EntryDate=(TextView) findViewById(R.id.EntryDate);
        TextView ExpiryDate=(TextView) findViewById(R.id.ExpiryDate);
        TextView Quantity=(TextView) findViewById(R.id.Quantity);

        for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
            String barcode = (String) snapshot.child("barcode").getValue();
            String pname = (String) snapshot.child("pname").getValue();
            String date = (String) snapshot.child("date").getValue();
            String expiration = (String) snapshot.child("expiration").getValue();
            String quantity = (String) snapshot.child("quantity").getValue();
            Barcode.setText(barcode);
            Pname.setText(pname);
            EntryDate.setText(date);
            ExpiryDate.setText(expiration);
            Quantity.setText(quantity);

            Log.d("TAG", barcode);
      }
}

答案 1 :(得分:0)

尝试在for(DataSnapshot snapshot:dataSnapshot.getChildren())循环之前检查if(dataSnapshot.exists()),它将等待datasnap存在然后在你想要的字段中设置setText