带有recyclerview的双重打印地理查询

时间:2019-04-11 01:53:56

标签: java android firebase firebase-realtime-database geofire

我尝试使用geoquery填充recyclerview,并且将geoquery的半径设置为0.1,因此我只能从firebase中获得1个项目(并且它可以正常工作)。

但是,为什么所有的ID都是根据我在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) {

            }
        });
    }
}

我希望根据我为地理查询设置的半径,仅输出在Firebase中找到的一项。

1 个答案:

答案 0 :(得分:0)

好的,我在下面简单地解决了。现在,仍在弄清楚如何将其填充到RecyclerView中(因为我尝试过始终是Object Ca n'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();
                        }
                    }