ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, R.array.lsViewArray );
lsViewArray.XML:
<resources>
<string-array name="lsViewArray">
<item>Magenta</item>
<item>Green</item>
<item>Blue</item>
<item>Red</item>
<item>Black</item>
<item>Orange</item>
<item>Purple</item>
<item>Pink</item>
<item>White</item>
<item>Violet</item>
<item>Saffron</item>
<item>Yellow</item>
<item>Brown</item>
</string-array>
</resources>
It is giving me error due R.array.lsViewArray
error :- Expected resource of type id
What can be done except making an Array in MainActivity
i want to access it through lsViewArray.xml
or some other method that I should be knowing
With your current code, you're using R.array.lsViewArray
as your textViewResourceId. The textViewResourceId is the id of the TextView within the layout resource to be populated. In your case, the layout resource to be populated is the android.R.layout.simple_list_item1
. It doesn't contain any Textview with the id R.array.lsViewArray
so you're getting the error.
To fix this, you can use a constructor that accepts T[] objects.
First, get the array from your resources. To do this, type:
String[] lsViewArrayData = getResources().getStringArray(R.array.lsViewArray);
Then create your adapter and pass the object lsViewArrayData:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, lsViewArrayData);