Home > Software design >  How can I set variable double to the textview in a recyclerview?
How can I set variable double to the textview in a recyclerview?

Time:03-31

I am doing my lab in which I have to create a ViewPager2 application that will show the phone images, model and their prices. To do so, I have created all the necessary activities, java classes, adapters and all. I'm getting an error while setting the value of double to my textView. Please have a look at the code. I'm getting the error at:

holder.txtPrice.setText(c.getPrice());

This is CellphoneAdapter.java

package com.android.lab5;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class CellphoneAdapter extends RecyclerView.Adapter<CellphoneAdapter.MyViewHolder> {
    private ArrayList<Cellphone> cellList;

    public CellphoneAdapter(ArrayList<Cellphone> cellphone){
        cellList = cellphone;
    }

    @NonNull
    @Override
    public CellphoneAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new MyViewHolder(layoutInflater, parent);
    }

    @Override
    public void onBindViewHolder(@NonNull CellphoneAdapter.MyViewHolder holder, int position) {
        Cellphone c = cellList.get(position);

        Context actContext = holder.itemView.getContext();
        int resid = actContext.getResources().getIdentifier(c.getImage(), "drawable", actContext.getPackageName());

        holder.imgPhone.setImageResource(resid);
        holder.txtModel.setText(c.getModel());
        holder.txtPrice.setText(c.getPrice());
    }

    @Override
    public int getItemCount() {
        return cellList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView txtModel, txtPrice;
        ImageView imgPhone;

        public MyViewHolder(LayoutInflater inflater, ViewGroup parent){
            super(inflater.inflate(R.layout.pager_layout, parent, false));
            txtModel = itemView.findViewById(R.id.txtBrand);
            txtPrice = itemView.findViewById(R.id.txtPrice);
            imgPhone = itemView.findViewById(R.id.imgPhone);
        }
    }
}

This is Cellphone.java

package com.android.lab5;

public class Cellphone {
    private String image;
    private String model;
    private double price;

    public Cellphone(String image, String model, double price) {
        this.image = image;
        this.model = model;
        this.price = price;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

This is MainActivity.java

package com.android.lab5;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ViewPager2 vPager;
    private RecyclerView.Adapter adapter;
    ArrayList<Cellphone> cellList = new ArrayList<>();

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

        Cellphone c1 = new Cellphone("galaxy", "Galaxy", 500);
        Cellphone c2 = new Cellphone("iphone", "iPhone", 899);
        Cellphone c3 = new Cellphone("pixel", "Pixel", 699);

        cellList.add(c1);
        cellList.add(c2);
        cellList.add(c3);

        vPager = findViewById(R.id.vPager);

        adapter = new CellphoneAdapter(cellList);

        vPager.setAdapter(adapter);
    }
}

CodePudding user response:

Use String.valueOf() to get a String value


   public static String valueOf(double d) {
        return Double.toString(d);
   }

code:

holder.txtPrice.setText(String.valueOf(c.getPrice()));

CodePudding user response:

SetText needs CharSequence or a Resource id, see link

You might want to make your double a string by using Double.toString()

holder.txtPrice.setText(c.getPrice());

==>

holder.txtPrice.setText(Double.toString(c.getPrice()))
  • Related