开发者

How do I store a List<String> as a resource in Android?

I have a database with 10,000 entries. The database is not updated regularly, but I need to fetch data from it frequently.

One of the options I give to choose a row is a ListView with text filtering.

But the ListView takes a few seconds to load. This time is taken to query the database and put the results from the Cursor into a List. So I decided to create the List whenever the database is updated and use it everytime I want to make the ListView.

I need pointers on how I can store the List so that I can retrieve it immediately and show the ListView.

Any alternatives you can provide would also be appreciated.

UPDATE:

My initial code:

public List<String> fetchNames() {
    String[] columns={NAME_KEY};
    List<String> names = new ArrayList<String>();
    Cursor namesCursor=mydb.query(TABLE_NAME, columns, null, null, null, n开发者_运维百科ull, NAME_KEY, null);
    Log.w("1", "Got query result");
    namesCursor.moveToFirst();
    while(!namesCursor.isAfterLast()){
        names.add(namesCursor.getString(0));
        namesCursor.moveToNext();
    }
    Log.w("1", "List created");
    return names;
}

It takes about 5 seconds between the time "Got query result" and "List created" is logged.

I modified the code to use CursorAdapter

public SimpleCursorAdapter fetchNames() {
    String[] columns={"_id", NAME_KEY};
    String[] from={NAME_KEY};
    int[] to={R.id.list_item_1};
    Cursor namesCursor=mydb.query(TABLE_NAME, columns, null, null, null, null, NAME_KEY, null);
    Log.w("1", "Got query result");
    SimpleCursorAdapter names = new SimpleCursorAdapter(dbContext, R.layout.list_item, namesCursor, from, to);
    Log.w("1", "Returning CursorAdapter");
    return names;
}

The activity still takes 5 sec to load, but only after "Returning CursorAdapter" is logged.

I feel that the delay is in fetching from the cursor, which is why I wanted to store it as a quickly fetchable resource. Do correct me if I am wrong in thinking that as a string array in resources, it would load faster.


Why not use a CursorAdapter that you attach to your ListView rather than extracting your data into a List? It will automatically update when the database changes, and will be much more efficient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜