我如何在 for 循环中为构造函数编写这些项目?
How can i write these items for constructor in a for-loop?
所以我目前正在为 android 编写应用程序,我在 java/android 中仍然是菜鸟。
无论如何,我有这 2 个字符串,一个带有名称,另一个带有电子邮件,我想使用自定义适配器将它们输出到列表视图中。
到目前为止它工作正常,但我不知道如何动态设置项目(使用 for 循环)。
要创建适配器等,我使用了本教程:
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
我只是将 ImageView 更改为第二个 TextView。
在教程代码中,列表中添加了 5 个项目,但我动态地需要它们,因为我并不总是有相同数量的姓名+电子邮件要输出
我已经尝试通过以下方式将其放入 for 循环中:
Weather weather_data[] = new Weather[names.length];
for(int z=0; z == names.length){
Weather[z]={new Weather(names[z], emails[z])};
}
我也尝试过在前面添加 "new" 并尝试将所有内容设置为空,基本上是反复试验,因为我对此了解不多。
谁能告诉我如何动态添加项目?
(Ps:抱歉,如果我用错了名字来描述任何东西)
这应该有效
Weather weather_data[] = new Weather[names.length];
for(int z=0; z < names.length; z++){
weather_data[z] = new Weather(names[z], emails[z]);
}
阅读本文以了解 for 循环的工作原理
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
这个是数组
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
试试这个
ArrayList<Weather> weatherData = new ArrayList<>();
for (int i=0; i < names.length(); i++){
weatherData.add(new Weather(names[i], emails[i]));
}
然后当你需要它作为 Weather[]
使用 weatherData.toArray()
所以我目前正在为 android 编写应用程序,我在 java/android 中仍然是菜鸟。 无论如何,我有这 2 个字符串,一个带有名称,另一个带有电子邮件,我想使用自定义适配器将它们输出到列表视图中。 到目前为止它工作正常,但我不知道如何动态设置项目(使用 for 循环)。 要创建适配器等,我使用了本教程: http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
我只是将 ImageView 更改为第二个 TextView。
在教程代码中,列表中添加了 5 个项目,但我动态地需要它们,因为我并不总是有相同数量的姓名+电子邮件要输出
我已经尝试通过以下方式将其放入 for 循环中:
Weather weather_data[] = new Weather[names.length];
for(int z=0; z == names.length){
Weather[z]={new Weather(names[z], emails[z])};
}
我也尝试过在前面添加 "new" 并尝试将所有内容设置为空,基本上是反复试验,因为我对此了解不多。
谁能告诉我如何动态添加项目? (Ps:抱歉,如果我用错了名字来描述任何东西)
这应该有效
Weather weather_data[] = new Weather[names.length];
for(int z=0; z < names.length; z++){
weather_data[z] = new Weather(names[z], emails[z]);
}
阅读本文以了解 for 循环的工作原理
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
这个是数组
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
试试这个
ArrayList<Weather> weatherData = new ArrayList<>();
for (int i=0; i < names.length(); i++){
weatherData.add(new Weather(names[i], emails[i]));
}
然后当你需要它作为 Weather[]
使用 weatherData.toArray()