1

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.

Dragos Rachieru
  • 602
  • 8
  • 21

3 Answers3

0

Delete from Database and refresh the listview.

Umer Asif
  • 441
  • 1
  • 4
  • 15
0

You have to update your list that you passed into the adapter and then call adapter.notifyDataSetChanged()

Claud
  • 1,065
  • 3
  • 26
  • 38
  • the question is how to update it? i can't use dataAdapter.add() – Dragos Rachieru Feb 01 '16 at 16:14
  • You can use notifyDataSetChange() or just set the adapter again like you did `listView.setAdapter(dataAdapter);` – Claud Feb 01 '16 at 16:16
  • how to change the adapter, i can't change the SimpleCursorAdapter – Dragos Rachieru Feb 01 '16 at 16:24
  • You can declare a newAdapter with your new list that you updated and then set the adapter. However The correct way is to make a custom adapter and then refresh the list. List this http://stackoverflow.com/questions/18395753/how-to-dynamically-update-a-listview-with-custom-adapter – Claud Feb 01 '16 at 16:28
0

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);
PPartisan
  • 8,173
  • 4
  • 29
  • 48