0
final DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Client");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot snapshot1:snapshot.child("c_orders").getChildren()){
                final String rt=snapshot1.getKey().toString();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

    FirebaseRecyclerOptions<client_model_btncheckorders> options2 =
            new FirebaseRecyclerOptions.Builder<client_model_btncheckorders>()
                    .setQuery(FirebaseDatabase.getInstance().getReference().child("Client").child("c_orders").child(rt).child("check_orders"), client_model_btncheckorders.class)
                    .build();
    adapter2=new adapter_clientside_checkbtn(options2);
    recyclerView2.setAdapter(adapter2);


    return view;
}

Here I have to retrieve the value of "rt" which is in for loop and that is to be then passed to a Firebase database query to retrieve the child element's value. But the problem is, "rt" may contain many usernames of the firebase real-time database. I have to split them and refer a each username to the query. It is not possible to write Firebase reference inside for loop because the adapter of the recycler view(where data needs to be displayed) cannot be accessed. The recycler view where I was trying to display data from firebase database displays by pressing a button of another recycler view. Is there any solution for this?

Akash
  • 11
  • 2
  • Have a List outside the scope; add it to the list and access it after for block – Siva Rahul Jun 03 '22 at 06:48
  • Create a global or a local scoped (outside the listener) `String` variable, break the loop once you have the relevant value inside the loop. – Darshan Jun 03 '22 at 06:57
  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Jun 04 '22 at 07:17

1 Answers1

0

You cannot retrieve that variable. It exists inside the loop only - that is the concept of variable scopes.

You need to define the variable in the scope where you want to use it. The loop has access to it's own scope but also the scope outside.

It would look like this then:

String rt = null;

final DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Client");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot snapshot1:snapshot.child("c_orders").getChildren()){
                rt=snapshot1.getKey().toString();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });

    FirebaseRecyclerOptions<client_model_btncheckorders> options2 =
        new FirebaseRecyclerOptions.Builder<client_model_btncheckorders>()
            .setQuery(FirebaseDatabase.getInstance().getReference().child("Client").child("c_orders").child(rt).child("check_orders"), client_model_btncheckorders.class)
            .build();
    adapter2=new adapter_clientside_checkbtn(options2);
    recyclerView2.setAdapter(adapter2);

    return view;
}

But watch out about runtime: You set the string in an event handler. It would mean you'd have to run the query after the event handler has run. Would that not justify to run it from the event handler? Or you use a separate thread? I am not sure where you actually want to use the value.

Queeg
  • 7,748
  • 1
  • 16
  • 42