1

So I have the custom adapter for ListView

I can set values like this:

dataModels.add(new DataModel("Apple Pie", "Android 1.0", "1","September 23, 2008"));
dataModels.add(new DataModel("Banana Bread", "Android 1.1", "2","February 9, 2009"));
...

But I need to set data from some different R.array's

<string-array name="Name">
    <item>Apple Pie</item>
    <item>Banana Bread</item>
</string-array>
<string-array name="Version">
    <item>Android 1.0</item>
    <item>Android 1.1</item>
</string-array>
...
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

You have to build each DataModel from arrays

ArrayList<DataModel> dataModels = new ArrayList<DataModel>();

for (int i = 0; i < getResources().getStringArray(R.array.Name).length; i++) {
    dataModels.add(new DataModel(
            getResources().getStringArray(R.array.Name)[i],
            getResources().getStringArray(R.array.Version)[i],
            getResources().getStringArray(R.array.Id)[i],
            getResources().getStringArray(R.array.Date)[i]));
}
alireza
  • 514
  • 3
  • 14
  • How can I dynamically change R.array."THIS VALUE" in code? For example, I have Name_1, Name_2, Name_3,.... Can I write something like this: (R.array.Name_ + i) (pseudocode)? @alireza – Abdulgafur Bersugir Sep 11 '18 at 19:11
  • @AbdulgafurBersugir [link](https://stackoverflow.com/questions/18707553/use-dynamic-r-strings-in-android) I haven't tried it but it should work – alireza Sep 11 '18 at 20:08