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

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

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

&lt;string-array name=&quot;numbers&quot;&gt;
    &lt;item&gt;@string/one&lt;/item&gt;
    &lt;item&gt;@string/two&lt;/item&gt;
    &lt;item&gt;@string/three&lt;/item&gt;
&lt;/string-array&gt;

and for drawables use integer-array for example

&lt;integer-array name=&quot;drawables&quot;&gt;
    &lt;item&gt;@drawable/one&lt;/item&gt;
    &lt;item&gt;@drawable/two&lt;/item&gt;
    &lt;item&gt;@drawable/three&lt;/item&gt;
&lt;/integer-array&gt;

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 &lt; 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

&lt;array name=&quot;recipeLongs&quot;&gt;
        &lt;item&gt;@string/recipe_long_1&lt;/item&gt;
        &lt;item&gt;@string/recipe_long_2&lt;/item&gt;
        &lt;item&gt;@string/recipe_long_3&lt;/item&gt;
    &lt;/array&gt;
    &lt;array name=&quot;recipePics&quot;&gt;
        &lt;item&gt;@drawable/porridge1&lt;/item&gt;
        &lt;item&gt;@drawable/porridge2&lt;/item&gt;
        &lt;item&gt;@drawable/porridge3&lt;/item&gt;
    &lt;/array&gt;

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 &lt; 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))));
         }

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:

确定