簡體   English   中英

在onitemlongclick監聽器中刪除listview和數據庫中的值

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

單擊ListView時,它會從索引處的ListView中刪除值,但不會再次從該索引中刪除該值。

例如,它刪除索引1中的值,如果我刪除該值,則不會從索引1中刪除。如果選擇其他索引則從另一個索引中刪除

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;
        }

在列表視圖上設置數據(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方法(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;

刪除數據也會影響列表視圖中顯示的id,希望以序列形式將id更改為1,2,3。 謝謝

嘗試這個,

 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.
}

添加此代碼后,請告訴我@Harpreet。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM