android Recyclerview无法从Firebase检索双精度数据/值

3htmauhk  于 2022-12-28  发布在  Android
关注(0)|答案(1)|浏览(139)

在使用FirebaseRecylerAdapter实现RecyclerView时,我很难检索到准确的双精度数据。为了澄清我的问题,我收到了正确的双精度数据,但没有小数值。双精度数据显示为整数和零小数(例如100.0、40.0、23.0)
适配器类别

public class MyAdapter extends FirebaseRecyclerAdapter<UserDrive, MyAdapter.MyViewHolder> {

    public MyAdapter(@NonNull FirebaseRecyclerOptions<UserDrive> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull UserDrive model) {

        //double ma_ovScore = (double) Math.round(Double.valueOf(model.getOvScore())*100)/100;
        holder.adptr_ovScore.setText(String.valueOf(model.getOvScore()));
        Log.i("OvScore", ""+Double.parseDouble(String.valueOf(model.getOvScore())));
        holder.adptr_dateTime.setText(DateFormat.format("MM.dd.yyyy", new Date(model.getDateTime())).toString());

        holder.adptr_ovScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppCompatActivity activity = (AppCompatActivity) view.getContext();
                activity.getSupportFragmentManager().beginTransaction().replace(R.id.wrapper, new descFragment(model.getOvScore(),model.getDateTime(),model.getDistance(),model.getManualDistractionScore(),model.getsafeDrive(),model.gettexting(),model.gettalkPhone(),model.getoperatingRad(),model.getdrinking(),model.getreachBehind())).addToBackStack(null).commit();
            }
        });

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerowdesign,parent,false);
        return new MyViewHolder(view);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView adptr_ovScore, adptr_dateTime;

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

            adptr_ovScore = itemView.findViewById(R.id.new_ovScore);
            adptr_dateTime = itemView.findViewById(R.id.new_dateTime);
        }
    }
}

用户驱动器类

public class UserDrive{
    public double ovScore=100.0;
    public double distance;

    public double manualDistractionScore;

    public long dateTime;

    //classes
    private double safeDrive;
    private double texting;
    private double talkPhone;
    private double operatingRad;
    private double drinking;
    private double reachBehind;

    public UserDrive() {
    }

    public UserDrive(double safeDrive, double texting, double talkPhone, double operatingRad, double drinking,
                     double reachBehind, double ovScore, double distance, double manualDistractionScore){
        this.safeDrive=safeDrive;
        this.texting=texting;
        this.talkPhone=talkPhone;
        this.operatingRad=operatingRad;
        this.drinking=drinking;
        this.reachBehind=reachBehind;

        this.ovScore=ovScore;
        this.distance=distance;
        this.manualDistractionScore=manualDistractionScore;
    }

    public double getOvScore(){

        return (double) ovScore;
    }

    public double getDistance(){
        return (double) distance;
    }

    public double getManualDistractionScore() {
        return manualDistractionScore;
    }

    public long getDateTime() {
        return dateTime;
    }

    public void setDateTime(long dateTime){
        this.dateTime=dateTime;
    }

    //getValueEachClass
    public double getsafeDrive(){return (double) safeDrive;}
    public double gettexting(){return (double) texting;}
    public double gettalkPhone(){return (double) talkPhone;}
    public double getoperatingRad(){return (double) operatingRad;}
    public double getdrinking(){return (double) drinking;}
    public double getreachBehind(){return (double) reachBehind;}

    public void setSafeDrive(double safeDrive) {
        this.safeDrive = safeDrive;
    }
    public void setTexting(double texting) {
        this.texting = texting;
    }
    public void setTalkPhone(double talkPhone) {
        this.talkPhone = talkPhone;
    }
    public void setOperatingRad(double operatingRad) {
        this.operatingRad = operatingRad;
    }
    public void setDrinking(double drinking) {
        this.drinking = drinking;
    }
    public void setReachBehind(double reachBehind) {
        this.reachBehind = reachBehind;
    }

    public void setManualDistractionScore(double manualDistractionScore) { this.manualDistractionScore = manualDistractionScore; }

    public void setOvScore(int ovScore){this.ovScore=ovScore;}

    public void deductOvScore(double deduct){
        this.ovScore=ovScore-deduct;
    }
}

火线初始值:

日志输出:

预期输出应为精确的双精度值,即两位小数(例如93.34、92.13)

zyfwsgd6

zyfwsgd61#

代码中的问题在于使用了一个不正确的setter:

public void setOvScore(int ovScore){this.ovScore=ovScore;}

参数的类型是int而不是double,因此会出现这种情况,要解决这个问题,必须将参数的类型从int改为double:

public void setOvScore(double ovScore){this.ovScore=ovScore;}
//                      👆

相关问题