使用Simplecursoradapter的Listfilter

时间:2013-02-16 02:29:33

标签: android

我想在用户在文本框中输入内容时过滤我的列表。

但尝试输入文本框时没有任何反应。

我看到很多话题,但可以意识到出了什么问题。

我知道很多次都会问这个问题,但我可以解决它。

这是我的代码:

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

我的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) {

        adapter.getFilter().filter(s);

    }

};

3 个答案:

答案 0 :(得分:1)

您还需要一些额外的东西才能使用SimpleCursorAdapter进行过滤。首先,您需要为适配器标识string conversion column,然后需要实现FilterQueryProvider并通过setFilterQueryProvider方法调用将其添加到适配器。

以下是FilterQueryProvider实现的一个简单示例:

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

答案 1 :(得分:0)

你看过这些吗?

Filtering ListView with custom (object) adapter < - 在textview上..用户开始输入文字..

http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ < - 显示顶部的文本字段..然后在其中键入...

答案 2 :(得分:0)

我有自定义方法来使用两组ArrayList来实现过滤。 (您可以根据需要对其进行修改。)

1)搜索ArrayList
2)Main ArrayList

第一种方法是将搜索数组设置为与主数组相同的值。

public void setarray() {


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

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

第二种方法是用你的数组显示你的ListView。

public void displaylist() {

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

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

        mylistView.setAdapter(adapter);


    }

过滤ListView的第三种方法

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

希望这会有所帮助。