PDA

View Full Version : Question about using hash table with ArrayList and ArrayAdapter



ppmoore
03-31-2009, 05:34 AM
Hello,

I have a simple application to read a dictionary text file


File filePath = new File( "/data/app-private", "dictionary.txt");
ArrayList<String> dictionary = new ArrayList<String>();

BufferedReader input = new BufferedReader(new FileReader(fileName));
while( ( fileLine = input.readLine() ) != null )
{
dictionary.add(fileLine);
}

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this,
android.R.layout.simple_dropdown_item_1line,
dictionary);
textView.setAdapter(adapter);This works. However it becomes very slow if there are many entries in the dictionary string list.

Can I speed it up?
How does the ArrayList<String> internally organise its entries?
Is there a way to combine it with one of the Hash classes?

Many thanks,
Paul

Scythe
03-31-2009, 08:59 AM
The ArrayList is literally named as it is -- it's an ever expanding array-based list of items. You would have to use a different adapter (not an ArrayAdapter, you might have to make your own or see if there's another option in the SDK), but you could use a HashMap to generate the list. You'd take the performance hit on load, but the reading of the values would be far faster.

The problem though, is the UI thread might struggle with creating a bunch of elements to meet your needs depending on how many items you have.

You might also want to use a SQL Lite DB for storage, since IO calls are far more expensive than any other task.