TiagoIB
TiagoIB

Reputation: 439

Retrieve and display Firebase data in EditText and edit content save again

Create data editing screen of a user profile. The intent is to retrieve the data saved in the Firebase database, and display them in an EditText. With database data already suggested for the user to edit or not, optionally.

EXAMPLE:
Firebase Database
- users 
--iduser
----name
----lastname


Screen Example: 
Fields:
- EditText1 (mName)
- EditText2 (mLastName)

I tried something like this:

MainActivity:
private EditText mName;
private EditText mLastName;

    private DatabaseReference mDatabaseUsers;


    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Dashboard");
        setSupportActionBar(toolbar);

        mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");


        /*User logged in*/
        final String user_id = mAuth.getCurrentUser().getUid();
        mName = (EditText) findViewById(R.id.edtName);
        mLastName = (EditText) findViewById(R.id.edtLastName);          

        /*I have already created a String to capture the logged-in user ID because I need to access only those user's data.*/    
        /* I've got this far, I need help to set the value in EditText that will be displayed. Thank you!!*/

        mDatabaseUsers.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {




            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });



}

The intention would be to set the content in EditText, like this: mName.setText("Bank Details" , TextView.BufferType.EDITABLE);

Then, save the changed data back into the database.

Upvotes: 0

Views: 4799

Answers (2)

OnlySalman
OnlySalman

Reputation: 108

you are having an issue in data retrieving and displaying, data retrieving can be done in different ways on way I like is by using a Query it's simple and street flowered but First before I write the code for it let me clarify one thing you say this:

EXAMPLE:
Firebase Database
- users 
--iduser
----name
----lastname

So, you must reach your database via writing this

        mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("users");// small u not U

So, that's getting out of the way and now you are in your database there are many different ways this works but this is what I chose:

    Query query = firebaseDatabase.child(//write your user id here );
            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot!=null){
  mName.setText(dataSnapshot.child("name").getValue(String.class));
 // the same for the last name 
                        }

                    else {
   mName.setText("There is no mutching data ");
                     }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

Upvotes: 0

Bruno Ferreira
Bruno Ferreira

Reputation: 1571

Try something like that:

MainActivity:
private EditText mName;
private EditText mLastName;

    private DatabaseReference mDatabaseUser_name,mDatabaseUser_lastname;


    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Dashboard");
        setSupportActionBar(toolbar);


        /*User logged in*/
        final String user_id = mAuth.getCurrentUser().getUid();
        mName = (EditText) findViewById(R.id.edtName);
        mLastName = (EditText) findViewById(R.id.edtLastName);          

        mDatabaseUser_name = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id).child("name");
        mDatabaseUser_lastname = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id).child("lastname");

        mDatabaseUser_name.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                    mName.settext(dataSnapshot.getValue(String.class));

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        mDatabaseUser_lastname.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                    mLastName.settext(dataSnapshot.getValue(String.class));

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

}

Upvotes: 1

Related Questions