Zain08
Zain08

Reputation: 43

Retrieving data of currently logged in user using firebase realtime database in android studio

I have an application where users are signed up and signed in using firebase authentication and they save contact information. The application displays all the contact data inserted in the firebase database. Now what I want to do is that the application should only display the data inserted by the currently logged in user only not all data inserted. For this purpose, I'm getting the currently logged in user id and saving in the firbase database. The code below gets the currently logged in user's id:

mAuth = FirebaseAuth.getInstance();
        mAuth.getCurrentUser();

        FirebaseUser mUser = mAuth.getCurrentUser();

        String uId = mUser.getUid();

And this code is used to insert the currently logged in user's id in the database.

 Map<String,Object> map=new HashMap<>();
        map.put("name",name.getText().toString());
        map.put("course",course.getText().toString());
        map.put("email",email.getText().toString());
        map.put("uid",uId);
       
        FirebaseDatabase.getInstance().getReference().child("contacts").push()
                .setValue(map)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                       name.setText("");
                       course.setText("");
                       email.setText("");
                      
                        Toast.makeText(getApplicationContext(),"Inserted Successfully",Toast.LENGTH_LONG).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e)
                    {
                        Toast.makeText(getApplicationContext(),"Could not insert",Toast.LENGTH_LONG).show();
                    }
                });

For retrieving the data in a recyclerview I'm using the code below.

 FirebaseRecyclerOptions<model> options=
                new FirebaseRecyclerOptions.Builder<model>()
                .setQuery(FirebaseDatabase.getInstance().getReference().child("contacts"),model.class)
                .build();

        myadapter=new myadapter(options);
        recyclerView.setAdapter(myadapter);

Now please tell me a method how to compare the uid that is inserted in the firebase database with the currently logged in user's id to only get the data that was inserted by the currently logged in user. I read that the following query can be used to compare the data but I don't know to how to use it.

Query query = ref.orderByChild("contacts").equalTo("48bde8f8-3b66-40bc-b988-566ccc77335c");

Please guide me how to use a query or method to compare the user ids and retrieve the data that was inserted by the currently logged in user, not all the data that was inserted. I will be very thankful to you.

Upvotes: 0

Views: 1683

Answers (1)

Sami Shorman
Sami Shorman

Reputation: 158

First make the key of user node the id of the user instead of push():

 Map<String,Object> map=new HashMap<>();
    map.put("name",name.getText().toString());
    map.put("course",course.getText().toString());
    map.put("email",email.getText().toString());
    map.put("uid",uId);
   
    FirebaseDatabase.getInstance().getReference().child("contacts").child(uId)
            .setValue(map)

Then when you want to retrieve specific user object u can get the uId from:

String userId = FirebaseAuth.getInstance().getCurrentUser().getUId()

now u can add Listener to this DatabaseRefernce:

    DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("contacts").child(userId)..... db.addValueEventListener().......
           

That's it

Upvotes: 1

Related Questions