gpt4 book ai didi

android - 如何给listView中的每一项赋值?

转载 作者:行者123 更新时间:2023-11-30 01:21:44 25 4
gpt4 key购买 nike

我正在开发与健康相关的应用程序。在 ListView 中,我想为每个项目分配值。例如 milk 包含 21 卡路里 所以我想将 21 分配给 ListView item milk

这是我的 Activity 代码,包含 ListView

public class FoodEntry extends AppCompatActivity {

// Array of strings...
String[] food = {"Naan","Pav bhaji","chole tikiya", "rice", "soybean", "milk", "curd"};

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

ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.activity_item, food);
ListView listViewFoodItems = (ListView)findViewById(R.id.listViewFood);
listViewFoodItems.setAdapter(adapter);
}
}

最佳答案

而不是像您在这里那样使用 String[] 来存储您的数据:

// Array of strings...
String[] food = {"Naan","Pav bhaji","chole tikiya", "rice", "soybean", "milk", "curd"};

使用新对象,例如 CalorieCount。

// Array of CalorieCount
CalorieCount[] food = { new CalorieCount("Naan", 20) ... };

关于android - 如何给listView中的每一项赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36988668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com