3

My App has a List-view to display values from an SQLite DB. There is an edit-text on the of the listview for searching for values in the List-view. Here is some of my code:

public void onTextChanged(CharSequence s, int start, int before,int count) {
            String sCondition = "C_Data like '%" + ed.getText() + "%'";
            Cursor data = database.query("tbl_Contents", fields, sCondition, null, null, null, "C_Data");
            dataSource = new SimpleCursorAdapter(items_list.this, R.layout.row, data, fields, new int[] { R.id.first});
            lv1.setAdapter(dataSource); 
        }

I have tested it out and it seems to work fine but I'm worried about performance because that code need to access on the database every time the user presses a key.

Without using an explicit search button how can I improve the performance of the search?

1
  • 1
    Is there a peformance problem? If not, leave it alone and do no try to overoptimize Nov 25, 2011 at 15:34

2 Answers 2

0

One thing I can think of is, while populating your listview populate a list (with just the field you require). So you can search the list than hitting the DB.

0

Another option is to start a timer on each key press coupled with a key press counter. After every N keystrokes or whenever the timer goes off the query DB query function would get called. Also if the timer is already running when another key is pressed you would simply reset the delay. If you were to use a timer delay of 100 ms it would probably feel fast enough for the user while allowing them enough time to keep typing. The search would be executed when they stop typing.

Something like this would reduce the number of DB queries and while still issuing queries while the user is typing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.