尝试使用fori在ArrayList中填充数组列表(Java,Android)

huangapple go评论95阅读模式
英文:

Trying to fill Array list with fori (Java, Android)

问题

我有一个包含以下字段的ArrayList:

  1. ImageView recipePic;
  2. TextView recipeName;
  3. TextView recipeIngridients;
  4. TextView recipeLong;

我是这样向ArrayList中添加项目的:

  1. recyclerViewItems.add(new RecyclerViewItem(R.drawable.porridge1,
  2. getString(R.string.recipe_name_1), getString(R.string.recipe_short_1),
  3. getString(R.string.recipe_ingridients_1),
  4. getString(R.string.recipe_long_1)));
  5. recyclerViewItems.add(new RecyclerViewItem(R.drawable.porridge2,
  6. getString(R.string.recipe_name_2),getString(R.string.recipe_short_2),
  7. getString(R.string.recipe_ingridients_2),
  8. getString(R.string.recipe_long_2)));

等等。

我想要让代码更短更漂亮,编写了以下代码:

  1. for (int i = 1; i < 4; i++) {
  2. recyclerViewItems.add(new RecyclerViewItem(Integer.parseInt("R.drawable.porridge"+i),
  3. getString(Integer.parseInt("R.string.recipe_name_"+i)),
  4. getString(Integer.parseInt("R.string.recipe_short_"+i)),
  5. getString(Integer.parseInt("R.string.recipe_ingridients_"+i)),
  6. 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:

  1. ImageView recipePic;
  2. TextView recipeName;
  3. TextView recipeIngridients;
  4. TextView recipeLong;

And I add items to the ArrayList like this:

  1. recyclerViewItems.add(new RecyclerViewItem(R.drawable.porridge1,
  2. getString(R.string.recipe_name_1), getString(R.string.recipe_short_1),
  3. getString(R.string.recipe_ingridients_1),
  4. getString(R.string.recipe_long_1)));
  5. recyclerViewItems.add(new RecyclerViewItem(R.drawable.porridge2,
  6. getString(R.string.recipe_name_2),getString(R.string.recipe_short_2),
  7. getString(R.string.recipe_ingridients_2),
  8. getString(R.string.recipe_long_2)));

etc.

I wanted to make code shorter and pretier and produced such code:

  1. for (int i = 1; i < 4; i++) {
  2. recyclerViewItems.add(new RecyclerViewItem(Integer.parseInt("R.drawable.porridge"+i),
  3. getString(Integer.parseInt("R.string.recipe_name_"+i)),
  4. getString(Integer.parseInt("R.string.recipe_short_"+i)),
  5. getString(Integer.parseInt("R.string.recipe_ingridients_"+i)),
  6. 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,例如:

  1. <string-array name="numbers">
  2. <item>@string/one</item>
  3. <item>@string/two</item>
  4. <item>@string/three</item>
  5. </string-array>

对于可绘制资源,使用 integer-array,例如:

  1. <integer-array name="drawables">
  2. <item>@drawable/one</item>
  3. <item>@drawable/two</item>
  4. <item>@drawable/three</item>
  5. </integer-array>

并使用 TypedArray 读取每个图像的 ID,例如:

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

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

  1. for(int i = 0 ; i < n ; i++) {
  2. recyclerViewItems.add(new RecyclerViewItem(
  3. array.getResourceId(i,-1),
  4. recipeName[i],
  5. recipeShort[i],
  6. recipeIngridients[i],
  7. recipeLong[i]));
  8. }
英文:

You can store items as arrays in XML resources

for strings like name, Ingredients, Long you will use string-array
for example

  1. &lt;string-array name=&quot;numbers&quot;&gt;
  2. &lt;item&gt;@string/one&lt;/item&gt;
  3. &lt;item&gt;@string/two&lt;/item&gt;
  4. &lt;item&gt;@string/three&lt;/item&gt;
  5. &lt;/string-array&gt;

and for drawables use integer-array for example

  1. &lt;integer-array name=&quot;drawables&quot;&gt;
  2. &lt;item&gt;@drawable/one&lt;/item&gt;
  3. &lt;item&gt;@drawable/two&lt;/item&gt;
  4. &lt;item&gt;@drawable/three&lt;/item&gt;
  5. &lt;/integer-array&gt;

and use TypedArray to read every image id for example

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

and now you can use for loop to fill your ArrayList

  1. for(int i = 0 ; i &lt; n ; i++) {
  2. recyclerViewItems.add(new RecyclerViewItem(
  3. array.getResourceId(i,-1),
  4. recipeName[i],
  5. recipeShort[i],
  6. recipeIngridients[i],
  7. recipeLong[i]));
  8. }

答案2

得分: 0

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

首先,您需要将所有字符串添加到arrays.xml中的数组中,如下所示:

  1. <array name="recipeLongs">
  2. <item>@string/recipe_long_1</item>
  3. <item>@string/recipe_long_2</item>
  4. <item>@string/recipe_long_3</item>
  5. </array>
  6. <array name="recipePics">
  7. <item>@drawable/porridge1</item>
  8. <item>@drawable/porridge2</item>
  9. <item>@drawable/porridge3</item>
  10. </array>

不需要创建特殊类型的数组。

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

  1. TypedArray picsArray = getResources().obtainTypedArray(R.array.recipePics);
  2. TypedArray recipeNamesArray = getResources().obtainTypedArray(R.array.recipeNames);
  3. TypedArray recipeShortsArray = getResources().obtainTypedArray(R.array.recipeShorts);
  4. TypedArray recipeIngridientsArray = getResources().obtainTypedArray(R.array.recipeIngridients);
  5. TypedArray recipeLongsArray = getResources().obtainTypedArray(R.array.recipeLongs);
  6. for (int i = 0; i < 3; i++) {
  7. recyclerViewItems.add(new RecyclerViewItem(
  8. picsArray.getResourceId(i, -1),
  9. getString(recipeNamesArray.getResourceId(i, -1)),
  10. getString(recipeShortsArray.getResourceId(i, -1)),
  11. getString(recipeIngridientsArray.getResourceId(i, -1)),
  12. getString(recipeLongsArray.getResourceId(i, -1))));
  13. }

希望这对您有帮助!

英文:

Thanks to AmrDeveloper I understood how all this should work.

First you add all the strings to arrays in arrays.xml like

  1. &lt;array name=&quot;recipeLongs&quot;&gt;
  2. &lt;item&gt;@string/recipe_long_1&lt;/item&gt;
  3. &lt;item&gt;@string/recipe_long_2&lt;/item&gt;
  4. &lt;item&gt;@string/recipe_long_3&lt;/item&gt;
  5. &lt;/array&gt;
  6. &lt;array name=&quot;recipePics&quot;&gt;
  7. &lt;item&gt;@drawable/porridge1&lt;/item&gt;
  8. &lt;item&gt;@drawable/porridge2&lt;/item&gt;
  9. &lt;item&gt;@drawable/porridge3&lt;/item&gt;
  10. &lt;/array&gt;

No need to make special types of arrays.

Then I've added following code to MainActivity:

  1. TypedArray picsArray = getResources().obtainTypedArray(R.array.recipePics);
  2. TypedArray recipeNamesArray = getResources().obtainTypedArray(R.array.recipeNames);
  3. TypedArray recipeShortsArray = getResources().obtainTypedArray(R.array.recipeShorts);
  4. TypedArray recipeIngridientsArray = getResources().obtainTypedArray(R.array.recipeIngridients);
  5. TypedArray recipeLongsArray = getResources().obtainTypedArray(R.array.recipeLongs);
  6. for (int i = 0; i &lt; 3; i++) {
  7. recyclerViewItems.add(new RecyclerViewItem(
  8. picsArray.getResourceId(i,-1),
  9. getString(recipeNamesArray.getResourceId(i,-1)),
  10. getString(recipeShortsArray.getResourceId(i,-1)),
  11. getString(recipeIngridientsArray.getResourceId(i,-1)),
  12. getString(recipeLongsArray.getResourceId(i,-1))));
  13. }

huangapple
  • 本文由 发表于 2020年8月9日 22:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63327762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定