2012-10-11 45 views
1

我是Java,Android和SQLite的新手,我一直堅持這一點。我有列,即_id(自動增量),type,amount(int),categorydescription。三個查詢。在Android中刪除查詢和刷新ListView(sqlite)

首先,我無法刪除從數據庫檢索到的事務(行)。 (請參閱附件Logcat)。

其次,我需要知道如何在刪除條目後刷新ListView。

第三,我覺得這個愚蠢的問題。如果我打開一次數據庫並檢索數據,它會這樣做。此外,在不關閉數據庫的情況下,如果我刪除一個事務,它不會刪除它,並給出錯誤「數據庫未打開」。此外,如果我在檢索後關閉數據庫,然後在刪除時再次打開數據庫,它將起作用。我沒有明白。主要的問題是第一位的,但請你回答,如果你知道上述任何

final ListView lv = getListView(); 
    localArrayList.clear();    //To Stop the silly repeating issue 
    localDbCrud = new DbCrud(this); 
    localAdapter = new SimpleAdapter(
      this, 
      localArrayList, 
      R.layout.transaction_list_item, 
      new String[]{"Amount","Category","Date","Description"}, 
      new int[]{R.id.tli_amount,R.id.tli_category,R.id.tli_date,R.id.tli_desc}); 

    localDbCrud.open(); 
    DbCrud.getAllTransaction();    //Using HashMap localHashMap , localHashMap.put(localArrayList) in loop 
    localDbCrud.close(); 
    lv.setOnItemLongClickListener(new OnItemLongClickListener() { 

     public boolean onItemLongClick(final AdapterView<?> arg0, View arg1, 
       final int arg2, final long arg3) { 
      // TODO Auto-generated method stub 
      AlertDialog.Builder ladbuilder = new Builder(TransactionList.this); 
      ladbuilder.setTitle("Choose Your Option"); 
      ladbuilder.setItems(R.array.alertdialog_prompt, new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int selected_item) { 
        // TODO Auto-generated method stub 
        if (selected_item == 0){ 
         localDbCrud.open(); 
         HashMap itemMap = (HashMap)localAdapter.getItem(arg2); 
         int item_id =(Integer) itemMap.get("Id"); 
         DbCrud.deleteTransaction(item_id); 
         //final int ite= (Integer) arg0.getItemAtPosition(arg2); 
         // final int item_id = c.getInt(c.getColumnIndex(DbCrud.TN_ID)); 
         //DbCrud.deleteTransaction(item_id); 
         localDbCrud.close(); 

        } 
        else{ 


         //update code 

        } 
       } 
      }); 
      AlertDialog localad = ladbuilder.create(); 
      localad.show(); 


      return false; 
     } 
    }); 

    localDbCrud.close(); 

    setListAdapter(localAdapter); 

} 

logcat的

10-13 01:21:26.804: E/AndroidRuntime(22023): FATAL EXCEPTION: main 
10-13 01:21:26.804: E/AndroidRuntime(22023): java.lang.ClassCastException: java.lang.String 
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.hishighness.budgetracker.TransactionList$1$1.onClick(TransactionList.java:62) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:878) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.widget.ListView.performItemClick(ListView.java:3701) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1970) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.os.Handler.handleCallback(Handler.java:587) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.os.Looper.loop(Looper.java:130) 
    10-13 01:21:26.804: E/AndroidRuntime(22023): at android.app.ActivityThread.main(ActivityThread.java:3687) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at java.lang.reflect.Method.invokeNative(Native Method) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at java.lang.reflect.Method.invoke(Method.java:507) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
10-13 01:21:26.804: E/AndroidRuntime(22023): at dalvik.system.NativeStart.main(Native Method) 

這裏的getallTransaction()功能。本是哈希映射在我的項目的唯一痕跡

public static void getAllTransaction(){ 

    Cursor localCursor = localDatabase.query(true, TN_TABLE, null, null, null, null, null, null, null) ; 
    if (localCursor != null) { 

     localCursor.moveToFirst(); 
      do{ 
       HashMap<String,String> temp = new HashMap<String,String>(); 
       temp.put("Amount", localCursor.getString(localCursor.getColumnIndex("amount"))); 
       temp.put("Category", localCursor.getString(localCursor.getColumnIndex("category"))); 
       temp.put("Date", localCursor.getString(localCursor.getColumnIndex("date"))); 
       temp.put("Description", localCursor.getString(localCursor.getColumnIndex("description"))); 
       temp.put("Id", localCursor.getString(localCursor.getColumnIndex("_id"))); 
       TransactionList.localArrayList.add(temp); 



      }while (localCursor.moveToNext()); 

    } 

回答

1

首先,我無法刪除數據庫

檢索事務處理(行)

61行將arg0.getItemAtPosition(arg2)的結果投射到Cursor,但返回類型可能是HashMap(正如它在Logcat輸出中所述)。通常情況下,您會從數據庫查詢中獲得Cursor

如果您在構建時將每個數據庫行的ID放入HashMap,那麼您將能夠將該ID傳遞給onClick事件中的deleteTransaction()呼叫。所以,你需要

temp.put("Id", localCursor.getInt(localCursor.getColumnIndex("_id"))); 

getAllTransaction(),然後修改你的onClick()方法做這樣的事情:

localDbCrud.open(); 
HashMap itemMap = (HashMap)localAdapter.getItem(arg2); 
int item_id = Integer.parseInt((String)itemMap.get("Id")); 
DbCrud.deleteTransaction(item_id); 
localDbCrud.close(); 

我還建議重新命名arg2(和其他人)有更清晰的名稱,以便代碼更容易遵循。

其次,我需要知道如何刪除條目

你可以叫notifyDataSetChanged()你的適配器上刷新ListView你已經做了更改數據集後後刷新列表視圖。

編輯:請注意SimpleAdapter是用於靜態數據,所以你的里程可能會有所不同。最好的解決方案可能是切換到不同類型的適配器,例如ArrayAdapter,或者每次更改數據集時都創建一個新的SimpleAdapter

+0

我已經使用哈希映射來檢索數據,但怎麼回事。進一步,如果類型轉換是問題,你如何建議我應該繼續... – Hishighness731

+0

根據上面的代碼很難給出明確的答案,但你可能想要做這樣的事情:'Cursor c = localDbCrud.query( table_name,null,「_id =」+ arg0.getItemAtPosition(arg2),null,null,null,null);' – acj

+0

我無法在當前類中調用該查詢,因此需要在DbCrud中爲該函數調用一個函數' m仍然卡在這.. – Hishighness731