【问题标题】:How to cast int to set inside textView [duplicate]如何将int转换为在textView中设置[重复]
【发布时间】:2018-08-01 16:01:23
【问题描述】:

当我将 productQuantity 字符串更改为整数应用程序崩溃时遇到问题,我无法理解错误,但他将我带到这里:

holder.quantity.setText(currentItem.getProductQuantity());

当我投射 setText(currentItem.getProductQuantity()) 时它不起作用

package com.original.original.original.admin;

public class ProductItem {
    String productName;
    String productCode;

    String productBuyPrice;
    String productSalePrice;
    int productQuantity;
    private String key;

    public ProductItem() {
    }

    public ProductItem(String productName, String productCode, String productBuyPrice, String productSalePrice, int productQuantity) {
        this.productName = productName;
        this.productCode = productCode;
        this.productBuyPrice = productBuyPrice;
        this.productSalePrice = productSalePrice;
        this.productQuantity = productQuantity;
    }

//    @Exclude
    public String getKey() {
        return key;
    }

//    @Exclude
    public void setKey(String key) {
        this.key = key;
    }

    public String getProductCode() {
        return productCode;
    }

    public String getProductName() {
        return productName;
    }

    public String getProductBuyPrice() {
        return productBuyPrice;
    }

    public String getProductSalePrice() {
        return productSalePrice;
    }

    public int getProductQuantity() {
        return productQuantity;
    }
}

还有这个onBindView 方法

@Override
    public void onBindViewHolder(productViewHolder holder, int position) {
        ProductItem currentItem = items.get(position);

        holder.name.setText(currentItem.getProductName());
        holder.code.setText(currentItem.getProductCode());
        holder.buyPrice.setText(currentItem.getProductBuyPrice());
        holder.salePrice.setText(currentItem.getProductSalePrice());
        holder.quantity.setText(currentItem.getProductQuantity());


    }

【问题讨论】:

  • 使用 String.valueOf()

标签: java android firebase android-recyclerview android-viewholder


【解决方案1】:

试试String.valueOf(YOUR_VALUE);

类似textView.setText(String.valueOf(7));

我想,在你的情况下,

holder.quantity.setText(String.valueOf(currentItem.getProductQuantity()));

【讨论】:

    【解决方案2】:

    From android documentation setText(int 残差) 使用字符串资源标识符设置要显示的文本。

    它崩溃的原因是,系统认为您正在调用 textview.setText(int resId) 方法并尝试查找具有您作为整数传递给 setText() 的 id 的资源。

    所以你需要先将数字转换为字符串,然后再设置为textview。

    【讨论】:

      【解决方案3】:

      只需转换这一行:

      holder.quantity.setText(currentItem.getProductQuantity());
      

      holder.quantity.setText(currentItem.getProductQuantity()+"");
      

      它会工作,但警告产品数量应该始终获取并返回整数,否则应用程序将崩溃

      【讨论】:

        【解决方案4】:

        如果您确定“holder.quantity”和“currentItem”不为空,请使用此代码

        holder.quantity.setText(String.valueOf(currentItem.getProductQuantity()));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-29
          • 2017-07-17
          • 2015-03-01
          • 2013-12-03
          • 1970-01-01
          • 2021-08-14
          • 2016-06-02
          • 2013-08-01
          相关资源
          最近更新 更多