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

问题描述 投票:0回答:1

我有一个餐厅应用程序,我想将我的产品菜单构建为recyclerview,在每个cardview中都包含图像,名称,成分,价格和其他按钮。我在构建字符串的成分数组时遇到问题,因为我想根据每个产品的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());

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

找不到适合于setText(String [])的方法 holder.ingredients.setText(currentMenuObject.getIngredients());方法 TextView.setText(CharSequence)不适用 (参数不匹配; String []无法转换为CharSequence) 方法TextView.setText(int)不适用

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

java android android-studio recycler-adapter
1个回答
0
投票

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

例如:

String str ="";
StringBuilder str = new StringBuilder();
    for (String s : currentMenuObject.getIngredients()) {
        str.append(" ").append(s);
    }
holder.ingredients.setText(str.toString());

这将添加由空格(“”)分隔的字符串数组的值。

© www.soinside.com 2019 - 2024. All rights reserved.