简体   繁体   中英

Get data from firebase Realtime database

I want to get data from firebase in sorted manner, I tried below method but its not working propely and collects all the data.

Here is the image for my firebase database:

数据库映像

  DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference();
        
String input = "G";
Date cd = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
String formattedDate = df.format(cd);
FirebaseUser currentUser = mAuth.getCurrentUser();
DatabaseReference data = databaseRef.child("Data").child(currentUser.getUid()).child(formattedDate.toString()).child(medname.toString());
databaseRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        List<String> data = new ArrayList<String>();

        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
            data.add(postSnapshot.getValue().toString());
        }
        Log.d("datafriebase",""+data);
    }

I want to get data in sorted manner, like value of 1, value of 2 but i am getting whole database

You're getting the whole database, because you're reading from databaseRef , which you initialized as:

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference();

To get only the nested data, read from data , which you set to reference that path:

DatabaseReference data = databaseRef.child("Data").child(currentUser.getUid()).child(formattedDate.toString()).child(medname.toString());

So:

data.addValueEventListener(new ValueEventListener() {
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM