1

To get it out of the way, yes there is a similar question on SO, but it doesn't explain exactly what my situation asks for I think.

In the Firebase Realtime Database, I have a json tree that looks something like this:

users{
   userid{
       templates{
          templateName1{  // example name that the user saves their template as.
                          // each template is an ArrayList
                  //templateName1 ArrayList contents
            }
          templateName2{ 
                  //templateName1 ArrayList contents
            }
          templateName2{ 
                  //templateName1 ArrayList contents
            }
       }
   }
}

I need to be able to list each template in "templates", and at other times simply get the ArrayList value of a specific template. However I can't seem to figure out how to do this simple thing.

From what I've googled/read in the docs, my code should look something like this:

DatabaseReference mTemplateRef = mDatabase.child("users").child(uid).child("templates");

    mTemplateRef.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    dataSnapshot.getChildren();

                    // somehow get the values here

                }
    });

When I debug, and enter the expression data dataSnapshot.getChildren() I can see the contents as expected, but I can't seem to get that data into my app. Any ideas?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
therealone
  • 421
  • 10
  • 22

3 Answers3

2

This is what I was looking for:

settingsRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
                // easy
                String value = dataSnapshot1.getValue(String.class);                    
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
therealone
  • 421
  • 10
  • 22
1

It's suggested that you create a class for a template with getters, empty and parameterised constructors as shown

public class Template {

    String name;
    ArrayList<String> list;

    public Template() {
    }

    public Template(String name, ArrayList<String> list) {
        this.name = name;
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public ArrayList<String> getList() {
        return list;
    }
} 

now you could easily do this

    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot data : dataSnapshot.getChildren()){

                Template template = data.getValue(Template.class);
                // use this object and store it into an ArrayList<Template> to use it further

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
DarshanGowda0
  • 452
  • 3
  • 11
  • Hm, I'm still having issues. How would this allow me to list out all of the ArrayLists under the key "templates"? Maybe have a separate ArrayList that each iteration adds to? How is this setting the ArrayList to your template class? I'm also getting this error: com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.app.template_editor.template – therealone Oct 05 '16 at 12:36
  • I'm going to leave my previous comment up in case you see anything obvious, but I think I know what to do, I just won't be able to test it until later today after school. I'll update if it works out – therealone Oct 05 '16 at 12:40
  • So, right now I did what you said, but I'm getting an error that I "can't convert object of type ArrayList to type Template" with Template being the class you provided. – therealone Oct 06 '16 at 17:44
  • take a look at this for arrays - [link](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html) – DarshanGowda0 Oct 11 '16 at 14:44
0

something like this code :

mTemplateRef.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                // dataSnapshot  itself  a child of   "templates"   
         DataSnapshot messageSnapshot= dataSnapshot;
                    DataType author =         (DataType)messageSnapshot.child("array").getValue();

                    // somehow get the values here

                }
    });