簡體   English   中英

如何使用OnItemLongClickListener()從SQLite創建的列表視圖中刪除項目?

[英]How to delete items from listview created from SQLite using OnItemLongClickListener()?

我在使用SimpleCursorAdapter創建的列表視圖中找不到要刪除的答案,所以我在片段中創建了該列表視圖,這是代碼

final Cursor cursor = myDb.cautarevenituri();

    // The desired columns to be bound
    final String[] columns = new String[] {
            DatabaseHelper.COL_2,
            DatabaseHelper.COL_3
    };

    int[] toviewids = new int[] { R.id.nume_item,R.id.valoare_item};
    dataAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(),R.layout.item_layout,cursor,columns,toviewids,0);
    //
    final ListView listView = (ListView)getView().findViewById(R.id.listView_venituri);

    listView.setAdapter(dataAdapter);
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                                       int position, long arg3) {
            Toast.makeText(getActivity(), " " + position, Toast.LENGTH_LONG).show();

            //delete from listview in database and listview too??
            //
            //

            return false;
        }

    });

謝謝。

從數據庫中刪除並刷新列表視圖。

您必須更新傳遞給適配器的列表,然后調用adapter.notifyDataSetChanged()

使用swapCursor()方法更新SimpleCursorAdapter的數據:

myDb.getWriteableDatabase().delete(TABLE_NAME, KEY_ID + "=?", position);
Cursor newCursor = myDb.getReadableDatabase().query(**etc.**);
dataAdapter.swapCursor(newCursor);

暫無
暫無

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

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