TextWatcher not filtering ListView?
Here is the relevant code:
private TasksAdapter adapter;
private final TextWatcher filterer = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
if (adapter.getFilterQueryProvider() != null)
return adapter.getFilterQueryProvider().runQuery(constraint);
StringBuffer buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuffer();
buffer.append("UPPER (");
buffer.append(Tasks.TITLE);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
Cursor c = getContentResolver().query(Tasks.CONTENT_URI,
null, (buffer == null ? null : buffer.toString()), args,
Tasks.DEFAULT_SORT_ORDER);
Log.v(TAG, "Constraint is " + constraint);
adapter.changeCursor(c);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
c.moveToFirst();
return c;
}
});
adapter.getFilter().filter(newTaskField.getText().toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCr开发者_运维技巧eate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "TasksList activity started.");
Cursor tasks = managedQuery(Tasks.CONTENT_URI, projection, null, null, Tasks.DEFAULT_SORT_ORDER);
adapter = new TasksAdapter(this, tasks);
setListView(adapter)
newTaskField.addTextChangedListener(filterer)
...
}
My application then just crashes without an exception, I get this though:
12-30 19:25:31.718: INFO/ActivityManager(66): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mohit.geotasks/.activities.TasksList bnds=[3,136][77,215] } from pid 128
12-30 19:25:32.219: INFO/ActivityManager(66): Start proc com.mohit.geotasks for activity com.mohit.geotasks/.activities.TasksList: pid=888 uid=10030 gids={1015}
12-30 19:25:34.239: INFO/ActivityThread(888): Pub com.mohit.geotasks: com.mohit.geotasks.provider.TasksProvider
12-30 19:25:34.428: DEBUG/dalvikvm(66): GC_CONCURRENT freed 839K, 48% free 4142K/7815K, external 2036K/2548K, paused 13ms+26ms
12-30 19:25:34.818: VERBOSE/TasksList(888): TasksList activity started.
12-30 19:25:35.918: INFO/ActivityManager(66): Displayed com.mohit.geotasks/.activities.TasksList: +3s737ms
12-30 19:25:37.958: INFO/ActivityManager(66): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher } from pid 66
12-30 19:25:39.048: WARN/InputManagerService(66): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4071a098 (uid=10030 pid=888)
12-30 19:25:40.458: INFO/ActivityManager(66): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mohit.geotasks/.activities.TasksList bnds=[3,136][77,215] } from pid 128
12-30 19:25:44.728: WARN/KeyCharacterMap(888): No keyboard for id 0
12-30 19:25:44.738: WARN/KeyCharacterMap(888): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 19:25:44.858: VERBOSE/TasksList(888): Created Options menu.
12-30 19:25:46.958: WARN/InputManagerService(66): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4060c1a0
12-30 19:25:49.358: INFO/dalvikvm(888): threadid=9: stack overflow on call to Lcom/mohit/geotasks/activities/TasksList$1;.access$0:LL
12-30 19:25:49.358: INFO/dalvikvm(888): method requires 8+20+0=28 bytes, fp is 0x42dd9318 (24 left)
12-30 19:25:49.358: INFO/dalvikvm(888): expanding stack end (0x42dd9300 to 0x42dd9000)
12-30 19:25:49.358: INFO/dalvikvm(888): Shrank stack (to 0x42dd9300, curFrame is 0x42ddbf54)
12-30 19:25:49.358: WARN/dalvikvm(888): threadid=9: thread exiting with uncaught exception (group=0x40015560)
12-30 19:25:49.458: DEBUG/AndroidRuntime(888): Shutting down VM
12-30 19:25:49.488: WARN/dalvikvm(888): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-30 19:25:49.488: INFO/Process(888): Sending signal. PID: 888 SIG: 9
12-30 19:25:49.558: INFO/ActivityManager(66): Process com.mohit.geotasks (pid 888) has died.
12-30 19:25:49.558: INFO/WindowManager(66): WIN DEATH: Window{4060a168 com.mohit.geotasks/com.mohit.geotasks.activities.TasksList paused=false}
What is the problem? I also get a StackOverFlowError at this line:
return adapter.getFilterQueryProvider().runQuery(constraint);
This looks strange:
if (adapter.getFilterQueryProvider() != null)
return adapter.getFilterQueryProvider().runQuery(constraint);
I would remove this completely. Because looks like this results in calling the public Cursor runQuery(CharSequence constraint)
again and again.
精彩评论