Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Filtering a SimpleCursorAdapter ListView

Right now, I'm running into issues trying to implement a FilterQueryProvider in my custom SimpleCursorAdapter, since I'm unsure of what to do in the FilterQueryProvider's runQuery function.

In other words, since the query that comprises my ListView basically gets the rowID, name, and a third column from my databases's table, I want to be able to filter the cursor based on the partial value of the name column.

However, I am uncertain of whether I can do this directly from runQuery without expanding my DB class since I want to filter the existing cursor, or will I have to create a new query function in my DB class that partially searches my name column, and if so, how would I go about creating the query statement while using the CharSequence constraint argument in runQuery?

I am also concerned about the performance issues associated with trying to run multiple queries based on partial text since the DB table in question has about 1300-1400 rows. In other words, would I run into a bottleneck trying to filter the cursor?

like image 559
Diego Tori Avatar asked Feb 27 '10 19:02

Diego Tori


1 Answers

You need to run a query that will return a new filtered cursor:

public class MyActivity extends ListActivity implements FilterQueryProvider {

    private Cursor cursor;

    @Override
    public Cursor runQuery(CharSequence constraint) {
        if(cursor != null) {
            cursor.close();
        }
        cursor = somehowGetAFilteredCursorFor(constraint);
        startManagingCursor(cursor);
        return cursor;
    }

}

like image 54
yanchenko Avatar answered Oct 05 '22 07:10

yanchenko