0

我正在关注本教程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"> ";
}
4

1 回答 1

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"> ";
}
于 2013-07-16T12:21:29.587 回答