1

We have a very flat Firebase data structure and have been trying to load the DB into an ArrayList by first loading the DB to a HashMap. We know the size of the ArrayList it is only 1 so that tells us we are not obtaining all the data. Our question is how to iterate over the datastore? We are also using a Model Class called UserInformation

    db = FirebaseDatabase.getInstance().getReference(); 
    dbRef = db.child("Quiz Table");
    dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Iterable<DataSnapshot> children = dataSnapshot.getChildren();

                //GenericTypeIndicator<HashMap<String, Object>> objT = new GenericTypeIndicator<HashMap<String, Object>>() {};
                String objHashMap = dataSnapshot.child(String.valueOf(X)).getValue(String.class);
                ArrayList<String> objArrayList = new ArrayList<>();
                objArrayList.add(String.valueOf(objHashMap));

                if (objArrayList != null) {

                    String question = objArrayList.get(0);
                    String ansone = objArrayList.get(0);
                    String anstwo = objArrayList.get(0);
                    String ansthree = objArrayList.get(0);
                    String ansfour = objArrayList.get(0);
                    String corans = objArrayList.get(0);
                    //etTableName.setText(pName);
                    etQuestion.setText(question);
                    etAnsOne.setText(ansone);
                    etAnsTwo.setText(anstwo);
                    etAnsThree.setText(ansthree);
                    etAnsFour.setText(ansfour);
                    etCorAns.setText(corans);
                }
            }
Vector
  • 3,066
  • 5
  • 27
  • 54
  • You've defined `children`, but you're not doing anything with it. Did you intend to? – Doug Stevenson May 24 '18 at 06:48
  • @DougStevenson have a look at this Answered Questionhttps://stackoverflow.com/questions/48833010/firebase-for-android-studio-how-do-you-loop-through-each-item-in-a-database/50501793#50501793 – Vector May 24 '18 at 07:19

1 Answers1

3

As you mentioned in the comments: You've defined an iterator, but did not use it. You're only adding a single child to the ArrayList. You can iterate through each child, adding it to your ArrayList like this:

ArrayList<String> objArrayList = new ArrayList<>();
for(DataSnapshot child : dataSnapshot.getChildren())
    objArrayList.add(child.getValue(String.class));
  • Perieira This solves more days of struggle than I care to admit The real issue with all the past posts everyone uses the DB for one line Chat Apps I still do not know why this works by removing the objHashMap line of code All the top notch tutorials show using a HashMap ? ? ? Thanks – Vector May 24 '18 at 15:47