无法将类型java.lang.String的值转换为double

时间:2019-07-10 04:25:18

标签: java android firebase

大家好,我是android开发领域的新手。查看我的编码,我将把firebase的输出打印到显示所有数据的卡片视图中,但是当我创建具有不同类型的值(例如来自firebase的字符串和双精度值)的类模型时,它将失败转换。为什么这样的问题?我也想要解决方案。

我尝试使用Datasnapshotfirebase获取数据并将字符串值转换为双精度值。

Firebase:

This is what my <code>firebase</code> looks like.

获取值的功能

        {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                lrpModels =new ArrayList<>();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
                {
                    //the problem at this line
                    LRP_model lrpC = postSnapshot.getValue(LRP_model.class);
                    lrpModels.add(lrpC);
                }
                //creating adapter
                mLRPAdapter = new LRPAdapter(getApplication(),lrpModels, "LRP");
                //add the adapter to the recyclerview
                recyclerView.setAdapter(mLRPAdapter);
            }

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

            }
        });

模型类


    private String uid;
    private String lrpName;
    private String lrpDesc;
    private String lrpStartpoint;
    double lrpSLat;
    double lrpSLng;
    private String lrpEndpoint;
    double lrpELat;
    double lrpELng;
    private String lrpDistance;
    private String lrpSubRegion;
    private String lrpRegion;
    private String userId;
    private ArrayList<Route_model> route_models;

    public LRP_model(){}
    public LRP_model(String uid, String lrpName, String lrpDesc, String lrpStartpoint, double lrpSLat, double lrpSLng, String lrpEndpoint, double lrpELat, double lrpELng, String lrpDistance, String lrpSubRegion, String lrpRegion, String userId, ArrayList<Route_model>route_models) {
        this.uid = uid;
        this.lrpName = lrpName;
        this.lrpDesc = lrpDesc;
        this.lrpStartpoint = lrpStartpoint;
        this.lrpSLat = lrpSLat;
        this.lrpSLng = lrpSLng;
        this.lrpEndpoint = lrpEndpoint;
        this.lrpELat = lrpELat;
        this.lrpELng = lrpELng;
        this.lrpDistance = lrpDistance;
        this.lrpSubRegion = lrpSubRegion;
        this.lrpRegion = lrpRegion;
        this.userId = userId;
        this.route_models = route_models;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public void setLrpName(String lrpName) {
        this.lrpName = lrpName;
    }

    public void setLrpDesc(String lrpDesc) {
        this.lrpDesc = lrpDesc;
    }

    public void setLrpStartpoint(String lrpStartpoint) {
        this.lrpStartpoint = lrpStartpoint;
    }

    public void setLrpSLat(double lrpSLat) {
        this.lrpSLat = lrpSLat;
    }

    public void setLrpSLng(double lrpSLng) {
        this.lrpSLng = lrpSLng;
    }

    public void setLrpEndpoint(String lrpEndpoint) {
        this.lrpEndpoint = lrpEndpoint;
    }

    public void setLrpELat(double lrpELat) {
        this.lrpELat = lrpELat;
    }

    public void setLrpELng(double lrpELng) {
        this.lrpELng = lrpELng;
    }

    public void setLrpDistance(String lrpDistance) {
        this.lrpDistance = lrpDistance;
    }

    public void setLrpSubRegion(String lrpSubRegion) {
        this.lrpSubRegion = lrpSubRegion;
    }

    public void setLrpRegion(String lrpRegion) {
        this.lrpRegion = lrpRegion;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUid() {
        return uid;
    }

    public String getLrpName() {
        return lrpName;
    }

    public String getLrpDesc() {
        return lrpDesc;
    }

    public String getLrpStartpoint() {
        return lrpStartpoint;
    }

    public double getLrpSLat() {
        return lrpSLat;
    }

    public double getLrpSLng() {
        return lrpSLng;
    }

    public String getLrpEndpoint() {
        return lrpEndpoint;
    }

    public double getLrpELat() {
        return lrpELat;
    }

    public double getLrpELng() {
        return lrpELng;
    }

    public String getLrpDistance() {
        return lrpDistance;
    }

    public String getLrpSubRegion() {
        return lrpSubRegion;
    }

    public String getLrpRegion() {
        return lrpRegion;
    }

    public String getUserId() {
        return userId;
    }

    public ArrayList<Route_model> getRoute_models(){
        return  route_models;
    }

    public void setRoute_models(ArrayList<Route_model> route_models){
        this.route_models = route_models;
    }

}

我希望将值检索为double,并且也可以将lat和long放入映射中,但是显示的错误未能转换字符串值。

1 个答案:

答案 0 :(得分:0)

检查Your Firebase look
发现您已将double用作数据类型。将其更改为

的字符串
double lrpSLat;  change to String lrpSLat;
double lrpSLng;  change to String lrpSLng;
double lrpELat;  change to String lrpELat;
double lrpELng;  change to String lrpELng;

您更新的LRP_model将是

package com.prime_components; public class LRP_model implements Serializable {
    private String uid;
    private String lrpName;
    private String lrpDesc;
    private String lrpStartpoint;
    String lrpSLat;
    String lrpSLng;
    private String lrpEndpoint;
    String lrpELat;
    String lrpELng;
    private String lrpDistance;
    private String lrpSubRegion;
    private String lrpRegion;
    private String userId;
    private ArrayList<Route_model> route_models;

    public LRP_model(){}
    public LRP_model(String uid, String lrpName, String lrpDesc, String lrpStartpoint, String lrpSLat, String lrpSLng, String lrpEndpoint, String lrpELat, String lrpELng, String lrpDistance, String lrpSubRegion, String lrpRegion, String userId, ArrayList<Route_model>route_models) {
        this.uid = uid;
        this.lrpName = lrpName;
        this.lrpDesc = lrpDesc;
        this.lrpStartpoint = lrpStartpoint;
        this.lrpSLat = lrpSLat;
        this.lrpSLng = lrpSLng;
        this.lrpEndpoint = lrpEndpoint;
        this.lrpELat = lrpELat;
        this.lrpELng = lrpELng;
        this.lrpDistance = lrpDistance;
        this.lrpSubRegion = lrpSubRegion;
        this.lrpRegion = lrpRegion;
        this.userId = userId;
        this.route_models = route_models;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public void setLrpName(String lrpName) {
        this.lrpName = lrpName;
    }

    public void setLrpDesc(String lrpDesc) {
        this.lrpDesc = lrpDesc;
    }

    public void setLrpStartpoint(String lrpStartpoint) {
        this.lrpStartpoint = lrpStartpoint;
    }

    public void setLrpSLat(String lrpSLat) {
        this.lrpSLat = lrpSLat;
    }

    public void setLrpSLng(String lrpSLng) {
        this.lrpSLng = lrpSLng;
    }

    public void setLrpEndpoint(String lrpEndpoint) {
        this.lrpEndpoint = lrpEndpoint;
    }

    public void setLrpELat(String lrpELat) {
        this.lrpELat = lrpELat;
    }

    public void setLrpELng(String lrpELng) {
        this.lrpELng = lrpELng;
    }

    public void setLrpDistance(String lrpDistance) {
        this.lrpDistance = lrpDistance;
    }

    public void setLrpSubRegion(String lrpSubRegion) {
        this.lrpSubRegion = lrpSubRegion;
    }

    public void setLrpRegion(String lrpRegion) {
        this.lrpRegion = lrpRegion;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUid() {
        return uid;
    }

    public String getLrpName() {
        return lrpName;
    }

    public String getLrpDesc() {
        return lrpDesc;
    }

    public String getLrpStartpoint() {
        return lrpStartpoint;
    }

    public String getLrpSLat() {
        return lrpSLat;
    }

    public String getLrpSLng() {
        return lrpSLng;
    }

    public String getLrpEndpoint() {
        return lrpEndpoint;
    }

    public String getLrpELat() {
        return lrpELat;
    }

    public String getLrpELng() {
        return lrpELng;
    }

    public String getLrpDistance() {
        return lrpDistance;
    }

    public String getLrpSubRegion() {
        return lrpSubRegion;
    }

    public String getLrpRegion() {
        return lrpRegion;
    }

    public String getUserId() {
        return userId;
    }

    public ArrayList<Route_model> getRoute_models(){
        return  route_models;
    }

    public void setRoute_models(ArrayList<Route_model> route_models){
        this.route_models = route_models;
    }

}