4

I have an ArrayList defined in my Android app for my MainActivity.

I've been running into difficulties trying to understand how to retrieve the interests of the user and add it to my array list.

Here's how my Firebase data is set up:

FirebaseData

I understand that I must load the data asynchronously, but I'm confused as to how I should go about that.

here's my code for trying to add to the ArrayList:

public class MainActivity extends AppCompatActivity {


private DatabaseReference eventDatabase;
private DatabaseReference usersDatabase;

private FirebaseAuth mAuth;
private String currentUserID;

private ArrayList<String> userInterests = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    eventDatabase = FirebaseDatabase.getInstance().getReference().child("Event");
    usersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

    mAuth = FirebaseAuth.getInstance();

    currentUserID = mAuth.getCurrentUser().getUid();

    usersDatabase.child(currentUserID).child("interests").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                boolean isInterested = (Boolean)postSnapshot.getValue();
                if (isInterested){
                    userInterests.add(postSnapshot.getKey());
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    System.out.println(userInterests);

I want the ArrayList to consist of the user's interests, if it is false in the database, it won't be added to the list. Example: [COMPETITION, CULTURE, EDUCATION...etc]

When it returns the list in the console, I just see an empty list -- "[]"

2
  • where are you initializing userInterests Commented Dec 28, 2016 at 9:26
  • Is there data in the database at the path being returned? How did you verify this? Do you take any action to make the UI redraw? Have you tried a breakpoint at .add() to see if it's ever called? Where are the contents of the array list being output after receiving an event from the database? See how to ask and creating an mcve. Commented Dec 28, 2016 at 21:18

2 Answers 2

6

This is what you want.

usersDatabase.child(currentUserID).child("interests").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
     for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
           boolean isInterested = (Boolean)postSnapshot.getValue();
        if (isInterested) {
            userInterests.add(postSnapshot.getKey());
        }
   }
Sign up to request clarification or add additional context in comments.

1 Comment

the console returns an empty list when using this. Is there something I'm missing?
2

If you are using firebase with Android, then i would suggest you to use firebase-ui components, Firebase UI, it will be very helpful

Or Instead of trying to cast in Boolean, try with String and the convert it to Boolean

Comments

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.