java - 如何在onBindViewHolder方法中管理字符串数组?

标签 java android android-studio android-recyclerview

我有一个餐厅应用程序,我想将我的产品菜单构建为回收 View ,在每个卡片 View 中包含图像、名称、成分、价格和其他按钮。我在构建字符串成分数组时遇到问题,因为我想根据每个产品的 Firebase 数据添加它们。这是我的产品类别:

public class MenuObject {
private String image, name, quantity,  price;
private int increment, decrement, button;
private String[] ingredients;

public MenuObject(){}

public MenuObject(String image, String name, String[] ingredients, String price, int increment, String quantity, int decrement, int button){
    this.image = image;
    this.name = name;
    this.ingredients = ingredients;
    this.price = price;
    this.increment = increment;
    this.quantity = quantity;
    this.decrement = decrement;
    this.button = button;
}

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

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String[] getIngredients() {
    return ingredients;
}
public void setIngredients(String[] ingredients) {
    this.ingredients = ingredients;
}

public String getPrice() {
    return price;
}
public void setPrice(String price) {
    this.price = price;
}

public String getQuantity() {
    return quantity;
}
public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public int getDecrement() {
    return decrement;
}
public void setDecrement(int decrement) {
    this.decrement = decrement;
}

public int getIncrement() {
    return increment;
}
public void setIncrement(int increment) {
    this.increment = increment;
}

public int getButton() {
    return button;
}
public void setButton(int button) {
    this.button = button;
}

这是方法中给我带来麻烦的行:

holder.ingredients.setText(currentMenuObject.getIngredients());

但是,在我的适配器中,错误消息显示:

no suitable method found for setText(String[])
holder.ingredients.setText(currentMenuObject.getIngredients()); method TextView.setText(CharSequence) is not applicable (argument mismatch; String[] cannot be converted to CharSequence) method TextView.setText(int) is not applicable

我不知道如何在 onBindViewHolder 方法中管理字符串数组,或者是否需要更改类中的某些内容。

最佳答案

您无法将字符串数组设置为 TextView setText 方法。您必须附加数组中的字符串才能创建单个字符串。然后将其设置为您的 TextView

例如:

if(currentMenuObject.getIngredients() != null) {
       String str ="";
        StringBuilder str = new StringBuilder();
            for (String s : currentMenuObject.getIngredients()) {
                str.append(",").append(s);
            }

    if (currentMenuObject.getIngredients().length > 0) {
        str.deleteCharAt(0); // remove first comma
    }
        holder.ingredients.setText(str.toString());
}

这将添加以逗号分隔的字符串数组的值(“,”)。

关于java - 如何在onBindViewHolder方法中管理字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62403101/

相关文章:

java - 是否可以在 catch 中重新分配最终变量,即使分配是 try 中的最后一个操作?

java - 如何在我的 Android 应用程序中保存一组简单对象?

android - 如何在 Android Studio 中使用我的上传 key (没有 keystore )签署我的 android 应用程序包?

android - style.xml 和 theme.xml android studio 之间的区别?

java - 无法在索引 2 处绑定(bind)参数,因为索引超出范围

java - Java中: List, ArrayList和Map、HashMap

java - 使用 Spring JPA 为 STRING_AGG 和 Postgresql 为 Group_By 设置聚合函数

android - 无法为混合 Android 应用程序运行 'forcedroid create'

android - 设置 Google Analytics 以触发具有维度更改的新 session

android - 如何在VSC中运行和调试React Native Android应用程序?