0

i am trying to read a specific child in Firebase which i named Tags. the problem is, i just can't put the object from tags (dados.getValue) into a ArrayList to later populate in my ListView.

I know is simple, sorry about that, ut i am new here in android

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_taglist);

        tags = new ArrayList<>();
        tagList = (ListView) findViewById(R.id.tagsList);

        tagsRefs = FirebaseConfig.getFireBase();
        tagsRefs.child("tags").child("categorias");

        tagsRefs.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot dados : dataSnapshot.child("tags").getChildren()) {
                    System.out.println("tag EXTRAIDA NO taglist " + dados.getValue());
                     dados.getValue();   //HOW CAN I PUT THIS INTO AN ARRAY TO LATER ADD IN MY ArrayList tags?? 
                    String tagS = dados.getValue(String.class);
                    tags.addAll(tagS);
                }
            }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


        tagAdapter = new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_2,
                android.R.id.text1,
                tags);


        tagList.setAdapter(tagAdapter);

        tagList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                selectedTag = (String) tagList.getItemAtPosition(position);

                setSelectedTag(selectedTag);

            }
        });


    }

here is my database: h

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Aury0n
  • 217
  • 3
  • 14

1 Answers1

0

This is happening because onDataChange() method is called asynchronously. This means that the statement that adds tags to your list is executed before onDataChange() method has been called. That's why your list is empty outside that method. So in order to use that lists, you need to use it inside the onDataChange().

For other approach, please visit this post and this post.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • tks so much for the advice, the problem was my tags declaration in ArrayList tagss = (ArrayList) dados.getValue(); – Aury0n May 25 '17 at 14:29