0

おそらく数千のアイテムのリストがあります(約200アイテムの短いリストでテストしています)。情報は、SQLite、ContentProviderローダに格納されSimpleCursorAdapterて使用されます。リストは辞書順にソートされ、android:fastScrollEnabledが使用されます。リストはスムーズにスクロールします。項目の正確な名前がわかっている場合は問題ありません。

時折、名前の途中に部分文字列を含むアイテムを見つけたいと思うことがあります。`... LIKE "%wanted%" は私にとっての解決策です。ただし、ユーザーに増分フィルタリングを提供したいと思います。つまり、部分文字列の入力中にリストの内容が更新されます。その理由は、多くの文字を入力する必要がない可能性があり、できるだけ早く項目を見つける必要があるからです。目標は、1 つまたは 1 つのアイテムを見つけることではありません。目標は、候補アイテムを概観し、それらの 1 つをタッチで選択するために手動スクロールが許容されるように、リストをフィルタリングすることです。

SearchViewアクションバーによく似合うウィジェットに出会いました。とにかく、ドキュメントでそれについてもっと読んで、それが私にとって正しいツールであるかどうかはわかりません. または、推奨される実装が私に適している場合。(私はAndroidの初心者で、よく理解しているかどうかさえわかりません。)

アクション バーに SearchView があるのと同じアクティビティで、リストのインクリメンタル フィルタリングにウィジェットを使用することは可能ですか? 必要な動作を実装する方法を示す可能性のあるコードを教えていただけますか?

4

1 に答える 1

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());
      }
     });
   }   
}
于 2013-04-30T07:16:35.287 に答える