Ahsan Sherazi
Ahsan Sherazi

Reputation: 33

Retrieve all Users id from Firebase Database

I have save data of users to firebase database with hashmap. Users's data is ordered by their ID's.

Saving code is:

emailauth = FirebaseAuth.getInstance();
    donorid= emailauth.getCurrentUser().getUid();
    mrefrnce= FirebaseDatabase.getInstance().getReference().child("Users").child("Donor").child(donorid);

    HashMap hmap= new HashMap();
    hmap.put("user_name",usern);
    hmap.put("bloodgroup",bld);
    hmap.put("user_address",address);
    hmap.put("user_type","Blood Donor");
    mrefrnce.updateChildren(hmap).addOnCompleteListener(new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
            if(task.isSuccessful()){
                Toast.makeText(MakeDonorProfile.this,"Data has been Saved",Toast.LENGTH_SHORT).show();
                progress.dismiss();
                Intent intent= new Intent(MakeDonorProfile.this, PlacePick.class);
                startActivity(intent);
            }
        }
    });

In the above code i have saved data by donor id

This is retrieving code

DatabaseReference donordRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Donor");
donordRef.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long size =  dataSnapshot.getChildrenCount();
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            getDonorData donordata = dataSnapshot.getValue(getDonorData.class);
            String latitude = child.child("latitude").getValue().toString();
            String longitude = child.child("longitude").getValue().toString();
            String bloodgroup = child.child("bloodgroup").getValue().toString();
            String do_address = child.child("user_address").getValue().toString();
            String donarname = child.child("user_name").getValue().toString();
            double loclatitude = Double.parseDouble(latitude);
            double loclongitude = Double.parseDouble(longitude);
            LatLng  cod = new LatLng(loclatitude, loclongitude);
            mMap.addMarker(new MarkerOptions().position(cod).title( donarname).snippet("Blood Group:   "+bloodgroup+"\n"+"Address:   "+do_address));
            mMap.setInfoWindowAdapter(new InfoWindowAdapter(SearchActivity.this));

        }


    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }


});

Now in retrieving code i want to get donor id with other data.

How I can get this id for all donors in DataSnapshot for loop?

Database picture is click database picture

Thanks in Advance

Regards

Ahsan Sherazi

Upvotes: 1

Views: 1219

Answers (1)

Harsh Jain
Harsh Jain

Reputation: 1372

I think this might help.

DatabaseReference databaseRef = databaseReference.child("Donor");
        databaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
                Log.d("GetDonorId","DataSnapshot :"+dataSnapshot1.getKey());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Upvotes: 1

Related Questions