2014-12-31 73 views
-1

我有數據庫,但我不kwow如何使用列表視圖搜索。我搜索互聯網,但我用「串」,但我有一個數據庫,所以我不能使用字符串,請幫助使用搜索列表視圖中,從數據庫中加載,而不是設置字符串產品[] = {}固定字符串搜索時,我不得不數據庫

數據
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

列表視圖中搜索'當我有database' ......在數據庫中搜索,相反,並在每次搜索後重新填充listview! –

回答