通过显示数据库中的所有数据,此代码可以正常工作,但是我在过滤时遇到问题。我尝试了很多代码,但没有任何效果,有人可以帮我吗?谢谢
package com.example.dictionary;
import java.util.List;
import com.example.dictionary.R;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity{
Cursor cursor;
ListView listView;
SimpleCursorAdapter adapter;
Button back, clear;
List<String> items;
String get;
EditText et;
int textlength = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
打开我的数据库并获取所有数据
Historydb db = new Historydb(this);
db.open();
cursor = db.getword();
startManagingCursor(cursor);
cursor.moveToFirst();
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor,
new String[] { "word" }, new int[] { android.R.id.text1 });
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
我在edittext中设置了addtextchangelistener。
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
adapter.getFilter().filter(s);
}});
}
}
最佳答案
它不起作用,因为adapter.getfilter().filter(cs);
不适用于SimpleCursorAdapter。
您必须先将adapter.setFilterQueryProvider
用于SimpleCursorAdapter
。
这是完整的描述:ListView, SimpleCursorAdapter, an an EditText filter -- why won't it do anything?
这是:Using an EditText to filter a SimpleCursorAdapter-backed ListView
关于android - 过滤SimpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21780499/