user3757775 user3757775 - 2 months ago 9
Android Question

Setting name for list in class on firebase

I am using firebase in an android application where I have a house that has a list of rooms. I am trying to find out how to use the name of the room as the value for the child rather than the 0 and 1.

The database structure is below

Database Structure

Answer Source

You need to use a Map<StringRoomName,RoomDetailsClass> this will show StringRoomName in place of 0 and RoomDetailsClass as json object value. where StringRoomName = String

   class Room{
    //your other members
    Map<String,RoomDetailsClass> roomMap;
    // gettes and setters or constructor for initialization

    class RoomDetailsClass{

    //your room details

    }
  }

and upload it using

if (isDataValidatied()) {
                mDatabase.push().setValue(roomObject, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                        if (databaseError == null) {
                            makeToast("One entry created successfully");
                            clearData();
                        } else {
                            makeToast("data creation failed");
                        }
                    }
                });

            } else {
                makeToast("Please check data entered");
            }