0

I tried to populate the recyclerview with geoquery and I set the radius of geoquery only 0.1 so I would get only 1 item from firebase (and it is working).

But, why were all IDs printed and looped based on item that I found in firebase.

View my Firebase JSON

View the end result in emulator

reference = FirebaseDatabase.getInstance().getReference("Users").child("Location");
GeoFire geoFire = new GeoFire(reference);

double my_lat = -6.2629133;
double my_lon = 106.8355483;

GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(my_lat, my_lon), 0.1);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(String key, GeoLocation location) {
        Query locationDataQuery = FirebaseDatabase.getInstance().getReference("Users").child("Cleaner");
        locationDataQuery.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){

                    for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
                        Sitter p = dataSnapshot1.getValue(Sitter.class);
                        list.add(p);
                    }
                    sitterAdapter = new SitterAdapter(MainActivity.this, list);
                    sitter_place.setAdapter(sitterAdapter);
                    Toast.makeText(getApplicationContext(), "Data Ada", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "Data Kosong", Toast.LENGTH_SHORT).show();
                }
            }

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

            }
        });
    }
}

I expect the output only 1 item that I found in firebase based on radius that I set for my geoquery.

Dennis Alund
  • 2,916
  • 1
  • 13
  • 34
  • Possible duplicate of [Difference between addValueEventListener() and addListenerForSingleValueEvent() of firebase](https://stackoverflow.com/questions/41579000/difference-between-addvalueeventlistener-and-addlistenerforsinglevalueevent) – Martin Zeitler Apr 11 '19 at 02:11
  • I tried to replace it with addListenerForSingleValueEvent but still nothing different. Thank you for helping anyway. – Panda Setiawan Apr 11 '19 at 02:20
  • those two events might still be interfering each other; I've once explained it [here](https://stackoverflow.com/a/52289410/549372). – Martin Zeitler Apr 11 '19 at 02:58

1 Answers1

0

Okay, I solved it by simply below. And right now still figuring out how to populate it to RecyclerView (since I tried it was always Object Can't String yadayadayada) :

Query locationDataQuery = FirebaseDatabase.getInstance().getReference("Users").child("Cleaner").child(key).child("info");

                locationDataQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.exists()){

                            Log.w("Name", dataSnapshot.child("cleaner_name").getValue(String.class));
                            Log.w("ID", dataSnapshot.child("cleaner_id").getValue(String.class));

//                            for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
//                                Sitter p = dataSnapshot1.getValue(Sitter.class);
//                                list.add(p);
//                            }

                            sitterAdapter = new SitterAdapter(MainActivity.this, list);
                            sitter_place.setAdapter(sitterAdapter);
                            Toast.makeText(getApplicationContext(), "Data Ada", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Toast.makeText(getApplicationContext(), "Data Kosong", Toast.LENGTH_SHORT).show();
                        }
                    }