Android-How to set Notify CursorAdapter with ListView inside onItemLongClickListener()

165 Views Asked by At

I am trying to remove long pressed item from my ListView, populated by a cursor adapter, item is removed but adapter is not notified.

I have read this:

How to notifyDataSetChange() from onitemclick() method?

and that solution may worked for the the subclass of an ArrayAdapter, but my one extends CursorAdapter,

cant resolve method: notifyDataSetChanged() (while trying to solve using the answer of that question)

Code inside notifyDataSetChanged():

 notesListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


                Log.d(TAG, "onItemLongClick: " + position + " id: 0" + id);

                List<String> taskList = dbhelper.getTaskList(userName);

                Log.d(TAG, "onItemLongClick: " + taskList);

                dbhelper.deleteTask(userName, taskList.get(position));


                taskList.remove(position);

                cursorAdapter.notifyDataSetChanged();


            //    ListAdapter adapter = notesListView.getAdapter();

            //    adapter.notifyDataSetChanged();  can't resolve method

Another thing is every time user long pressed on an item, a List<String> is retrieved from database, and removed it from the position it was pressed.

Where I am doing wrong? I also need a decent way of removing item from database which is selected, thanks in advance!

Edit:

Here are codes of my adapter:

public class MyCursorAdapter extends CursorAdapter {

private LayoutInflater inflater;

public MyCursorAdapter(Context context, Cursor c) {
    super(context, c);

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    View view = inflater.inflate(R.layout.list_item_cursor, parent, false);

    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView titleTextView = (TextView) view.findViewById(R.id.list_title);

    TextView detailsTextView = (TextView) view.findViewById(R.id.list_details);

    String titleString = null;
    String detailsString = null;
    try {
        titleString = cursor.getString(1);
        detailsString = cursor.getString(2);
    } catch (Exception e) {
        Log.d("TAG", "Exception on: " + e.toString());
    }

    titleTextView.setText(titleString);
    detailsTextView.setText(detailsString);


}

}

0

There are 0 best solutions below