wright_arturo
wright_arturo

Reputation: 81

Firebase Data in Android Spinner

This is a follow on from a previous post, that I'm still having a few issues with - See original post here: Populating Android spinner with Firebase Data.

I have made amendments to my Firebase rules in order to achieve user-based authentication for each user. I have successfully implemented this, however, one problem still remains - The spinner in my MaintenanceActivity is no longer populating with the propertyAddress attribute that is from the properties node in my database.

My current security set up works perfectly for adding, deleting and updating records, however, it does not seem to be populating the spinner with the propertyAddress.

Here are my security rules:

    {
  "rules": {
    "maintenance" : {
      "$uid" : {
    ".read": "$uid === auth.uid",
    ".write": "$uid === auth.uid"
  }
},
    "properties" : {
     "$uid" : {
    ".read": "$uid === auth.uid",
    ".write": "$uid === auth.uid"
    }
}
}
}

Code extract from MaintenanceActivity which I hoped would have populated the spinner:

fDatabaseRoot.child("properties").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                final List<String> propertyAddressList = new ArrayList<String>();

                for (DataSnapshot addressSnapshot: dataSnapshot.getChildren()) {
                    String propertyAddress = addressSnapshot.child("propertyAddress").getValue(String.class);
                    if (propertyAddress!=null){
                        propertyAddressList.add(propertyAddress);
                    }
                }

                Spinner spinnerProperty = (Spinner) findViewById(R.id.spinnerProperty);
                ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(MaintenanceActivity.this, android.R.layout.simple_spinner_item, propertyAddressList);
                addressAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerProperty.setAdapter(addressAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

For further clarification, here is my data structure

Data Structure

Upvotes: 0

Views: 1113

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80924

Under the properties node you have two ids, the first one is the userid and the second one is a random id generated by the push() method.

To be able to access the property address you need to get the userid and then using for(DataSnapshot addressSnapshot: dataSnapshot.getChildren()) { you will iterate inside the random id and get the attributes there.

So change this:

fDatabaseRoot.child("properties").addListenerForSingleValueEvent(new ValueEventListener() {

to this:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
fDatabaseRoot.child("properties").child(userid).addListenerForSingleValueEvent(new ValueEventListener() {

Upvotes: 1

Related Questions