1

When I uploaded an ArrayList of objects in my firebase database with setValue,

It was stored this way:database screenshot

And now I'm wondering if I could retrieve this ArrayList from the database to use the information in it to populate a listview.

public class Alarm {

    private String alarm_time;
    private String alarm_ampm;
    private String alarm_days;

    public void setAlarm_time(String time) {
        alarm_time = time;
    }

    public void setAlarm_ampm(String ampm) {
        alarm_ampm = ampm;
    }

    public void setAlarm_days(String days) {
        alarm_days = days;
    }

    public String getTime() {
        return this.alarm_time;
    }

    public String getAmpm() {
        return this.alarm_ampm;
    }

    public String getDays() {
        return this.alarm_days;
    }
}

This is what an Alarm looks like, and the Alarms are stored in the ArrayList that I uploaded to the database.

Is there a way to retrieve an ArrayList directly from firebase database? Or is there any library that might help me out?

1 Answer 1

1

Try this

private void getAlarmLlist() {
        final List<Alarm> alarmList=new ArrayList();
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("alarm_list");
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                try {
                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        Alarm alarm= postSnapshot.getValue(Alarm.class);
                        alarmList.add(alarm);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.w(TAG, "Error: ", databaseError.toException());
            }
        });
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.