试图筛选一个ListView与runQueryOnBackgroundThread但没有任何反应 - 我缺少什么?但没、有任何、反应、ListView

2023-09-06 02:50:45 作者:肆意。

予有在数据库中的国家的列表。我创建了一个选择国家的活动,包括一个编辑框过滤和它显示标志和国家名称的列表。

I have a list of countries in a database. I have created a select country activity that consists of a edit box for filtering and a list which displays the flag and country name.

在活动开始的列表显示了国家按字母顺序排列的完整列表 - 工作正常。当用户开始输入到搜索框中我希望用他们的打字要过滤的列表。我的数据库查询是在AutoCompleteView previously工作(我只是想切换到一个单独的文本框和列表),所以我知道我的完整的查询和我约束的查询工作。我所做的就是添加一个TextWatcher到的EditText视图和每一个文本改变时我调用列表中的SimpleCursorAdapter runQueryOnBackgroundThread与编辑框中的文字作为约束条件。麻烦的是列表永远不会更新。我已经在调试器设置断点和TextWatcher确实让呼叫runQueryOnBackgroundThread和我FilterQueryProvider被称为与预期的约束。该数据库查询会很好,光标返回。

When the activity starts the list shows the entire list of countries sorted alphabetically - works fine. When the customer starts typing into the search box I want the list to be filtered based on their typing. My database query was previously working in an AutoCompleteView (I just want to switch to a separate text box and list) so I know my full query and my constraint query are working. What I did was add a TextWatcher to the EditText view and every time the text is changed I invoke the list's SimpleCursorAdapter runQueryOnBackgroundThread with the edit boxes text as the constraint. The trouble is the list is never updated. I have set breakpoints in the debugger and the TextWatcher does make the call to runQueryOnBackgroundThread and my FilterQueryProvider is called with the expected constraint. The database query goes fine and the cursor is returned.

光标适配器有一个过滤器查询提供一套(及视图粘合剂,以显示标志):

The cursor adapter has a filter query provider set (and a view binder to display the flag):

    SimpleCursorAdapter adapter = new SimpleCursorAdapter (this,
            R.layout.country_list_row, countryCursor, from, to);
    adapter.setFilterQueryProvider (new CountryFilterProvider ());
    adapter.setViewBinder (new FlagViewBinder ());

在FitlerQueryProvider:

The FitlerQueryProvider:

private final class CountryFilterProvider implements FilterQueryProvider {

    @Override
    public Cursor runQuery (CharSequence constraint) {
        Cursor countryCursor = myDbHelper.getCountryList (constraint);
        startManagingCursor (countryCursor);
        return countryCursor;
    }
}

而EditText上有一个TextWatcher:

And the EditText has a TextWatcher:

    myCountrySearchText = (EditText)findViewById (R.id.entry);
    myCountrySearchText.setHint (R.string.country_hint);
    myCountrySearchText.addTextChangedListener (new TextWatcher() {
        @Override
        public void afterTextChanged (Editable s) {
            SimpleCursorAdapter filterAdapter = (SimpleCursorAdapter)myCountryList.getAdapter ();
            filterAdapter.runQueryOnBackgroundThread (s.toString ());
        }


        @Override
        public void onTextChanged (CharSequence s, int start, int before, int count) {
            // no work to do
        }


        @Override
        public void beforeTextChanged (CharSequence s, int start, int count, int after) {
            // no work to do
        }
    });

数据库查询看起来是这样的:

The query for the database looks like this:

public Cursor getCountryList (CharSequence constraint)  {
    if (constraint == null  ||  constraint.length () == 0)  {
        //  Return the full list of countries
        return myDataBase.query (DATABASE_COUNTRY_TABLE, 
                new String[] { KEY_ROWID, KEY_COUNTRYNAME, KEY_COUNTRYCODE }, null, null, null, 
                null, KEY_COUNTRYNAME);
    }  else  {
        //  Return a list of countries who's name contains the passed in constraint
        return myDataBase.query (DATABASE_COUNTRY_TABLE, 
                new String[] { KEY_ROWID, KEY_COUNTRYNAME, KEY_COUNTRYCODE }, 
                "Country like '%" + constraint.toString () + "%'", null, null, null, 
                "CASE WHEN Country like '" + constraint.toString () + 
                "%' THEN 0 ELSE 1 END, Country");
    }
}

这似乎只是有一个缺失的环节某处。任何帮助将是AP preciated。

It just seems like there is a missing link somewhere. Any help would be appreciated.

谢谢

伊恩

推荐答案

好像你做了很多工作的东西,已经内置到Android系统。看看Android的搜索对话框,在 http://developer.android。 COM /引导/主题/搜索/搜索dialog.html 的,它应该是很容易为你。

It seems like you're doing a lot of work for something that is already built into Android. Take a look at the Android Search Dialog at http://developer.android.com/guide/topics/search/search-dialog.html, and it should be very easy for you.