2011-12-03 28 views
0

我想能夠使用列表視圖來過濾數據庫。我希望能夠使用editText並使用SQL Like運算符來過濾列表。我已創建一個數據庫輔助類...使用ListView和使用EditText過濾SQLite數據庫

package pkg.CookBook; 

    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 
    import android.content.Context; 


    public class DatabaseOpenHelper { 


     private static final String KEY_ROWID = "_id"; 
     private static final String KEY_NAME = "Recipe_Name"; 
     private static final String KEY_CATEGORY = "Recipe_Category"; 
     private static final String KEY_DESCRIPTION = "Recipe_Description"; 
     private static final String FOREIGN_ROWID2 = "Ing_Recipe_ID"; 
     private static final String KEY_ROWID2 = "Ing_id"; 
     private static final String KEY_NAME2 = "Ingredient_Name"; 
     private static final String KEY_CATEGORY2 = "Ingredient_Category"; 
     private static final String DATABASE_NAME = "database"; 
     private static final String DATABASE_TABLE = "CBRecipeData"; 

     private static final String DATABASE_TABLE2 = "CBIngredientData"; 

     private final Context myContext; 
     private databaseHelper myHelper; 
     private SQLiteDatabase myDatabase; 

     public static class databaseHelper extends SQLiteOpenHelper{ 


      public databaseHelper(Context context){ 
       super(context, DATABASE_NAME, null,1); 
      } 

     // Database creation sql statement 
     /*private static final String DATABASE_CREATE = "create table CBRecipeData  (recipe_id integer primary key autoincrement, " 
       + "recipe category text not null, recipe name text not null, recipe description text not null);"; 
     private static final String DATABASE_CREATE2 = "create table CBIngredientData (ing_recipe_id foreign key reference CBRecipeData recipe_id " + "integer ingredient_id integer primary key autoincrement, " 
       + "recipe category text not null, recipe name text not null, recipe description text not null);"; 

     */ 

     @Override 
     public void onCreate(SQLiteDatabase database) { 
      database.execSQL("Create Table"+ DATABASE_TABLE + " (" + 
     KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     KEY_NAME + " TEXT NOT NULL, " + 
     KEY_CATEGORY + " TEXT NOT NULL, " + 
     KEY_DESCRIPTION + "TEXT NOT NULL"); 

     database.execSQL("Create Table"+ DATABASE_TABLE2 + " (" + 
        FOREIGN_ROWID2 + "foreign key reference CBRecipeData recipe_id, " + 
        KEY_ROWID2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        KEY_NAME2 + " TEXT NOT NULL, " + 
        KEY_CATEGORY2 + " TEXT NOT NULL"); 

     } 

@Override 
public void onUpgrade(SQLiteDatabase database, int oldversion, int newVersion) { 

    database.execSQL("Drop Table If Exists " + DATABASE_TABLE); 
    onCreate(database); 

} 
     } 

public DatabaseOpenHelper(Context c){ 

    myContext = c; 
} 


public DatabaseOpenHelper open(){ 

    myHelper = new databaseHelper(myContext); 
    myDatabase = myHelper.getWritableDatabase(); 
    return this; 
} 

public void close(){ 
    myHelper.close(); 
} 

public void createEntry(String Name, String Description, String Category){ 

} 
    } 


.... 

我很困惑不過,我怎麼可以過濾數據庫以及如何實際在列表視圖中顯示這個..

String query = "SELECT _id, Recipe_Name "+ 
      "FROM CBRecipeData "+ 
      "WHERE Recipe_name Like %" + filterText.getText().toString() + "%" + 
      "ORDER BY Recipe_Name"; 


      final Cursor c = dbHelper.getReadableDatabase().rawQuery(query,  null); 

我知道這就像我想實現的過濾器,但如何與列表視圖使用這個問題是問題..

任何指向正確的方向將是偉大的! :) 謝謝stef

回答