2015-06-09 16 views
2

我是新來的android開發,我已經使用遊標適配器填充值到列表視圖。我想要使​​用列表視圖刪除和更新值,但我不知道如何使用遊標適配器完成這些操作。也不是我能點擊列表視圖項刪除和更新從cusrsor適配器的值

我已經在我的數據庫處理程序類用於刪除下面的方法和更新值

刪除方法

public void DeletingCustodian(Custodians custodians) 
    { 
     SQLiteDatabase db_database = getWritableDatabase(); 
     //Deleting the custodian from the Database where the custodian ID matches to the selcted ID 
     db_database.delete(TABLE_CUSTODIAN,CUSTODIAN_ID + "=?" , new String[]{String.valueOf(custodians.getCust_id())}); 
     db_database.close(); 
    } 

更新方法

public int updateCustodian(Custodians cust) 
    { 
     SQLiteDatabase db_database = getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(CUSTODIAN_NAME,cust.getCust_Name()); 
     values.put(CUSTODIAN_DESIGNATION,cust.getCust_Design()); 
     values.put(CUSTODIAN_DEPARTMENT,cust.getDepartment()); 

     int roweffected = db_database.update(TABLE_CUSTODIAN,values,CUSTODIAN_ID + "=?", new String[]{String.valueOf(cust.getCust_id())}); 
     db_database.close(); 
     return roweffected; 
    } 

我已經創建了一個上下文顯示編輯和刪除的菜單,當選擇某個項目時顯示。通過查詢數據庫

public void onCreateContxtManu(ContextMenu menu,View view, ContextMenu.ContextMenuInfo menuInfo) 
    { 
     super.onCreateContextMenu(menu,view,menuInfo); 

     menu.setHeaderTitle("Custodian Options"); 
     menu.add(Menu.NONE,EDIT,menu.NONE,"Edit Custodian"); 
     menu.add(Menu.NONE,DELETE,menu.NONE,"Delete Custodian"); 
    } 

public void deletingitemsfromlist() 
    { 
     CustodianListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 

       return false; 
      } 
     }); 

    } 
    public boolean onContextItemSelected(MenuItem item) 
    { 
     switch(item.getItemId()) 
     { 
      case EDIT: 


       break; 
      case DELETE: 


       break; 
     } 

     return false; 
    } 

回答

1

試試這個,它可能h ELP你

在數據庫連接器

// Delete a row in Local database 
    public void delete(int ids) { 

     database.delete(TABLE_NAME, KEY_ROWID + " = " + ids, null); 
    } 
在你的列表視圖頁面代碼

刪除代碼Onitem長單擊刪除

@Override 
       public boolean onItemLongClick(AdapterView arg0, View v, 
       int position, long arg3) 
      { 
       try 
       { 

       TextView id = (TextView) v.findViewById(R.id.textView4); 

       ids1 = Integer.parseInt(id.getText().toString()); 
       AlertDialog.Builder ad = new AlertDialog.Builder(Page1_Landing.this); 
       //ad.setTitle("Notice"); 
       ad.setMessage("Sure you want to delete this Item ?"); 

       //Delete Positive Button 
       ad.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() 
       { 


       @SuppressWarnings("deprecation") 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        try 
        { 



          //Delete of record from Database and List view. 
          helper.delete(ids1); 
          cur.requery(); 
          myCursorAdap.notifyDataSetChanged(); 
          List.setAdapter(myCursorAdap); 
          Toast.makeText(Page1_Landing.this,"Selected Product is Successfully Deleted...!", Toast.LENGTH_SHORT).show(); 



        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
          Log.i("Exception ", "in Delete a Product"); 
        } 
       } 
       }); 

       //long press delete cancel 
       ad.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() 
       { 

       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 


        dialog.dismiss(); 


       } 
       }); 
       ad.show(); 



       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
       return true; 
       } 

更新代碼

DatabaseConnector helper = new DatabaseConnector(this); 

helper.open(); 

helper.updatedetails(rowID,name1,desc, dept); 
Toast.makeText(getApplicationContext(),"Updated Successfully...!", Toast.LENGTH_SHORT).show(); 

更新數據庫代碼

// updating the data .... 
    public boolean updatedetails(long rowId, String name, 
      String desc, String dept) 
    { 
     ContentValues args = new ContentValues(); 

     args.put(KEY_ROWID, rowId); 
     args.put(KEY_NAME, product); 
     args.put(KEY_DESC, cat); 
     args.put(KEY_DEPT, serial); 



     return database.update(TABLE_NAME, args, KEY_ROWID + "=" + rowId, null) > 0; 
    } 
+0

讓我知道您在此代碼中遇到的任何錯誤 – Mano

+0

我確實嘗試了您的步驟,但我面臨的是一些小問題。事情是我已經在listview頁面中聲明瞭我的遊標適配器類來填充列表視圖。 – NikhilRcop

+0

Thankx馬諾您的步驟working.But得到一個空點異常,因爲我已經使用了我在我的問題中提到的相同的更新方法,而不是使用整數我已經使用無效可以讓我知道我可以如何解決更新問題 – NikhilRcop

1

更新數據庫中的數據,並獲得新的光標再次然後調用

oldCursor = myCursorAdapter.swapCursor(newCursor); // hands you back oldCursor 

或:

myCursorAdapter.changeCursor(newCursor); // automatically closes old Cursor 

myCursorAdapter.notifyDataSetChanged()通知ListView,該數據集有改變了,它應該刷新自己

+0

我很抱歉,但我不知道該怎麼做,但現在我嘗試從列表視圖中獲取刪除項目。 – NikhilRcop

+0

簡單修改您的數據庫,然後查詢數據庫中的新數據並調用上述方法。 – Kartheek