简体   繁体   English

在onitemlongclick监听器中删除listview和数据库中的值

[英]delete value from listview and database as well in onitemlongclick listener

On clicking on ListView it deletes the value from ListView at index but not deleted that value from that index again. 单击ListView时,它会从索引处的ListView中删除值,但不会再次从该索引中删除该值。

for eg., it deletes the value from index 1 and again if I delete that value it doesn't delete from index 1. deleted from another index if selected other index 例如,它删除索引1中的值,如果我删除该值,则不会从索引1中删除。如果选择其他索引则从另一个索引中删除

onItemLongClickListener (Book.class) onItemLongClickListener(Book.class)

        lview.setOnItemLongClickListener(new 
        AdapterView.OnItemLongClickListener() {
            @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View 
           view, int i, long l) {

            String parse = String.valueOf(i);

            Integer deletedrows =  mydb.DeleteData(String.valueOf(i));
            if(deletedrows > 0 )
            {

                Toast.makeText(BookList.this, "deleted" + parse, Toast.LENGTH_SHORT).show();

            }
            else
            {

                Toast.makeText(BookList.this, "not Deleted" + parse, Toast.LENGTH_SHORT).show();
            }
            return true;
        }

Set data on list view (Book.class) 在列表视图上设置数据(Book.class)

      public void getdata()
       {

     ListAdapter lviewAdapter;
    ArrayList<HashMap<String, String>> userList;

        userList = mydb.getalldata();
        if(userList.isEmpty())
        {
           showdata("Error","Nothing Found");
           return;
        }
         ArrayList list = new ArrayList();
        StringBuffer buffer = new StringBuffer();

       lviewAdapter = new SimpleAdapter(BookList.this, userList, 
        R.layout.book_custom_list,
            new String[]{"ID","url","title"},
            new int[]{  R.id.customid,R.id.customurl,R.id.customtitle});
       lview.setAdapter(lviewAdapter);


   }

getdata method (Database Helper.class) getdata方法(Database Helper.class)

   public ArrayList<HashMap<String, String>> getalldata()
    {
      SQLiteDatabase db = this.getWritableDatabase();
      ArrayList<HashMap<String, String>> userList = new ArrayList<>();
      Cursor cursor = db.rawQuery("select * from " + TABLE_NAME,null);

    while (cursor.moveToNext()){
        HashMap<String,String> user = new HashMap<>();
        user.put("ID",cursor.getString(cursor.getColumnIndex(Id_name)));

    user.put("title",cursor.getString(cursor.getColumnIndex(title_name)));
        user.put("url",cursor.getString(cursor.getColumnIndex(url_name)));
        userList.add(user);
    }
    return userList;

delete data also affected the id showing in the list view, want that to change the id to 1,2,3 in sequential form. 删除数据也会影响列表视图中显示的id,希望以序列形式将id更改为1,2,3。 thanks 谢谢

Try this, 尝试这个,

 lview.setOnItemLongClickListener(new 
    AdapterView.OnItemLongClickListener() {
        @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View 
       view, int i, long l) {

        String parse = String.valueOf(i);

        //Add this code when you try to delete the item

        try{
        SQLiteDatabase db = getWritableDatabase();
        String query = "delete from TABLE_NAME where id like "+"'"+(ListView_ID)+"'";
        db.delete(TABLE_ORDERS, KEY_LISTVIEW_ID + "='" + LISTVIEW_ID+ "'", null);
        Cursor cursor = db.rawQuery(query,null);
        cursor.close();

        //Now set the Data in the ListView from the database after you delete the item
        getFreshData();
    }
    catch (Exception e){
        Log.e("order_proof",""+e);
    }

        return true;
    }




private void getFreshData(){

 String query = "SELECT * FROM TABLE_NAME";
 ArrayList<HashMap<String, String>> userList = new ArrayList<>();
  Cursor cursor = db.rawQuery("select * from " + TABLE_NAME,null);

while (cursor.moveToNext()){
    HashMap<String,String> user = new HashMap<>();
    user.put("ID",cursor.getString(cursor.getColumnIndex(Id_name)));

user.put("title",cursor.getString(cursor.getColumnIndex(title_name)));
    user.put("url",cursor.getString(cursor.getColumnIndex(url_name)));
    userList.add(user);
}
return userList;

//populate this list in the ListView.
}

Let me know after you add this code @Harpreet. 添加此代码后,请告诉我@Harpreet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM