Home > Software engineering >  Converting String datatype to Double in Recyclerview
Converting String datatype to Double in Recyclerview

Time:12-12

I'm trying to retrieve the coordinates of the user from Firebase and display them in a RecyclerView as a log of all locations sent by users. However, the app is crashing. I already have a similar RecyclerView but all of the data being displayed is String.

My database structure:

{
  "Blocked Users" : {
    "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : 0,
    "rbHcHgUYb3R0WwwBa10OpXeba1T2" : 1
  },
  "User Location" : {
    "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : {
      "latitude" : 35.2732867,
      "longitude" : -118.9991467,
      "timestamp" : 1639198720555
    }
  },
  "Users" : {
    "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : {
      "Alert Level" : "High",
      "Emergency Type" : "Natural Disaster",
      "address" : "Lapaz, Tarlac",
      "emergencyNum" : "09123342345",
      "lName" : "QWErty",
      "name" : "Rrlac",
      "phoneNum" : "09123234534",
      "userTimestamp" : 1637648670269
    }
  }
}

This is my Data class:


public class MainData {
    String address, name, phoneNum, UID, lName;
    Double latitude, longitude;


    MainData () {};

    public MainData(String address, String name, String phoneNum, String UID) {
        this.address = address;
        this.name = name;
        this.phoneNum = phoneNum;
        this.UID = UID;
        this.lName = lName;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getUID() {
        return UID;
    }

    public void setUID(String UID) {
        this.UID = UID;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }
}

This is my Adapter for the Location Log

public class MainAdapter2 extends FirebaseRecyclerAdapter<MainData, MainAdapter2.myViewHolder> {

    @Override
    protected void onBindViewHolder(@NonNull myViewHolder holder, @SuppressLint("RecyclerView") int position, @NonNull MainData model) {
        holder.lName.setText(model.getlName());
        holder.latitude.setText(model.getLatitude());
        holder.longitude.setText(model.getLongitude());


        Glide.with(holder.image.getContext())
                .load(model.getlName())
                .placeholder(R.drawable.common_google_signin_btn_icon_dark)
                .circleCrop()
                .error(R.drawable.btn_google_signin_dark_normal)
                .into(holder.image);

    }

    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);

        return new myViewHolder(view);
    }

    public MainAdapter2(@NonNull FirebaseRecyclerOptions<MainData> mainDataFirebaseRecyclerOptions) {
        super(mainDataFirebaseRecyclerOptions);
    }

    class myViewHolder extends RecyclerView.ViewHolder {

            CircleImageView image;
            TextView latitude, longitude, lName;



        public myViewHolder(@NonNull View itemView) {
            super(itemView);

            image = (CircleImageView) itemView.findViewById(R.id.img1);
            lName = (TextView) itemView.findViewById(R.id.nameText);
            latitude = (TextView) itemView.findViewById(R.id.LatitudeText);
            longitude = (TextView) itemView.findViewById(R.id.LongitudeText);


        }
    }
    
}

This is the code that gets the data from Firebase:

public class LocationLog extends AppCompatActivity {

    RecyclerView recyclerView;
    MainAdapter2 mainAdapter;

    String uid = FirebaseAuth.getInstance().getUid();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locationlog);

        recyclerView = (RecyclerView) findViewById(R.id.locationList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        FirebaseRecyclerOptions<MainData> mainDataFirebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<MainData>()
                .setQuery(FirebaseDatabase.getInstance().getReference().child("User Location"), MainData.class)
.setQuery(FirebaseDatabase.getInstance().getReference().child("Users"), MainData.class)
                .build();
        mainAdapter = new MainAdapter2(mainDataFirebaseRecyclerOptions);
        recyclerView.setAdapter(mainAdapter);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mainAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mainAdapter.stopListening();
    }

}

The stacktrace:

2021-12-11 15:30:40.269 10220-10220/com.example.rescuealertadmin E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.rescuealertadmin, PID: 10220
    com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Double to String
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
        at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
        at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
        at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
        at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:36)
        at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:109)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
2021-12-11 15:30:40.270 10220-10220/com.example.rescuealertadmin E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:784)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:731)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

How should I convert the double values to string or is there any way to display the double values? Thank you.

CodePudding user response:

You just convert Double to String in onBindViewHolder

Like:

holder.latitude.setText(Double.toString(model.getLatitude()));
holder.longitude.setText(Double.toString(model.getLongitude()));

CodePudding user response:

You can use:

holder.latitude.setText( String.valueOf( model.getLatitude() ));

Or:

holder.latitude.setText( model.getLatitude()   "" );
  • Related