How to retrieve the all the Electrician
data into a ListView
?
3 Answers
If you want to have a listview where each item in the listview contains some or all data of one of the electricians, you first need a datasnapshot of the node 'Electrician'. Also create an object named for example 'elecrician', which has the same data fields as you have in the firebase database. Then use the datasnapshot to create an array of 'electrician' objects similar to the answer of Ryan Pierce. Now that you have an array of electricians with their data, you can show the data that you want to display in a listview.
Besides, maybe it is easier to use time in milliseconds instead of the dates as strings if you want to calculate for example how long an employee has been working there (just easier to work with in my opinion). Also, the field 'JobTitle' might be overkill as the jobtitle is already stated by the node title, e.g. 'Electrician'.

- 221
- 5
- 24
Create list of objects (Electrician) from firebase and then create list view by passing the list of objects in the custom adapter.

- 99
- 1
- 9
Assuming you have a model class for your electrician object named Electrician
, to get a list of electrician objects, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference electricianRef = rootRef.child("Employee").child("Electrician");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Electrician> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Electrician electrician = ds.getValue(Electrician.class);
list.add(electrician);
Log.d("TAG", electrician.getLname());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
};
electricianRef.addListenerForSingleValueEvent(valueEventListener);
You can achieve this even simpler, using the String class and getting a list of electrician names for example:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference electricianRef = rootRef.child("Employee").child("Electrician");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String lName = ds.child("Lname").getValue(String.class);
list.add(lName);
Log.d("TAG", lName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
};
electricianRef.addListenerForSingleValueEvent(valueEventListener);

- 130,605
- 17
- 163
- 193
-
in the 8th line ist is showing required:java.lang.String found:com.example.hp.Electrician – Devajit Aug 09 '18 at 10:00
-
You're right. Sorry, my mistake. Please see my updated answer. Should be: `String lName = ds.child("Lname").getValue(String.class);`. – Alex Mamo Aug 09 '18 at 10:02
-
no in the second screenshot – Devajit Aug 09 '18 at 10:02
-
Yes, please see again. Does it work now? – Alex Mamo Aug 09 '18 at 10:02
-
I think the code works so thanks for that.. but i m not getting the output because of my own code may be i.e when i click on a recyclerview item (Electrician ) it just goes to next activity but does not view the listview where all the details from the data are retrived...i guess i have to change my code on onclicklistener in my recyclerview ...can u please suggest me a way to solve that – Devajit Aug 09 '18 at 10:26
-
Good to hear that it worked! This is basically another question. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Aug 09 '18 at 10:27