1

I want to create an "edit user profile" page on Android Studio. The user(which is logged in obviously) can go in and edit user info such as weight and age. Here is how my firebase database is set up:

The database

In this case, a user who is logged in as "Raxor2k", wants to change he`s info such as Age and Weight.

I have made a function that queries the database, and it manages to reach the "AdditionalUserInfo" table which is good. But the next task is to reach those specific values that belong to the logged-in user.

here is the code:

public class UserSettingsActivity extends AppCompatActivity {

    private Button mEditInfoButton;

    private TextView usernameField;

    private EditText ageField, weightField;

    private DatabaseReference dbUsernames;


    DatabaseReference the_additional_userInfo_table = FirebaseDatabase.getInstance().getReference("AdditionalUserInfo");

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

        ageField = (EditText) findViewById(R.id.ageID);
        weightField = (EditText) findViewById(R.id.weightID);

        mEditInfoButton = (Button) findViewById(R.id.editButton);

        usernameField = (TextView) findViewById(R.id.usernameTextViewID);

        mEditInfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QueryUserInfo();
            }
        });


    }


    public void QueryUserInfo(){
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference the_additional_userInfo_table = database.getReference("AdditionalUserInfo");
        //Query query =the_additional_userInfo_table.child("username");

        the_additional_userInfo_table.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    Toast.makeText(UserSettingsActivity.this, "User exists!", Toast.LENGTH_SHORT).show();
                    // dataSnapshot is the "issue" node with all children with id 0
                    for (DataSnapshot issue : dataSnapshot.getChildren()) {
                        // do something with the individual "issues"
                    }
                    if(!dataSnapshot.exists()){

                        Toast.makeText(UserSettingsActivity.this, "nooooo user!", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {


            }


        });
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
raxor2k
  • 97
  • 1
  • 9

1 Answers1

2

To get the data under AdditionalUserInfo that corresponds to a specific user, you need to use a query that looks like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference additionalUserInfoRef = rootRef.child("AdditionalUserInfo");
Query userQuery = additionalUserInfoRef.orderByChild("username").equalTo("Raxor2k");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Map<String, Object> map = new HashMap<>();
            map.put("user-age", "30");
            map.put("user-weight", "30");
            ds.getRef().updateChildren(map);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
userQuery.addListenerForSingleValueEvent(valueEventListener);

The result in your database will be the change of both properties from 25 to 30. Since you didn't add the database as a text, file I have used in the query he user name but a more elegant way of querying the database would be to use the uid.

Query userQuery = additionalUserInfoRef.orderByChild("user-id").equalTo("IXL ... Eoj");
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Lets now say I wanted to do this the opposite way where I want to read the age and weight for the user: Raxor2k, how would you have done this? – raxor2k Dec 20 '19 at 16:14
  • Simply using `String userAge = ds.child("user-age").getValue(String.class)`. That's it. But removing the other 4 lines of code. – Alex Mamo Dec 20 '19 at 16:15
  • 1
    You are simply a legend! We need more people like you on the: #android-dev channel on IRC! – raxor2k Dec 20 '19 at 16:25