0

I'm stumped trying to figure out how to compare two pricesto determine which one is larger and which one is smaller using if/else statements. By using the arrows to denote it

error: double cannot be dereferenced

As it stands, whenever I compile my code I get this error:

public class MeetAdapter extends RecyclerView.Adapter<MeetAdapter.ProductViewHolder> {
    private Context mCtx;
    private List<Meet> meetList;
    public MeetAdapter(Context mCtx, List<Meet> meetList) {
        this.mCtx = mCtx;
        this.meetList = meetList;
    }
    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.meat_list_row, null);
        return new ProductViewHolder(view);
    }
    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
        Meet meet = meetList.get(position);
        Picasso.with(mCtx)
                .load(meet.getUrl_id())
                .into(holder.imageViewM);
        holder.textViewName.setText(meet.getName());
        holder.newPrice.setText(String.valueOf(meet.getPrice()));
        holder.old_price.setText(meet.getOld_price());
        holder.textViewDate_id.setText(meet.getDate_id());
    }
    @Override
    public int getItemCount() {
        return meetList.size();
    }
    class ProductViewHolder extends RecyclerView.ViewHolder {
        TextView textViewName, old_price, newPrice, textViewDate_id;
        ImageView imageViewM, imageViewD;
        public ProductViewHolder(View itemView) {
            super(itemView);
            textViewName = itemView.findViewById(R.id.meat_name);
            //old_price = itemView.findViewById(R.id.meat_Direction);
            newPrice = itemView.findViewById(R.id.meat_price);
            textViewDate_id = itemView.findViewById(R.id.meat_date);
            imageViewM = itemView.findViewById(R.id.image_meat);
            imageViewD = itemView.findViewById(R.id.meat_Direction);
            double newPrice = (double) newPrice.getText().toString();
            double old_price = (double) old_price.getText().toString();
            if (newPrice > old_price) {
                imageViewD.setImageResource(R.drawable.arrow_up);
            }if (newPrice < old_price){
                imageViewD.setImageResource(R.drawable.arrow_down);
            }else {
                imageViewD.setImageResource(R.drawable.arrow_tow);
            }
        }
    }
}

the error

1 Answers1

0

Use the same syntax for same type of variables: don't mix them. Also you're casting a string to a double without using the proper converter.

oldPrice = itemView.findViewById(R.id.meat_Direction);
newPrice = itemView.findViewById(R.id.meat_price);
...
...
double old_price = Double.parseDouble(oldPrice.getText().toString())
double new_price = Double.parseDouble(newPrice.getText().toString())

Then do the comparison.

Marco
  • 352
  • 2
  • 14