【问题标题】:Android list -- how to find the wanted item?Android列表——如何找到想要的项目?
【发布时间】:2013-04-27 20:20:16
【问题描述】:

我确实有一个可能包含数千个项目的列表(只是使用包含大约 200 个项目的较短列表进行测试)。信息存储在 SQLite 中,使用了 ContentProvider、loader 和 SimpleCursorAdapter。该列表按字典顺序排序,并使用android:fastScrollEnabled。列表滚动流畅——只要知道项目的确切名称就没有问题。

有时,我想找到在其名称中间某处包含某些子字符串的项目。 `... LIKE "%wanted%" 对我来说是一个解决方案。但是,我想给用户一个增量过滤——即在输入子字符串期间更新列表内容。原因是可能不需要输入很多字符,并且应该尽快找到该项目。目标不是找到一个或一个项目。目标是过滤列表,以便可以接受手动滚动以概览候选项目并通过触摸选择其中一个。

我遇到了SearchView 小部件,它在操作栏上看起来很漂亮。无论如何,在文档中阅读更多关于它的内容,我不确定它是否适合我。或者,如果推荐的实施是适合我的实施。 (我是一个android初学者,我什至不确定我是否理解它。)

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

【问题讨论】:

标签: android android-widget


【解决方案1】:

试用示例代码:

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());
      }
     });
   }   
}

【讨论】:

  • 谢谢。我了解到适配器必须是可过滤的,这也适用于我使用的SimpleCursorAdapter。简单地用选择条件.restartLoader而不是摆弄.filter不是更容易吗?
猜你喜欢
  • 2023-03-04
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2019-06-14
  • 1970-01-01
  • 2022-12-23
相关资源
最近更新 更多