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>
...

1 Answer 1

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]));
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
@AbdulgafurBersugir link I haven't tried it but it should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.