HessianMad
HessianMad

Reputation: 567

Delete an element from a ListView in Android

I'm trying to delete an element of my ListView when a element is clicked by the user, but when the user click it, my application crashes and I don't know why.

Here is the code of deleting the element when it's selected:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    tasques.remove(position);
    notifyDataSetChanged();

}

Here is the onCreate of my mainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tasquesArrayList = new ArrayList<>();

    listView = (ListView) findViewById(R.id.listview);

    ...

    adapter = new tascaListViewAdapter(this,tasquesArrayList);

    ...

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(adapter);
    adapter.notifyDataSetChanged();

}

Here's the error:

Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131492951, class android.widget.ListView) with Adapter(class edu.lasalle.pprog2.ac2.adapter.tascaListViewAdapter)]
                        at android.widget.ListView.layoutChildren(ListView.java:1618)
                        at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:3731)
                        at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:997)
                        at android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:3744)
                        at android.view.ViewRootImpl.ensureTouchMode(ViewRootImpl.java:3728)
                        at android.view.ViewRootImpl$EarlyPostImeInputStage.processPointerEvent(ViewRootImpl.java:4244)
                        at android.view.ViewRootImpl$EarlyPostImeInputStage.onProcess(ViewRootImpl.java:4212)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)
                        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220)
                        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
                        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6349)
                        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                        at android.os.MessageQueue.nativePollOnce(Native Method)
                        at android.os.MessageQueue.next(MessageQueue.java:323)
                        at android.os.Looper.loop(Looper.java:136)
                        at android.app.ActivityThread.main(ActivityThread.java:6119)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: edu.lasalle.pprog2.ac2, PID: 3629
                  java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131492951, class android.widget.ListView) with Adapter(class edu.lasalle.pprog2.ac2.adapter.tascaListViewAdapter)]
                      at android.widget.ListView.layoutChildren(ListView.java:1618)
                      at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:3731)
                      at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:997)
                      at android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:3744)
                      at android.view.ViewRootImpl.ensureTouchMode(ViewRootImpl.java:3728)
                      at android.view.ViewRootImpl$EarlyPostImeInputStage.processPointerEvent(ViewRootImpl.java:4244)
                      at android.view.ViewRootImpl$EarlyPostImeInputStage.onProcess(ViewRootImpl.java:4212)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)
                      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220)
                      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
                      at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6349)
                      at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                      at android.os.MessageQueue.nativePollOnce(Native Method)
                      at android.os.MessageQueue.next(MessageQueue.java:323)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Application terminated.

Thanks in advance!

Upvotes: 0

Views: 158

Answers (2)

HessianMad
HessianMad

Reputation: 567

Fixed! I had another adapter = new tascaListViewAdapter(this,tasquesArrayList); in another part of my code...

Upvotes: 1

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this code: notify the adapter using adapter.notifyDataSetChanged(); also invalidate the previous data.

 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    tasques.remove(position);
    adapter.notifyDataSetChanged();
    adapter.notifyDataSetInvalidated();
}

Upvotes: 1

Related Questions