英文:
Trying to fill Array list with 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.drawable.porridge1"中解析出整数,即使"R.drawable.porridge1"被视为整数。我理解得对吗?尽管只有字符串才能进行连接... 是否有类似的方法来升级第一个解决方案?
英文:
I've got an ArrayList consisting of items that have such fields:
ImageView recipePic;
TextView recipeName;
TextView recipeIngridients;
TextView recipeLong;
And I add items to the ArrayList like this:
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)));
etc.
I wanted to make code shorter and pretier and produced such code:
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)))); }
As all the resources follow the same naming scheme I thought that such code would be better but it doesn't work. I think it's because you can't parse integer from "R.drawable.porridge1" even though "R.drawable.porridge1" counts as integer. Am I understanding this right? Although concatenating only works on strings... Are there similiar ways of upgrading first solution?
答案1
得分: 0
你可以将项目存储为 XML 资源中的数组。
对于像 name、Ingredients、Long 这样的字符串,你将使用 string-array,例如:
<string-array name="numbers">
<item>@string/one</item>
<item>@string/two</item>
<item>@string/three</item>
</string-array>
对于可绘制资源,使用 integer-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]));
}
英文:
You can store items as arrays in XML resources
for strings like name, Ingredients, Long you will use string-array
for example
<string-array name="numbers">
<item>@string/one</item>
<item>@string/two</item>
<item>@string/three</item>
</string-array>
and for drawables use integer-array for example
<integer-array name="drawables">
<item>@drawable/one</item>
<item>@drawable/two</item>
<item>@drawable/three</item>
</integer-array>
and use TypedArray to read every image id for example
TypedArray array = getResources().obtainTypedArray(R.array.drawables);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(array.getResourceId(index, defaultValue));
and now you can use for loop to fill your ArrayList
for(int i = 0 ; i < n ; i++) {
recyclerViewItems.add(new RecyclerViewItem(
array.getResourceId(i,-1),
recipeName[i],
recipeShort[i],
recipeIngridients[i],
recipeLong[i]));
}
答案2
得分: 0
感谢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))));
}
希望这对您有帮助!
英文:
Thanks to AmrDeveloper I understood how all this should work.
First you add all the strings to arrays in arrays.xml like
<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>
No need to make special types of arrays.
Then I've added following code to 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))));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论