从Firebase检索多个条目

时间:2018-11-02 11:04:21

标签: java android firebase firebase-realtime-database

enter image description here

大家好。我正在尝试检索01-Nov-201802-Nov-2018下的ID,但无法这样做。我已经尝试过this问题,但无法获取数据。我的代码是:

ListView listView;
ArrayList<String> list = new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReferenceFromUrl("https://myapplication-340c0.firebaseio.com/").child("Teacher").child("batch 1").child("C7-Advanced Java");//.child("02-Nov-2018");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_data);
    listView = findViewById(R.id.list);
    myRef.addValueEventListener(new ValueEventListener() {
        public void onDataChange(DataSnapshot dataSnapshot) {
            collectPhoneNumbers((Map<String, Object>) dataSnapshot.getValue());

        }

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

        }


    });


}
private void collectPhoneNumbers(Map<String,Object> users) {

    ArrayList phoneNumbers = new ArrayList<>();

    //iterate through each user, ignoring their UID
    for (Map.Entry<String, Object> entry : users.entrySet()){

        //Get user map
        Map singleUser = (Map) entry.getValue();
        //Get phone field and append to list
        phoneNumbers.add( singleUser.get("02-Nov-2018").toString());
    }
    Log.d("data from db",phoneNumbers.toString());
//listView.setAdapter(adapter);
    System.out.println(phoneNumbers.toString());
}

Log.d出现错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at com.example.root.connieapp.ViewData.collectPhoneNumbers(ViewData.java:57)
    at com.example.root.connieapp.ViewData.access$000(ViewData.java:19)
    at com.example.root.connieapp.ViewData$1.onDataChange(ViewData.java:33)
  

我必须在firebase中添加更多值,并且其中会有很多。我如何获得许多价值?   请帮助我解决这个问题。

我重用了代码,但没有重命名变量。请不要介意..谢谢

1 个答案:

答案 0 :(得分:0)

要解决此问题,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Teacher").child("batch 1").child("C7-Advanced Java");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String date = ds.getKey();
            String code = ds.child("Code").getValue(String.class);
            String fams = ds.child("fams").getValue(String.class);
            String jams = ds.child("jams").getValue(String.class);
            Log.d(TAG, date + " / " + code + " / " + fams + " / " + jams);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
ref.addListenerForSingleValueEvent(valueEventListener);

您的logcat中的输出将是:

01-Nov-2018 / jam /p / p
02-Nov-2018 / sam /p / p