Android列表——如何找到想要的物品?

Android列表——如何找到想要的物品?,android,android-widget,Android,Android Widget,我确实有一个可能有几千个项目的列表,只是用大约200个项目的较短列表进行测试。信息存储在SQLite中,使用ContentProvider、loader和SimpleCursorAdapter。列表按字典顺序排序,并使用android:fastScrollEnabled。列表滚动顺畅-如果知道项目的确切名称,则没有问题 有时候,我想找到包含在它们名字中间的某个子字符串的项目。这个`。。。比如说%wanted%对我来说是个解决方案。但是,我想给用户一个增量过滤,即在键入子字符串期间更新列表内容。理

我确实有一个可能有几千个项目的列表,只是用大约200个项目的较短列表进行测试。信息存储在SQLite中,使用ContentProvider、loader和SimpleCursorAdapter。列表按字典顺序排序,并使用android:fastScrollEnabled。列表滚动顺畅-如果知道项目的确切名称,则没有问题

有时候,我想找到包含在它们名字中间的某个子字符串的项目。这个`。。。比如说%wanted%对我来说是个解决方案。但是,我想给用户一个增量过滤,即在键入子字符串期间更新列表内容。理由是可能不需要键入许多字符,应该尽快找到该项目。目标不是找到一个或没有项目。我们的目标是过滤列表,以便可以通过手动滚动浏览候选项并通过触摸选择其中一项

我遇到了SearchView小部件,它在操作栏上看起来很漂亮。不管怎样,在文档中读到更多关于它的内容,我不确定它是否适合我。或者如果推荐的实现是适合我的。我是一个android初学者,我甚至不确定自己是否理解得很好


在操作栏中具有SearchView的同一活动中,是否可以使用小部件对列表进行增量筛选?您能给我指出一些可能显示如何实现所需行为的代码吗?

要尝试的示例代码:

public class AndroidListViewFilterActivity extends Activity {

    ArrayAdapter<String> dataAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Generate list View from ArrayList
        displayListView();

    } 

    private void displayListView() {

       //Array list of countries
       List<String> countryList = new ArrayList<String>();
       countryList.add("Aruba");
       countryList.add("Anguilla");
       countryList.add("Netherlands Antilles");
       countryList.add("Antigua and Barbuda");
       countryList.add("Bahamas");
       countryList.add("Belize");
       countryList.add("Bermuda");
       countryList.add("Barbados");
       countryList.add("Canada");
       countryList.add("Costa Rica");
       countryList.add("Cuba");
       countryList.add("Cayman Islands");
       countryList.add("Dominica");
       countryList.add("Dominican Republic");
       countryList.add("Guadeloupe");
       countryList.add("Grenada");

      //create an ArrayAdaptar from the String Array
      dataAdapter = new ArrayAdapter<String>(this,R.layout.country_list, countryList);
      ListView listView = (ListView) findViewById(R.id.listView1);
      // Assign adapter to ListView
      listView.setAdapter(dataAdapter);

      //enables filtering for the contents of the given ListView
      listView.setTextFilterEnabled(true);

      listView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
         // When clicked, show a toast with the TextView text
             Toast.makeText(getApplicationContext(),((TextView) view).getText(), Toast.LENGTH_SHORT).show();
          }
      });

      EditText myFilter = (EditText) findViewById(R.id.myFilter);
      myFilter.addTextChangedListener(new TextWatcher() {

      public void afterTextChanged(Editable s) {
      }

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }

      public void onTextChanged(CharSequence s, int start, int before, int count) {
          dataAdapter.getFilter().filter(s.toString());
      }
     });
   }   
}

请尝试此链接@阿披实沙巴:谢谢。请在OnQueryTextListener上给出简短的答案。。。我会接受的。谢谢。我已经了解到适配器必须是可过滤的,这也适用于我使用的SimpleCursorAdapter。简单地使用选择条件重新启动.restartLoader而不是使用.filter不是更容易吗?