简体   繁体   中英

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

I can't find an answer to delete in a listview created using SimpleCursorAdapter So i make this listview in a fragment, here's the code

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

    });

Thank you.

从数据库中删除并刷新列表视图。

您必须更新传递给适配器的列表,然后调用adapter.notifyDataSetChanged()

Use the swapCursor() method to update the data inside a SimpleCursorAdapter :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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