Loading string values from a String or file instead of a "list" in the class file
I think I have a pretty easy problem to solve, but I have been beating my head against the wall for hours trying to get past it!
I have an adapter tha开发者_如何学Got loads a list of URLS:
adapter=new MyAdapter(this, lStrings);
list.setAdapter(adapter);
The list of URLS looks like this:
private String[] lStrings={
"http://www.domain.com/file1.jpg",
"http://www.domain.com/file2.jpg",
"http://www.domain.com/file3.jpg",
};
What I want instead is to load these values from a text file that lies on the SD card. For that matter, I would be okay with loading the values from a text file into a String, and then load the String into the list as I imagine that would be the "cleaner approach". However, all attempts to do this have failed.
For instance, I replaced the above snippet with this:
private String[] lStrings=
{
MainActivity.this.getString(R.string.myurllist)
};
... but then I get a Forced Close on loading.
I'm a bit new to Java, Android, and development in general. So if this is something really stupid, please excuse my ignorance.
Thanks in advance for any advice you can offer me.
It looks like you can use getStringArray(resourceId)
instead of getString
.
In this case you would create a string array resource (in arrays.xml) which would look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
<item>violet</item>
</string-array>
</resources>
Then your code would be:
private String[] lStrings= getStringArray(R.array.myurllist)
Check here for more: http://developer.android.com/resources/samples/ApiDemos/res/values/arrays.html
精彩评论