2015-12-15 48 views
0

我的數據庫類是這樣的,當我打電話deleteEntry,它不會刪除0指數,這是數據庫類方法填充刪除零的索引列表視圖中,從數據庫的Android

public int deleteEntry(String id) 
    { 
     SQLiteDatabase db = dbHelper.getWritableDatabase(); 
     //String id=String.valueOf(ID); 
     String where="ID=?"; 

     int numberOFEntriesDeleted= db.delete("TIME", where, new String[]{id}) ; 
     Toast.makeText(context, "Number of Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show(); 
     return numberOFEntriesDeleted; 
    } 

這是我onItemClickListener

list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       Log.d("Clicked item id", " " + id); 

       // data.remove(id); 
       String row = (String.valueOf(id)); 
       data.deleteEntry(row); 
+0

請檢查http://stackoverflow.com/a/25328608/1790537 – Nas

回答

0

你可以嘗試這樣的:

//---deletes a particular entry--- 
public int deleteEntry(String id) 
{ 
    SQLiteDatabase db = dbHelper.getWritableDatabase(); 
    return db.delete("TIME", "ID = " + id, null) ; 
} 

順便說一句,確保「時間」是你的表名,你也關閉數據庫。

0

嘗試傳球的位置,而不是一個ID:

list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     Log.d("Clicked item id", " " + id); 
     Log.d("Clicked item position", " " + position); 
     String row = (String.valueOf(position)); // the change is here 
     data.deleteEntry(row); 
} 

嘗試此操作後,請查看所獲得的日誌,並嘗試確定哪個值(id或位置)更好。無論如何,我想你應該存儲你在某個地方顯示的每個項目的數據庫ID,這樣你就不會只依賴列表中的位置(一旦從你的表中刪除任何項目,這將不起作用),因爲ID會不是順序的。

+0

謝謝Orlang ure .... :)它適用於我...偉大的工作(y) –

0

這裏是我做過什麼 認爲我有兩個表等動態列表視圖兩個項目(即自定義適配器)

我刪除的名字物品(你可以選擇的項目的名稱位置

Yourlist.setOnItemLongClickListener(新OnItemLongClickListener(){@覆蓋公共布爾onItemLongClick(適配器視圖爲arg0,查看ARG1,INT ARG2,長ID){

字符串str = Yourlist.getItemAtPosition(ARG2)的ToString() ;

//考慮我有兩個表

//創建一個名爲feedslist了一個名爲 「URL」,其中項目(供稿網址),將存儲

mydb.execSQL列的表格(「CREATE TABLE IF NOT EXISTS feedslist(id INTEGER PRIMARY KEY AUTOINCREMENT,url varchar);「);

//創建一個名爲其中項目(供稿名)將存儲

mydb.execSQL 「名稱」 列的表稱爲字幕列表(「CREATE TABLE IF NOT EXISTS subtitleslist(ID INTEGER PRIMARY KEY AUTOINCREMENT ,name varchar);「);

//這是刪除條目 alert.setPositiveButton(R.string.deleteok,新DialogInterface.OnClickListener方法(){

                    @Override 

                    public void onClick(DialogInterface dialog, int which) { 


                        //on positive click we delete the feed from selected position 


                        //we're gonna delete them from the db 



                        //using cursor 

                        Cursor cursor =mydb.rawQuery("SELECT * FROM feedslist;", null); 

                        Cursor cursor2 =mydb.rawQuery("SELECT * FROM subtitleslist;", null); 


                        String url = ""; 

                        String name = ""; 


                        //set url 

                        if (cursor != null && cursor.moveToFirst()) { 


                            while (!cursor.isAfterLast()) { 


                                //we get items at selected position 

                                url = mItems.get(datposition); 

                                cursor.moveToNext(); 

                            } 

                            cursor.close(); 

                        } 


                        //set feed name 

                        if (cursor2 != null && cursor2.moveToFirst()) { 


                            while (!cursor2.isAfterLast()) { 


                                //we get items at selected position 

                                name = mItems2.get(datposition); 

                                cursor2.moveToNext(); 

                            } 

                            cursor2.close(); 

                        } 


                        //set the names of the two tables 

                        String table1 = "feedslist"; 

                        String table2 = "subtitleslist"; 


                        //set where clause 

                        String whereClause_url = "url" + "=?"; 

                        String whereClause_feed = "name" + "=?"; 


                        //set the where arguments 

                        String[] whereArgs_url = new String[] { String.valueOf(url) }; 

                        String[] whereArgs_name = new String[] { String.valueOf(name) }; 


                        //delete 'em all 

                        mydb.delete(table1, whereClause_url, whereArgs_url); 

                        mydb.delete(table2, whereClause_feed, whereArgs_name); 


                        //remove items from the dynamic listview 


                        //for url 

                        mItems.remove(datposition); 


                        //for feed name 

                        mItems2.remove(datposition); 


                        //and update the dynamic list 

                        //don't move this method above the db deletion method or 

                        //you'll get javalangindexoutofboundsexception-invalid-index error 

                        adapter_dynamic.notifyDataSetChanged(); 

                        adapter_dynamic.notifyDataSetInvalidated(); 

                        listfeed.setAdapter(adapter_dynamic); 

  

您可以在這裏看到

工作程序

https://github.com/enricocid/iven-feed-reader