2015-12-15 49 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