skip to Main Content

Keep getting this error repeating when applying changes.

Current data using String -> Failed to convert value of type java.lang.Long to String

Current data declared as Double -> Failed to convert a value of type java.lang.String to double

searchDB = FirebaseDatabase.getInstance().getReference("Users");
geofenceDB = FirebaseDatabase.getInstance().getReference("Geo-fence");

searchList.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
searchList.setLayoutManager(layoutManager);

String userID = FirebaseAuth.getInstance().getUid();
geofenceDB.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        final double userLat = snapshot.child(userID).child("Latitude").getValue(Double.class);
        final double userLng = snapshot.child(userID).child("Longitude").getValue(Double.class);
        final double userRadius = snapshot.child(userID).child("Radius").getValue(Double.class).toString();

        for (DataSnapshot tutorSnapshot : snapshot.getChildren()){
            double tutorLat = tutorSnapshot.child("Latitude").getValue(Double.class);
            double tutorLng = tutorSnapshot.child("Longitude").getValue(Double.class);

            assert userRadius != null;
            //Get all Tutor data from Database
                searchDB.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        tutorArrayList = new ArrayList<>();
                        tutorArrayList.clear();
                        for (DataSnapshot key : snapshot.getChildren()) {
                            User tutor = key.getValue(User.class);
                            String typeData = tutor.getType().toLowerCase(Locale.ROOT);
                            if (typeData.equals("tutor")){
                                if (calculateDistanceInKilometer(userLat,userLng,tutorLat,tutorLng) < Double.parseDouble(userRadius)){
                                    Toast.makeText(getContext(),"Circle: "+calculateDistanceInKilometer(userLat,userLng,tutorLat,tutorLng),Toast.LENGTH_SHORT).show();
                                    tutorArrayList.add(tutor);
                                }

                            }
                        }
                        adapter = new SearchAdapter(getActivity(), tutorArrayList);
                        searchList.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        if (adapter.getItemCount()==0){
                            searchList.setVisibility(View.GONE);
                            empty.setVisibility(View.VISIBLE);
                        }else {
                            searchList.setVisibility(View.VISIBLE);
                            empty.setVisibility(View.GONE);
                        }
                    }

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

                    }
                });
        }
    }

=========================================================================

},
  "Geo-fence": {
    "3g94j92jqFY30tXzFe1GCXMrwKn1": {
      "Latitude": 3.1189083,
      "Longitude": 101.5712633,
      "Radius": "9999"
    }
  },

2

Answers


  1. If your Longitude is a string type, please remove "" (Double quotes).

    For example:
    enter image description here

    Login or Signup to reply.
  2. The problem is that you’ve stored the value of the Radius property as a string in your database. So when you try to then read it as a Double with snapshot.child(userID).child("Radius").getValue(Double.class), the SDK raises an error, because Radius does not have a double value.

    To get the radius as a string, do:

    final String userRadius = snapshot.child(userID).child("Radius").getValue(String.class);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search