0

ユーザーがテキスト ボックスに何かを入力したときに、リストをフィルター処理したいと考えています。

テキストボックスに入力しようとしても何も起こりません。

多くのトピックを見ましたが、何が悪いのかを理解することができました。

この質問は何度も聞かれることを知っていますが、私はそれを解決することができました.

これが私のコードです:

public class Find extends ListActivity {

private EditText filterText = null;
DBHandler db=new DBHandler(this);
ListView myList;
String[] items = new String[10];
SimpleCursorAdapter adapter=null;
int test = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_search);
    filterText = (EditText) findViewById(R.id.searchbox);
    filterText.addTextChangedListener(filterTextWatcher);


Cursor cursor=db.getEvents();
    startManagingCursor(cursor);
    String[] from = {"_id","full_name"};
    int[] to = { R.id.name_entry, R.id.number_entry};
    adapter = new SimpleCursorAdapter(this, R.layout.activity_find, cursor, from, to);
setListAdapter(adapter);

私のテキストウォッチャーメソッド:

    private TextWatcher filterTextWatcher = 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) {

        adapter.getFilter().filter(s);

    }

};
4

3 に答える 3

1

You also need a few extra things to have filtering working with a SimpleCursorAdapter. First you need to identify the string conversion column for your adapter and second you need to implement a FilterQueryProvider and add it to your adapter through the setFilterQueryProvider method call.

Here's a simple example of a FilterQueryProvider implementation:

mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
   @Override
   public Cursor runQuery(CharSequence constraint) { 
      if (constraint == null) {
         // contract: if constraint is null, return the same as before
     return mAdapter.getCursor();
      } else {
         //TODO: run your query here... 
      }
   }
});
于 2013-02-16T04:39:34.327 に答える
0

これらを見たことがありますか?

textviewでカスタム(オブジェクト)アダプタ <-を使用してListViewをフィルタリングします。ユーザーがテキストの入力を開始します。

http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/<- 上部にテキストフィールドが表示されます。その中に入力します...

于 2013-02-16T03:55:12.447 に答える
0

の2つのセットでフィルタリングを実現するカスタムメソッドがありArrayListます。(必要に応じて変更できます。)

1) ArrayList の検索
2) メイン ArrayList

最初の方法は、メイン配列と同じ値で検索配列を設定することです。

public void setarray() {


        for (int j = 0; j < MainArray.size(); j++) {

            SearchArray.add(MainArray.get(j));
        }
    }

2 番目の方法は、配列を使用して ListView を表示することです。

public void displaylist() {

        myCustomArrayAdaptor adapter = new myCustomArrayAdaptor(this,
                R.layout.myLayOut, SearchArray);

        mylistView = (ListView) findViewById(R.id.mylist);

        mylistView.setAdapter(adapter);


    }

ListView をフィルタリングする 3 番目の方法

public void setsearcharray(CharSequence searchstr) {

//clear your array before adding the new value in it

        SearchArray.clear();

        int len1 = searchstr.length();

// if your search text length is zero then transfer all your main array data to search array.
        if (len1 == 0) {

            for (int j = 0; j < MainArray.size(); j++) {

                SearchArray.add(MainArray.get(j));
            }

// then display your listview with your search array value.
            displaylist();
        } else {
            for (int j = 0; j < MainArray.size(); j++) {

                String strvalue = MainArray.get(j);

                int length = searchstr.length();

//if your search text length is smaller then the value that is in your array

                if (strvalue.length() >= length) {
                    String substr = strvalue.substring(0, length);

                    if (substr.equals(searchstr.toString())) {

                        SearchArray.add(strvalue);

                        displaylist();

                    } else {
                        displaylist();
                    }

                }

            }
        }

    } 

次に、カスタム検索方法をTextWatcher

private TextWatcher filterTextWatcher = 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) {

            setsearcharray(s);
        }

    };

次のようなリストを初めて生成setarray(); displaylist();するメソッドに追加することを忘れないでください。public void onCreate(Bundle savedInstanceState)

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lead_layout);

        setarray();

        displaylist();
}

これが役立つことを願っています。

于 2013-02-16T04:53:15.537 に答える