如何只在android数据库中显示某些元素

时间:2014-05-13 03:33:13

标签: android sqlite listview filter

我有一个内部的Android数据库,它存储的问题包括"问题","正确"," Wrong1"," Wrong2"," Wrong3"和" Set"。我想要一个列表视图,只显示该集合中的元素。例如,如果我有5个问题" Set"正在"科学",我单击科学选项卡,它应该只显示问题与科学"科学"在列表视图中。

    public class DBAdapter {


// For logging:
private static final String TAG = "DBAdapter";

// DB Fields
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
/*
 * CHANGE 1:
 */
// TODO: Setup your fields here:
public static final String KEY_QUESTION = "question";
public static final String KEY_CORRECT = "correct";
public static final String KEY_WRONG1 = "wrong1";
public static final String KEY_WRONG2 = "wrong2";
public static final String KEY_WRONG3 = "wrong3";
public static final String KEY_SET = "testing";

// TODO: Setup your field numbers here (0 = KEY_ROWID, 1=...)
public static final int COL_QUESTION = 1;
public static final int COL_CORRECT = 2;
public static final int COL_WRONG1 = 3;
public static final int COL_WRONG2 = 4;
public static final int COL_WRONG3 = 5;
public static final int COL_SET = 6;


public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_QUESTION, KEY_CORRECT, KEY_WRONG1, KEY_WRONG2, KEY_WRONG3, KEY_SET};

// DB info: it's name, and the table we are using (just one).
public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";
// Track DB version if a new version of your app changes the format.
public static final int DATABASE_VERSION = 4;   

private static final String DATABASE_CREATE_SQL = 
        "create table " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " integer primary key autoincrement, "

        /*
         * CHANGE 2:
         */
        // TODO: Place your fields here!
        // + KEY_{...} + " {type} not null"
        //  - Key is the column name you created above.
        //  - {type} is one of: text, integer, real, blob
        //      (http://www.sqlite.org/datatype3.html)
        //  - "not null" means it is a required field (must be given a value).
        // NOTE: All must be comma separated (end of line!) Last one must have NO comma!!
        + KEY_QUESTION + " string not null, "
        + KEY_CORRECT + " string not null, "
        + KEY_WRONG1 + " string not null,"
        + KEY_WRONG2 + " string not null,"
        + KEY_WRONG3 + " string not null,"
        + KEY_SET + " string not null"

        // Rest  of creation:
        + ");";

// Context of application who uses us.
private final Context context;

private DatabaseHelper myDBHelper;
private SQLiteDatabase db;

/////////////////////////////////////////////////////////////////////
//  Public methods:
/////////////////////////////////////////////////////////////////////

public DBAdapter(Context ctx) {
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}

// Add a new set of values to the database.
public long insertRow(String question, String correct, String wrong1, String wrong2, String wrong3, String testing) {
    /*
     * CHANGE 3:
     */     
    // TODO: Update data in the row with new fields.
    // TODO: Also change the function's arguments to be what you need!
    // Create row's data:
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_QUESTION, question);
    initialValues.put(KEY_CORRECT, correct);
    initialValues.put(KEY_WRONG1, wrong1);
    initialValues.put(KEY_WRONG2, wrong2);
    initialValues.put(KEY_WRONG3, wrong3);
    initialValues.put(KEY_SET, testing);

    // Insert it into the database.
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return db.delete(DATABASE_TABLE, where, null) != 0;
}

public void deleteAll() {
    Cursor c = getAllRows();
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
    if (c.moveToFirst()) {
        do {
            deleteRow(c.getLong((int) rowId));              
        } while (c.moveToNext());
    }
    c.close();
}

// Return all data in the database.
public Cursor getAllRows() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String question, String correct, String wrong1, String wrong2, String wrong3, String testing) {
    String where = KEY_ROWID + "=" + rowId;

    /*
     * CHANGE 4:
     */
    // TODO: Update data in the row with new fields.
    // TODO: Also change the function's arguments to be what you need!
    // Create row's data:
    ContentValues newValues = new ContentValues();
    newValues.put(KEY_QUESTION, question);
    newValues.put(KEY_CORRECT, correct);
    newValues.put(KEY_WRONG1, wrong1);
    newValues.put(KEY_WRONG2, wrong2);
    newValues.put(KEY_WRONG3, wrong3);
    newValues.put(KEY_SET, testing);

    // Insert it into the database.
    return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}



/////////////////////////////////////////////////////////////////////
//  Private Helper Classes:
/////////////////////////////////////////////////////////////////////

/**
 * Private class which handles database creation and upgrading.
 * Used to handle low-level database access.
 */
private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DATABASE_CREATE_SQL);           
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        // Destroy old database:
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        // Recreate new database:
        onCreate(_db);
    }
}

}

0 个答案:

没有答案