0

First, lemme show my string that populates a listview:

static final String[] title = new String[] {
    "Temaki Sushi", "Oyakodon", "Okonomiyaki", "Tofu Dango", "Oden",
    "Nikujaga", "Yellowtail Teriyaki", "Tendon", "Tonjiru", "Sukiyaki",
    };

Now, the question is, instead of hardcoding those items, is there any way to reference the items to an external text file in the res/raw folder? Lets say I have a text file titled titles.txt in raw folder.

borislemke
  • 8,446
  • 5
  • 41
  • 54
  • why not you use android String arrays concept? – kosa Jan 07 '12 at 13:56
  • I have a big database, in this example its only like 10 items. But when the app is completed, it will contain more than 70 items and will be updated every week. I find it easier to edit the text file whenever I need to make updates or changes, agree? Or is string array still easier? – borislemke Jan 07 '12 at 13:58
  • You need something like this: http://stackoverflow.com/questions/2453989/help-in-getting-string-array-from-arrays-xml-file – Paulo Almeida Jan 07 '12 at 14:03
  • Ok, may be you can use assets folder to store your file and write. Here is how someone tried. http://stackoverflow.com/questions/5086539/reading-file-from-assets-directory-throws-filenotfoundexception – kosa Jan 07 '12 at 14:05

2 Answers2

2

yes, you can do it

you should read the text like this

private String readTitlesRaw(){
    InputStream stream = Context.getResources().openRawResource(R.raw.titles))

    Writer writer = new StringWriter();
    char[] buffer = new char[10240];
    try {
        Reader reader = new BufferedReader(
        new InputStreamReader(stream, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        stream.close();
    }
    return writer.toString();
}

and then split it to array as you like, some thing like this should work

String[] titles=readTitlesRaw().split("SPLITER");

when your write SPLITER between each title

ben or
  • 653
  • 5
  • 17
  • I get this error on the Context.getResources() line: cannot make a statis reference to the non-static method getResources() from type Context – borislemke Jan 07 '12 at 14:14
  • any ideas solving that problem? – borislemke Jan 07 '12 at 14:35
  • You can pass the context to that method! readTitlesRaw(Context con) and when you call it give your current context. Use con.getResources(), since it's not stactic you need an object of Context to call it. – caiocpricci2 Jan 07 '12 at 20:50
0

You can create an Array file, under res/values, add your array there and associate it with an ArrayAdapter using ArrayAdapter.createFromResource. You should provide the context, the array you want to use and the layout of the items.

Example of Arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="EventArray">
        <item >Data</item>
        <item >Address</item>
        <item >Your items...</item>
    </string-array>    
</resources>

Then populate your listview with that ArrayAdapter! This code puts the content of the EventArray in your list!

 list = (ListView) findViewById(R.id.list);  //get your listview
 arrayadapter = ArrayAdapter.createFromResource(this, R.array.EventArray, R.layout.YourListviewLayout); // create the arrayadapter from EventArray using YourListviewLayout.
 list.setAdapter(arrayadapter);

Reference to ArrayAdapter.createFromResource: http://developer.android.com/reference/android/widget/ArrayAdapter.html

caiocpricci2
  • 7,714
  • 10
  • 56
  • 88
  • Cant do that, the items in the listview is inflating a view which contains multiple textviews and imageviews. I have to populate the listview with multiple text and image resources. But thanks anyway! ;) – borislemke Jan 07 '12 at 15:54