2013-05-15 25 views
0

我需要你的幫助。我在這方面花了太多時間,如果指定了id號,我只能更新數據庫。聲明和方法來更新sqlite數據庫

  1. 點擊列表視圖項並將數據發送到新的活動,編輯
  2. 編輯數據並返回到列表視圖活動。簡單的權利。嗯。

    @Override 
    public void onItemClick(AdapterView<?> listView, View view, int position, long id) {  
    Cursor cursor = (Cursor)listView.getItemAtPosition(position); 
    String title = cursor.getString(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_TITLE)); 
    String barcode = cursor.getString(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_BARCODE)); 
    String rowId = cursor.getString(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_ROWID)); 
    
    Intent movieEdit = new Intent(getBaseContext(), MovieEdit.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("rowId", rowId); 
    bundle.putString("title", title); 
    bundle.putString("barcode", barcode); 
    movieEdit.putExtras(bundle); 
    startActivity(movieEdit); 
    

接收來自列表視圖活性的意圖:

if (this.getIntent().getExtras() != null) { 
Bundle bundle = this.getIntent().getExtras(); 
      editTitle.setText(bundle.getString("title"));  
      editBarcode.setText(bundle.getString("barcode")); 
      editId.setText(bundle.getString("rowId")); 

      } 

通過按鈕激活的語句:

case R.id.buttonSave: 

mDb.open(); 
ContentValues values = new ContentValues(); 
values.put(MoviesDbAdapter.KEY_TITLE, title); 
values.put(MoviesDbAdapter.KEY_BARCODE, barcode); 
mDb.updateTitle("=?", title, barcode); 
mDb.close(); 

在MovieDbAdapter的方法:

public boolean updateTitle(long rowId, String title, String barcode) 
{ 
ContentValues args = new ContentValues(); 
args.put(KEY_TITLE, title); 
args.put(KEY_BARCODE, barcode); 
return mDb.update(SQLITE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 

我知道這已經做到了死亡,但無論我嘗試什麼,我都無法用新數據更新數據庫。 希望你能幫助大家。

回答

0

你的代碼更改爲以下

case R.id.buttonSave: 

    mDb.open(); 
    mDb.updateTitle(rowId, title, barcode); 
    mDb.close(); 
+0

感謝您抽出時間來回答我的查詢 - 可惜沒有成功。該應用程序不會崩潰或抱怨,但沒有數據更新到數據庫。 – user2384517

+0

更新語句返回的值是什麼,並且在傳遞之前檢查rowId是否保存一個值,並檢查數據庫中是否存在相同的值。如果所有這些都很好,並且仍然沒有成功發佈您的mDb對象類的代碼。 – prvn

+0

好吧,我可以看到從ListViewActivity傳遞的rowId的值,它與數據庫中的值匹配。來自logcat和數據庫轉儲的錯誤沒有任何異常。 – user2384517