Eduard
Eduard

Reputation: 141

How can i assign a name to items in an array list in firebase database

I want to be able to give names to items in an array list, and use those names in my firebase real time database. Currently, i have a setup where i have a 'ProgressWeek' object created if the user is in a different week than before. To this object i am constructing it like this:

myRef.child(week_year).setValue(new ProgressWeek(weekName, days));

'weekName' is a String variable that contains the name of the week, i.e '16 April - 23 April'. 'days' is an ArrayList. a ProgressDay is constructed like this:

days.add(new ProgressDay(name));

'name' is the name of that day, i.e 'Thu, 22-04'.

The problem i have is that when storing this in Firebase Realtime Database i get the following: enter image description here

'days' represents the ArrayList and 'weekName' is the String var given when constructing a new ProgressWeek object. As you can see days are sorted via indexing, or the order they were added to the list. This is not ideal because if i want data from day 'Fri, 16-04', i will have to iterate through the all of the days, and only get the data where dayName.equals("Fri, 16-04"). This will also require the making of different listeners.

What i want is to be able to use something like below in order to get the data that i want.

myRef.child("days").child(theDateOfToday).child("theDataThatIWant").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.getValue() != null)
                {
                    myData = snapshot.getValue(Integer.class);
                }
            }

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

            }
        });

The reason why i am using Objects to feed into the database, is because in the future i am planning on using a RecyclerView to display, weeks, and all the days in that week.

ProjectWeek Class:

import java.util.ArrayList;

public class ProgressWeek {
    private String weekName;
    private ArrayList<ProgressDay> days;

    ProgressWeek()
    {

    }

    ProgressWeek(String weekName, ArrayList<ProgressDay> days)
    {
        setWeekName(weekName);
        setDays(days);
    }

    public String getWeekName() {
        return weekName;
    }

    public void setWeekName(String weekName) {
        this.weekName = weekName;
    }

    public ArrayList<ProgressDay> getDays() {
        return days;
    }

    public void setDays(ArrayList<ProgressDay> days) {
        this.days = days;
    }
}

What i want in my DB as JSON:

{

"users" : { "exmapleUserID" : { "userData1" : 68, "userData2" : 49, "userData3" : 5, "weeks" : { "16" : { "days" : [ { "dayName" : "Sat, 17-04", "dataAboutDay" : 0 }, { "dayName" : "Sun, 18-04", "dataAboutDay" : 0 }, { "dayName" : "Mon, 19-04", "dataAboutDay" : 0 }, { "dayName" : "Tue, 20-04", "dataAboutDay" : 0 }, { "dayName" : "Wed, 21-04", "dataAboutDay" : 0 }, { "dayName" : "Thu, 22-04", "dataAboutDay" : 0 } ], "weekName" : "16 April - 23 April" } }, "userData4" : 60, "userData5" : 40, "username" : "exampleUser" } } }

NOTE: I want days to be IDed using their dayName.

Upvotes: 0

Views: 201

Answers (1)

DIGI Byte
DIGI Byte

Reputation: 4163

Firebase realtime database converts an array into an ordered list by its index, to change this you will have to convert the array to an object map or at the very least change the way it is generated to avoid having to remap it.

A common solution is a hashmap:

// Write a message to the database
Database = FirebaseDatabase.getInstance();
DbRef = Database.getReference("Path/To/Location");

//Writing HashmapMap<String, Object> 
HashMap<String, Object> days = new HashMap<>();
days.put("Name1/SubName1", "Value A");
days.put("Name1/SubName2", "Value B");
DbRef.updateChildren(days);

Upvotes: 1

Related Questions