TextWatcher不过滤ListView?

发布于 2024-10-09 11:23:43 字数 5227 浏览 1 评论 0原文

这是相关的代码:

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.onCreate(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)
    ...
}

然后我的应用程序就毫无例外地崩溃了,但我得到了这个:

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}

问题是什么?我还在这一行收到 StackOverFlowError:

return adapter.getFilterQueryProvider().runQuery(constraint);

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.onCreate(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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-10-16 11:23:43

这看起来很奇怪:

if (adapter.getFilterQueryProvider() != null)
    return adapter.getFilterQueryProvider().runQuery(constraint);

我会完全删除它。因为看起来这会导致一次又一次调用 public Cursor runQuery(CharSequenceconstraint)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文