2014-04-03 60 views
1

我正在嘗試製作Android應用程序來幫助患有頭痛的人。我有一個用於存儲危機的sqlite數據庫,用戶可以通過按下按鈕來添加危機。同樣的按鈕用於表示危機已經結束。換句話說,當你感到頭痛來臨時,你按下按鈕;然後,當它結束時,再次按下它,應用程序會更新相應的條目,並顯示「結束日期」。關於內容提供商的更新什麼都不做

但是,如果我的插入效果不錯,我的更新根本不會更新。這是它應該如何工作:

我首先檢索我的數據庫中的最新條目(這是最大的ID),然後我得到實際的日期,並把它放在一個ContentValue。最後我更新條目。

這裏是按鈕的代碼:

public void onClickStartStop(View v){ 
    Log.v("andromed", "Starting/Stopping crisis"); 
    String d = new Date().toString(); 
    ContentValues cv = new ContentValues(); 
    String user_info = ""; 
    String[] projection = {CriseContract.COLUMN_NAME_CRISE_ID, CriseContract.COLUMN_NAME_CRISE_DEBUT,CriseContract.COLUMN_NAME_CRISE_FIN}; 
    Cursor criseCursor = getContentResolver().query(CriseContract.CONTENT_URI, projection,"SELECT MAX("+CriseContract.COLUMN_NAME_CRISE_ID+") FROM "+CriseContract.TABLE_NAME, null, null); 
    Log.v("andromed",""+criseCursor.getCount()); 
    if(criseCursor.getCount()>=0){ 
     while(criseCursor.moveToNext()){ 
      String date_fin = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_FIN)); 
      if(!(date_fin==(null))){ 
       Log.v("andromed","Date exists "+date_fin); 
       user_info = "Crise enregistrée"; 
       cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, d); 
       Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv); 
      }else{ 
       String date_deb = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_DEBUT)); 
       if(date_deb==null){ 
        Log.v("andromed","No date in db"); 
        user_info = "Crise enregistrée"; 
        cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, d); 
        Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv); 
       }else{ 
        Log.v("andromed", "Need to close the crisis"); 
        cv.put(CriseContract.COLUMN_NAME_CRISE_FIN, d); 
        int tmp = getMaxId(); 
        String where = CriseContract.COLUMN_NAME_CRISE_ID+"="+tmp; 
        String[] st = {""+tmp}; 
        int nup = getContentResolver().update(CriseContract.CONTENT_URI,cv, where, null); 
        Log.v("andromed", nup+" rows updated"); 
        user_info = "Crise terminée"; 
       } 
      } 
     } 
    }else{ 
     user_info = "Erreur lors de la lecture"; 
    } 
    Toast t = Toast.makeText(getApplicationContext(),user_info, Toast.LENGTH_LONG); 
    t.show(); 
} 

(不介意日誌和烤麪包的東西,只是對我來說)。

這裏是我的函數來獲取最大ID:

private int getMaxId(){ 
    String[] projection = {CriseContract.COLUMN_NAME_CRISE_ID}; 
    String selection = "SELECT MAX("+CriseContract.COLUMN_NAME_CRISE_ID+") FROM "+CriseContract.TABLE_NAME; 
    Cursor c = getContentResolver().query(CriseContract.CONTENT_URI, projection, selection, null, null); 
    Log.v("andromed", ""+c.getCount()); 
    int maxid=-1; 
    if(c!=null){ 
     while(c.moveToNext()){ 
      maxid = c.getInt(c.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_ID)); 
     } 
    } 
    Log.v("andromed", "Greatest id in table Crise : "+maxid); 
    return maxid; 
} 

當然,我的合同類:

public final static class CriseContract{ 
    public static final String AUTHORITY = "com.piertris.andromed"; 
    public static final String BASE_PATH = "database"; 
    public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/"+BASE_PATH); 
    public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE+"/"+BASE_PATH; 
    public static final String CONTENT_ITEM_TYPE= ContentResolver.CURSOR_ITEM_BASE_TYPE+"/andromed"; 
    public static final String TABLE_NAME = "crises"; 
    public static final String COLUMN_NAME_CRISE_ID = "criseid"; 
    public static final String COLUMN_NAME_CRISE_DEBUT = "date_debut"; 
    public static final String COLUMN_NAME_CRISE_FIN = "date_fin"; 
    public static final String COLUMN_NAME_INTENSITE = "intensite"; 
    public static final String COLUMN_NAME_SYMPTOM = "symptome"; 
    public static final String COLUMN_NAME_MED = "prise_med"; 
    public static final String COLUMN_NAME_MEDS = "type_med"; 
    public static final String COLUMN_NAME_AURA = "aura"; 
    public static final String COLUMN_NAME_COMMENT = "comments"; 

} 

當我試圖結束當前的危機,我logcat的告訴我, 0行已更新。

感謝SO,我已經更正了由於功能錯誤使用導致的其他問題,但是這一次,我發現了唯一的鏈接:Android content provider not updating database,OP剛剛添加了一條評論,說他更新了他的ContentProvider,但而已。

我在做什麼錯?我是否「誤稱」我的專欄名稱?我是否濫用更新功能?

感謝您的幫助。

編輯

感謝Jozua,我意識到,我沒有實現我的ContentProvider文件的更新功能。好吧,我現在感覺非常愚蠢。一旦寫入update()函數,我會告訴你它是如何工作的。

再次感謝Jozua。

+0

頭痛呵呵...酷 – EdmDroid

+0

@hiphopdroid嘿,學校的項目,你知道... – Vrashnak

+0

是的@vrashnak,我知道 – EdmDroid

回答

0

好吧,我有點解決了這個問題,但方式非常糟糕。考慮到我爲了添加幾乎所有需要的東西,只是開始日期和id,我只需要將我的update()請求轉換爲delete(),然後是update(),就可以檢索到危機這一事實。其中我傳遞一個ContentValue,其中包含之前刪除的行的值。

我知道這是非常糟糕的編程,但至少它工作。

我不會接受我的回答,以防有人發現我的update()函數有什麼問題,並可能幫助其他人(甚至是我,以便我可以改進我的代碼)。

就是這樣:)

下面是相關的代碼部分:

public void onClickStartStop(View v){ 
    //go straight to relevant part 
    String date_deb = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_DEBUT)); 
    Log.v("andromed", "Need to close the crisis"); 
    cv.put(CriseContract.COLUMN_NAME_CRISE_ID, criseCursor.getInt(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_ID))); 
    cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, date_deb); 
    cv.put(CriseContract.COLUMN_NAME_CRISE_FIN, d); 
    int ndel = getContentResolver().delete(CriseContract.CONTENT_URI, CriseContract.COLUMN_NAME_CRISE_ID+"=?", new String[] {""+criseCursor.getInt(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_ID))}); 
    Log.v("andromed", ndel+" rows deleted"); 
    Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv); 
    user_info = "Crise terminée"; 
    //End of relevant code 
} 

感謝那些誰可能也會被搜索。