0

How to Get Child values from "restaurants" in android Firebase ? I am getting null values

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    database.getReference().child("restaurants").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<RestaurantDetails> foodItems = new ArrayList<>();
            Iterable<DataSnapshot> iterable = dataSnapshot.getChildren();
            for(DataSnapshot child : iterable){
                foodItems.add(
                        new RestaurantDetails(
                                child.getKey(),
                                child.child("name").getValue().toString(),
                                selectedCategory,
                                Double.parseDouble(child.child("ratings").getValue().toString()),
                                child.child("imageurl").getValue().toString(),
                                child.child("description").getValue().toString(),
                                Integer.parseInt(child.child("count").getValue().toString())
                        )
                );
                Log.d(child.getKey(),child.getValue().toString());
            }
            recyclerView.setAdapter(shoplistItemArrayAdapter);
            progressDialog.cancel();
        }

enter image description here

KENdi
  • 7,576
  • 2
  • 16
  • 31
Kaliraj
  • 11
  • 1
  • 7

1 Answers1

0

There is no need to create a new object of RestaurantDetails class because you are getting it from the database. So to solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference restaurantsRef = rootRef.child("restaurants");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            RestaurantDetails restaurantDetails = ds.getValue(RestaurantDetails.class);
            Log.d("TAG", restaurantDetails.getDescription());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
restaurantsRef.addListenerForSingleValueEvent(valueEventListener);

The output in your logcat will be:

Biryani,Chinese,North Indian,South India
Indian,Chines,Byran

If you want to use the String class instead of RestaurantDetails, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference restaurantsRef = rootRef.child("restaurants");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String description = ds.child("description").getValue(String.class);
            Log.d("TAG", description);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
restaurantsRef.addListenerForSingleValueEvent(valueEventListener);

The output will be the same.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks @Alex Mamo for reply..i tried to implement in recyclerView with ItemArray Adapter..still i am getting null value – Kaliraj Jul 05 '18 at 16:39
  • Using my code, do you get the desired values in your logcat? – Alex Mamo Jul 05 '18 at 16:41
  • yes..!! i get the values from logcat but how to add the value to the recylerView ? – Kaliraj Jul 05 '18 at 17:06
  • Good to hear that. In this case, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Jul 05 '18 at 17:11
  • Thanks @Alex , i will inform you once i Complete the RecyclerView – Kaliraj Jul 05 '18 at 17:21
  • Ok @Kaliraj Keep me poested. – Alex Mamo Jul 05 '18 at 17:22
  • hi @Alex , i check that code..little confusion in creating the adapter class..any idea ? – Kaliraj Jul 06 '18 at 05:13
  • little confusion like what? What is the issue? – Alex Mamo Jul 06 '18 at 06:34
  • I don't know how to create a adapter class based on fire base values to assign @Alex – Kaliraj Jul 06 '18 at 06:37
  • Please follow the exact steps that are explained in that post. You should make your own attempt given the information in that post, and ask another question if something else comes up, so me or other users can help you. – Alex Mamo Jul 06 '18 at 07:54
  • okay @Alex now recylerview works but it display only data from HotKitchen ? and how to display each shop wise details in recylerview? – Kaliraj Jul 06 '18 at 08:15
  • 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 Jul 06 '18 at 08:19