我的应用程序中有数据库,它完全加载到 listview 中,但是在 listview 中进行搜索时,它不起作用并且应用程序没有崩溃,下面是我的代码,我从数据库中检索数据并在 listview 中加载。
-------------DatabaseClass----
public Cursor getCursor() {
// TODO Auto-generated method stub
// SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
// queryBuilder.setTables(TABLE_NAME);
// openDataBase();
Cursor c = null;
try {
String[] asColumnsToReturn = new String[] { "lang_from","_id" };
c = myDataBase.query("words2", asColumnsToReturn, null, null, null,
null, null);
} catch (Exception e) {
System.out.print("" + e);
}
return c;
}
-----------MainActivity----------------
public class MainActivity extends Activity {
DatabaseClass myDbHelper;
private Cursor ourCursor = null;
private IngredientAdapter adapter = null;
ListView myListView;
EditText etSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSearch = (EditText) findViewById(R.id.etsearch);
myListView = (ListView) findViewById(R.id.listView1);
myListView.setTextFilterEnabled(true);
// = new DatabaseClass();
myDbHelper = new DatabaseClass(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
ourCursor = myDbHelper.getCursor();
startManagingCursor(ourCursor);
try {
adapter = new IngredientAdapter(this, ourCursor);
} catch (Exception e) {
System.out.print("" + e);
}
myListView.setAdapter(adapter);
} catch (SQLException sqle) {
throw sqle;
}
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
// MainActivity.this.adapter.getFilter().filter(cs);
Log.d("TAGGGGGGGG", cs.toString());
adapter.getFilter().filter(cs);
// adapter.notifyDataSetChanged();
//MainActivity.this.adapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// adapter.getFilter().filter(s.toString());
}
});
}
class IngredientAdapter extends CursorAdapter {
@SuppressWarnings("deprecation")
public IngredientAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View row, Context ctxt, Cursor c) {
// TODO Auto-generated method stub
IngredientHolder holder = (IngredientHolder) row.getTag();
holder.populateFrom(c, myDbHelper);
Log.d("TAAAAAAAAAAAAAAAGGGGG", "GOOD");
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
IngredientHolder holder = new IngredientHolder(row);
row.setTag(holder);
Log.d("TAAAAAAAAAAAAAAAGGGGG", "GOOD");
return (row);
}
}
class IngredientHolder {
private TextView name = null;
IngredientHolder(View row) {
name = (TextView) row.findViewById(R.id.ingredientText);
}
void populateFrom(Cursor c, DatabaseClass r) {
name.setText(r.getName(c));
}
}
}
请帮助和抱歉英语不好