我是android新手。我的问题 - 没有从数据库中填充的过滤列表。 我已经阅读了许多与我相同问题的文章和例子,并花了很多时间。 我无法理解我的错误并找到解决方案。我陷入了僵局。 为了得到理想的结果我必须改变什么?
public class MainActivity extends Activity {
public ListView listView;
public EditText editSearch;
public DataBaseHelper dataBase;
public Cursor cursor;
public SimpleCursorAdapter scAdapter;
private boolean langChangeMenu;
public void fillList(final boolean lang) {
String[] from;
int[] to;
listView.setAdapter(null);
if (lang == true) {// true
cursor = dataBase.getAllDataZGERMRUS();
from = new String[] { "ZGERMGR", "ZRUSGR" };
to = new int[] { R.id.word_name, R.id.word_name2 };
}
else {
cursor = dataBase.getAllDataZRUSGR();
from = new String[] { "ZGERMRG", "ZRUSRG" };
to = new int[] { R.id.word_name2, R.id.word_name };
}
scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,from, to);
listView.setAdapter(scAdapter);
listView.setFastScrollEnabled(true);
listView.setTextFilterEnabled(true);
editSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
scAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
scAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
scAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
if (lang){
return dataBase.getForSearchZGERMRUS(constraint.toString());
}
else{
return dataBase.getForSearchZRUSGERM(constraint.toString());
}
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = getPreferences(MODE_PRIVATE);
langChangeMenu = preferences.getBoolean("langBool", true);
listView = (ListView) findViewById(R.id.listView1);
editSearch = (EditText) findViewById(R.id.editText1);
runDataBase();
fillList(langChangeMenu);
}
}
functions from DataBase.class
public Cursor getForSearchZGERMRUS(CharSequence constraint){
return myDataBase.query("ZGERMRUS",fieldsGERMRUS, constraint.toString(), null, null, null,null);
}
public Cursor getForSearchZRUSGERM(CharSequence constraint){
return myDataBase.query("ZRUSGERM",fieldsRUSGERM, constraint.toString(), null, null, null,null);
}