0

enter image description here

Hi everyone. i am trying to retrieve the ids under 01-Nov-2018 and 02-Nov-2018 but I am unable to do so. I have tried this question but unable to get the data. My Code is:

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());
}

there is an error at 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)

i have to add more values in firebase and there would be many of them. how can i get many values ?? Please help me to overcome this issue.

I have reused the code but didn't rename variables. please don't mind that.. thanks

1 Answers1

0

To solve this, please use the following lines of code:

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);

The output in your logcat will be:

01-Nov-2018 / jam /p / p
02-Nov-2018 / sam /p / p
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • i need to add more fields under date. how can i get them all without using keys? –  Nov 02 '18 at 12:04
  • Let's stick to one question at a time but as a hint, if you want to add more file files, please see the official documentation regarding on how to add data to a Firebase database. How can you get them all without using keys, in the exact way I showed you in my answer. There is no need to use any key, right? – Alex Mamo Nov 02 '18 at 12:07
  • i tried to ask the same question i said now.can you please point out to the referenced link where can i get that? –  Nov 02 '18 at 12:14
  • A referenced link where you can get what? – Alex Mamo Nov 02 '18 at 12:21
  • where i can get about how to get all data without using keys, –  Nov 02 '18 at 12:23
  • You can see in my answer above, how you can do that. Have you even try it? Do you get that output? – Alex Mamo Nov 02 '18 at 12:25
  • by all data i mean if i have more than those three values, let say 50 values. –  Nov 02 '18 at 12:26
  • In the same way you are getting the `jams` using `String jams = ds.child("jams").getValue(String.class);` you can get any other new value like this: `String otherValue = ds.child("otherValue").getValue(String.class);`. If you are reffering to the number of nodes, there is nothing to do, because `getChildren()` method will do the work for you, ok? – Alex Mamo Nov 02 '18 at 12:30
  • Hi Nancy! Is there everything alright, can I help you with other informations? – Alex Mamo Nov 03 '18 at 05:47