我有数据库,但我不知道如何在listview中使用搜索。我搜索互联网,但我使用“字符串”,但我有一个数据库所以我不能使用字符串请帮助使用listview中的搜索从数据库加载数据而不是设置字符串产品[] = {}一个固定的字符串
lv = (ListView) findViewById(R.id.listView);
wordAdapter = new ArrayAdapter<Word>(this, R.layout.list_item,
R.id.product_name);
lv.setAdapter(adapter);
dbHandler=new DatabaseHandler(getApplicationContext());
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
MainActivity.this.wordAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
and this is database
private static final String DATABASE_NAME = "MYDB1",
TABLE_TUVUNG="tuvung",
KEY_ID = "id",
KEY_THEM = "them",
KEY_NGHIA = "nghia",
KEY_LOAI = "loai",
KEY_NGAY = "ngay";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_TUVUNG + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_THEM + " TEXT," + KEY_NGHIA + " TEXT," + KEY_LOAI + " TEXT," + KEY_NGAY + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TUVUNG);
onCreate(db);
}
public void createContact(Word contact) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_THEM, contact.getthem());
values.put(KEY_NGHIA, contact.getnghia());
values.put(KEY_LOAI, contact.getloai());
values.put(KEY_NGAY, contact.getngay());
db.insert(TABLE_TUVUNG, null, values);
db.close();
}
public Word getContact(int id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_TUVUNG, new String[] { KEY_ID, KEY_THEM, KEY_NGHIA, KEY_LOAI,KEY_NGAY }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );
if (cursor != null)
cursor.moveToFirst();
Word contact = new Word(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
db.close();
cursor.close();
return contact;
}
public void deleteContact(Word contact) {
SQLiteDatabase db =this.getWritableDatabase();
db.delete(TABLE_TUVUNG, KEY_ID + "=?", new String[] { String.valueOf(contact.getid()) });
db.close();
}
public void valuestact(Word contact){
ContentValues values = new ContentValues();
values.put(KEY_THEM, contact.getthem());
values.put(KEY_NGHIA, contact.getnghia());
values.put(KEY_LOAI, contact.getloai());
values.put(KEY_NGAY, contact.getngay());
return;
}
public int getwordsCount() {
SQLiteDatabase db =this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_TUVUNG, null);
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
public int updateContact(Word contact) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_THEM, contact.getthem());
values.put(KEY_NGHIA, contact.getnghia());
values.put(KEY_LOAI, contact.getloai());
values.put(KEY_NGAY, contact.getngay());
int rowsAffected = db.update(TABLE_TUVUNG, values, KEY_ID + "=?", new String[] { String.valueOf(contact.getid()) });
db.close();
return rowsAffected;
}
public List<Word> getAllwords() {
List<Word> words = new ArrayList<Word>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_TUVUNG, null);
if (cursor.moveToFirst()) {
do {
words.add(new Word(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4)));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return words;
}
答案 0 :(得分:0)
尝试此链接可能会解决您的问题 search a value from sqlite database and retrieve in listview
我希望它会帮助你......