2012-11-01 28 views
0

我有一個上下文菜單來刪除listactivity上的數據,但是當我在應用程序上嘗試它時,數據不會被刪除。以前我在AlmagHelper類中使用子串查詢輸入數據。如果我創建的編碼有問題? 請幫助..在列表活動中刪除帶有contextmenu的子字符串

,我創建了這個活動類..

public class Pendapatan extends ListActivity { 
    Cursor model=null; 
    AlmagAdapter adapter=null; 
    AlmagtHelper helper=null; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listview_nasabah); 

     helper=new AlmagtHelper(this); 

     model=helper.getAllPendapatan(); 
     startManagingCursor(model); 
     adapter=new AlmagAdapter(model); 
     setListAdapter(adapter); 

     registerForContextMenu(getListView()); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     getMenuInflater().inflate(R.menu.action_pendapatan, menu); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item){ 
     AdapterView.AdapterContextMenuInfo info= 
      (AdapterContextMenuInfo)item.getMenuInfo(); 

     switch (item.getItemId()) { 
     case R.id.hapus_pendapatan: 
      delete(info.id); 
      return(true); 
     } 
     return false; 
    } 

    private void delete(final long rowId) { 
     if (rowId>0) { 
      new AlertDialog.Builder(this) 
      .setTitle("Hapus") 
      .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        prosesDelete(rowId); 
       } 
      }) .setNegativeButton("Batal", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int whichButton) { 
       } 
      }).show(); 
       } 
    } 

    private void prosesDelete(long rowId) { 
     String[] args={String.valueOf(rowId)}; 

     //is there something wrong with this code? 

     helper.getWritableDatabase().delete("pendapatan", "_id =?", args); 
     model.requery(); 
    } 

這是進入包含在類AlmagHelper數據的代碼..

public Cursor getAllPendapatan() { 
    return(getReadableDatabase() 
        .rawQuery("SELECT substr(_id, 1, 10) as _id, sum(value) as total FROM pendapatan GROUP BY _id ", 
             null)); 
} 

我已經嘗試使用此代碼但它不起作用

private void prosesDelete(long rowId) { 
     String[] args={String.valueOf(rowId)}; 

     helper.getWritableDatabase().delete("pendapatan", "(substr(_id, 1, 10)) = _id =?", args); 
     model.requery(); 
    } 

是否有任何解決方案爲我噸做什麼?任何解決方案對我來說都非常有用。謝謝:-)

回答

0

刪除後只是做 setListAdapter(adapter);

+0

感謝您的答案,但它仍然無法正常工作。放在哪裏setListAdaper(適配器)呢?是否在prosesDelete? –

相關問題