3

I have a String Array Resource that has about 1000 drug names

<string-array name="index">
<item>Aspirin</item>
  <item>Levitra</item>
  ....
</string-array>

I tried to load this string array into a String[] inside an activity. Using the same syntax provided by Google's java documentation found here. http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

Resources res = getResources();
String[] drugs = res.getStringArray(R.array.index);

It appears my string-array has too many records and its causing my App to crash (stack-overflow?) When I reduce the string-array resource to about 506 records, the app works again, but adding even 1 more element in the string-array would crash the app.

What should I do? I want to code logic to filter and do other things to the list. I do not have a SQL db in the app so I can't do the processing in the DB. Is there a way to iterate through the string-array resource without loading it into a String []? I notice the resource is a pointer. Or is there an alternative data structure or approach that would work?

Thanks,

Mashsensor
  • 31
  • 1
  • 4
  • "I have a String Array Resource that has about 1000 drug names" -- why? What value are you getting out of a string array that could not be met by an ordinary XML resource? – CommonsWare Nov 25 '10 at 01:51
  • @CommonsWare Thanks, cheers! I am using a string-array resource because its the preferred way that Google's own tutorials use to load a list of records into a ListView. Everything works awesome when I have all the records broken down by their starting Alphabet. Now I am trying to implement a search function, so I ideally would like to iterate through all the records at once instead of doing a loop for each starting-Alphabet. I am not familiar with memory allocation architecture in Android, but I am a bit surprised that loading 506 records into a String[] would crash the app :p – Mashsensor Nov 25 '10 at 03:29
  • It would be awesome if there is a way to iterate through a string-array resource in R.arrays without loading it by value into a data structure first... :D, Or maybe a vector or a linked list would be able to hold more data? Maybe I just need to create a SQLite db and handle it there, or just end up iterating through all 26 Alphabets in my search method. Happy Thanksgiving btw – Mashsensor Nov 25 '10 at 03:40

2 Answers2

1

Is there a reason why you don't want to use an SQL database? I would strongly recommend setting up an SQLite database. String arrays were not designed for data of this size. Your app will be slow, and it will consume a lot of memory.

The only alternative I could think of would be to use individual strings and have an array of string ID values - that should work.

EboMike
  • 72,990
  • 14
  • 152
  • 157
  • Thanks for the response! I would certainly go with the option of storing the record in a SQLite db if there is no other alternatives. I was just wondering if there is a way of iterating through a string-array resource via pointer or something like that and not having to load the entire string-array into a String []. – Mashsensor Nov 25 '10 at 00:53
  • You can use `Resource.openRawResource`, but FWIK the format is not documented, and as such, you should NOT try to parse it. Really, having individual strings may be the better bet. That, or just create your own (binary?) resource format and store it as a raw resource. – EboMike Nov 25 '10 at 01:23
  • I would definitely give SQLite a try.. It'll give you a lot of useful functionality if you want to sort drugs, search drugs, etc. – Snailer Nov 25 '10 at 02:57
0

You can use a SAX parser to prevent your app from crashing as descripted here Should I be using something other than getResource().getStringArray() to populate a large array? However if you have such a large set of data you should consider implementing a database

Hansfritzi
  • 82
  • 6