1

I'm trying to make a ListView that reads from an array written in the default 'string' XML. My thought is that when I make an array adapter in the Java class, I cannot directly refer the adapter to an XML value. The adapter requests a Java value, which brings up the most important part of my question: How do I cast an XML string-array into a Java string array?

Here is my (relevant) code so far:

XML:

<string-array name="courseNames">
    <item>Lesson 1</item>
    <item>Lesson 2</item>
    <item>Lesson 3</item>
    <item>Lesson 4</item>
    <item>Lesson 5</item>
    <item>Lesson 6</item>
</string-array>

Java:

ListView lv1 = (ListView) findViewById(R.id.listViewID);

lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, R.array.courseNames));

But I cannot place courseNames as an argument as it requires a Java array.

1 Answer 1

1
<string-array name="courseNames">
    <item>Lesson 1</item>
    <item>Lesson 2</item>
    <item>Lesson 3</item>
    <item>Lesson 4</item>
    <item>Lesson 5</item>
    <item>Lesson 6</item>
</string-array>

In this case, you can use:

List<String> Lines = Arrays.asList(getResources().getStringArray(R.array.courseNames));
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but it still does not show the course names in the graphical layout. Edit: Nevermind, it is NOT showing in the graphical layout but it does show up when tested on mobile. Thanks!
yeah, you wont get a listview populated on the studio unless its executed on an emulator or device! mark it as answer if this was helpful! :)

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.