0

I am not able to search for a specific string in the database, I was stuck in a problem where i have to search for type of vehicle from users database and show only that information. I tried many solutions on stackoverflow but it was of no use may be because i have a complex structure of database Database structure I know the question is too vague but i have been stuck on this problem for hours so pease help me

I have tried searching for that field by using equalTo and orderby query

Go the truck used by driver using this code

        databaseReference1=FirebaseDatabase.getInstance().getReference("truckers").child(userId);
        final String[] type_of_truck = new String[1];
        databaseReference1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Log.e("Current Truck",""+dataSnapshot.child("Type of truck").getValue());
                type_of_truck[0] = (String) dataSnapshot.child("Type of truck").getValue();
            }

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

            }
        });
        return type_of_truck[0];
    }

Tried to find user using this code

private void findUsers(String type_of_truck) {
        DatabaseReference d = FirebaseDatabase.getInstance().getReference("users");
        d.orderByChild("type_of_truck").equalTo(type_of_truck).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Log.e("pp",""+dataSnapshot.getValue());
                for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
                    Log.e("pp",""+userSnapshot.getValue());
                }
            }

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

            }
        });
    }

For eg i am using LCV closed body so i should get all information of that delivery id under users reference where type_of_truck is "LCV closed body"

Aryan Soni
  • 236
  • 4
  • 15
  • 1
    You cannot return something now that hasn't been loaded yet. So please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo May 29 '19 at 10:39
  • Thanks a lot @AlexMamo for pointing out the duplicate this answer solved my problem – Aryan Soni May 29 '19 at 11:14

0 Answers0