重复相同的活动

时间:2013-07-16 12:13:45

标签: android sqlite android-sqlite

我正在按照本教程http://www.sarahghaffary.com/android-quotes-generator-sample-application-using-alarmmanager-and-sqlite-database/进行操作,但我想知道即使用完引号,我怎么能继续重复引号。我知道我需要更改下面的代码但不确定如何。只有初学者,任何帮助都会很棒!

代码:

 public String getRandomQuote(){
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT  * FROM " + QUOTES_TABLE_NAME + " WHERE " + KEY_ISREAD + " = 0 ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor != null)
    if(cursor.moveToFirst()){
        db.execSQL("update " + QUOTES_TABLE_NAME + " set " + KEY_ISREAD + " = 1 where "+ KEY_ID +" = "+ cursor.getString(0));
        return cursor.getString(1);
    }
return "Sorry, you ran out of quotes <img src="http://www.sarahghaffary.com/wp-includes/images/smilies/icon_biggrin.gif" alt=":D" class="wp-smiley"> ";
}

1 个答案:

答案 0 :(得分:2)

public String getRandomQuote(){
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT  * FROM " + QUOTES_TABLE_NAME + " WHERE " + KEY_ISREAD + " = 0 ORDER BY RANDOM() LIMIT 1";
String UpdateAllQuery= "Update "+ QUOTES_TABLE_NAME + " set " + KEY_ISREAD + " = 0";
Cursor cursor = db.rawQuery(selectQuery, null);
String quote = "";

if (cursor != null){
        cursor.moveToFirst();  
        if (cursor.getCount() < 1){
            db.execSQL(UpdateAllQuery);
            cursor = db.rawQuery(selectQuery, null);
            cursor.moveToFirst();
        }
        db.execSQL("update " + QUOTES_TABLE_NAME + " set " + KEY_ISREAD + " = 1 where "+ KEY_ID +" = "+ cursor.getString(0));
        quote = cursor.getString(1);
        return quote;
}else{
   return "Sorry, you ran out of quotes <img src="http://www.sarahghaffary.com/wp-includes/images/smilies/icon_biggrin.gif" alt=":D" class="wp-smiley"> ";
}