如何添加将检索已在SQLite上输入的数据的searchView

时间:2016-08-07 19:59:46

标签: android sqlite

我想知道我需要添加到这个应用程序,以便我可以在searchview中查看从数据库输入的数据。我已经创建了一个示例,其中有一个搜索工具,但它有硬编码的字符串,在这种情况下,我希望用户看到他们在数据库中输入的数据。请告诉我这需要什么工作?谢谢

这是我的主要课程

package ie.example.artur.projectrepeat;

public class DatabaseClass extends SQLiteOpenHelper {
    public static final String DATABASE_Name = "Product.db2";
    public static final String Table_Name = "product_table2";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "Name";
    public static final String COL_3 = "Quantity";
    public static final String COL_4 = "Category";
    public static final String COL_5 = "Importance";
    public DatabaseClass(Context context) {
        super(context, DATABASE_Name, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + Table_Name + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,Quantity TEXT,Category INTEGER,Importance TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("Drop Table If Exists" + Table_Name);
        onCreate(db);
    }

    public boolean insertData(String name, String quantity, String category,String importance) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, quantity);
        contentValues.put(COL_4, category);
        contentValues.put(COL_5, importance);

        long result = db.insert(Table_Name, null, contentValues);

            if (result == -1)
                return false;
            else
                return true;
        }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " + Table_Name, null);
        return res;
    }

    public boolean updateData(String id,String name,String quantity,String category,String importance ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1, id);
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, quantity);
        contentValues.put(COL_4, category);
        contentValues.put(COL_5, importance);
        db.update(Table_Name,contentValues,"id =?",new String[]{id});
                return true;
    }

    public Integer DeleteData (String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(Table_Name,"ID = ?",new String[]{id});
    }
}

这是我的Db Class

{{1}}

0 个答案:

没有答案