尝试用fori(Java,Android)填充数组列表



我有一个ArrayList,它由具有以下字段的项组成:

ImageView recipePic;
TextView recipeName;
TextView recipeIngridients;
TextView recipeLong;

我将项目添加到ArrayList中,如下所示:

recyclerViewItems.add(new RecyclerViewItem(R.drawable.porridge1,
getString(R.string.recipe_name_1), getString(R.string.recipe_short_1),
getString(R.string.recipe_ingridients_1),
getString(R.string.recipe_long_1)));
recyclerViewItems.add(new RecyclerViewItem(R.drawable.porridge2,
getString(R.string.recipe_name_2),getString(R.string.recipe_short_2),
getString(R.string.recipe_ingridients_2),
getString(R.string.recipe_long_2))); 

等等。

我想让代码更短、更漂亮,并产生了这样的代码:

for (int i = 1; i < 4; i++) {
recyclerViewItems.add(new RecyclerViewItem(Integer.parseInt("R.drawable.porridge"+i),
getString(Integer.parseInt("R.string.recipe_name_"+i)),
getString(Integer.parseInt("R.string.recipe_short_"+i)),
getString(Integer.parseInt("R.string.recipe_ingridients_"+i)),
getString( Integer.parseInt("R.string.recipe_long_"+i))));        }

由于所有资源都遵循相同的命名方案,我认为这样的代码会更好,但它不起作用。我认为这是因为你不能从";R.drawing.porridge1";即使";R.drawing.porridge1";计数为整数。我理解得对吗?虽然串联只适用于字符串。。。是否有类似的方法来升级第一个解决方案?

您可以将项目作为数组存储在XML资源中

对于name、Ingredients、Long等字符串,您将使用字符串数组例如

<string-array name="numbers">
<item>@string/one</item>
<item>@string/two</item>
<item>@string/three</item>
</string-array>

对于可提取的,使用整数阵列,例如

<integer-array name="drawables">
<item>@drawable/one</item>
<item>@drawable/two</item>
<item>@drawable/three</item>
</integer-array>

并使用TypedArray读取每个图像id,例如

TypedArray array = getResources().obtainTypedArray(R.array.drawables);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(array.getResourceId(index, defaultValue));

现在你可以使用for循环来填充你的ArrayList

for(int i = 0 ; i < n ; i++) {
recyclerViewItems.add(new RecyclerViewItem(
array.getResourceId(i,-1),
recipeName[i],
recipeShort[i],
recipeIngridients[i],
recipeLong[i]));
}

感谢AmrDeveloper,我理解了所有这些应该如何工作。

首先,将所有字符串添加到arrays.xml中的数组中,如

<array name="recipeLongs">
<item>@string/recipe_long_1</item>
<item>@string/recipe_long_2</item>
<item>@string/recipe_long_3</item>
</array>
<array name="recipePics">
<item>@drawable/porridge1</item>
<item>@drawable/porridge2</item>
<item>@drawable/porridge3</item>
</array>

无需制作特殊类型的数组。

然后我在MainActivity中添加了以下代码:

TypedArray picsArray = getResources().obtainTypedArray(R.array.recipePics);
TypedArray recipeNamesArray = getResources().obtainTypedArray(R.array.recipeNames);
TypedArray recipeShortsArray = getResources().obtainTypedArray(R.array.recipeShorts);
TypedArray recipeIngridientsArray = getResources().obtainTypedArray(R.array.recipeIngridients);
TypedArray recipeLongsArray = getResources().obtainTypedArray(R.array.recipeLongs);
for (int i = 0; i < 3; i++) {
recyclerViewItems.add(new RecyclerViewItem(
picsArray.getResourceId(i,-1),
getString(recipeNamesArray.getResourceId(i,-1)),
getString(recipeShortsArray.getResourceId(i,-1)),
getString(recipeIngridientsArray.getResourceId(i,-1)),
getString(recipeLongsArray.getResourceId(i,-1))));
}

最新更新