-2

I have a simple question. I want delete an item from ListView without relaunch the activity. Is this possible?

I tried with:

 adapter.remove(adapter.getItem(currentPosition)); // line 188

but the following error occurs:

11-22 17:59:36.520: E/AndroidRuntime(8124): FATAL EXCEPTION: main
11-22 17:59:36.520: E/AndroidRuntime(8124): java.lang.UnsupportedOperationException
11-22 17:59:36.520: E/AndroidRuntime(8124):     at java.util.AbstractList.remove(AbstractList.java:645)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:77)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at java.util.AbstractCollection.remove(AbstractCollection.java:230)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at android.widget.ArrayAdapter.remove(ArrayAdapter.java:248)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at host.activity.ACT_MessaggiRicevuti$3.onClick(ACT_MessaggiRicevuti.java:188)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at android.view.View.performClick(View.java:3110)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at android.view.View$PerformClick.run(View.java:11934)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at android.os.Handler.handleCallback(Handler.java:587)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at android.os.Looper.loop(Looper.java:132)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at android.app.ActivityThread.main(ActivityThread.java:4123)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at java.lang.reflect.Method.invokeNative(Native Method)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at java.lang.reflect.Method.invoke(Method.java:491)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-22 17:59:36.520: E/AndroidRuntime(8124):     at dalvik.system.NativeStart.main(Native Method)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • what are you using to hold the data in the listview adapter? remove it from the datastructure and then from the adapter call notifydatasetchanged() – Hades Nov 22 '12 at 16:52

2 Answers2

1

It depends on the Adapter you use.

If you are using an ArrayAdapter, just remove it, then notify the ListView. Depending on how the listView was set up, you might need to do something a bit more drastic, as was explained at this question.

ListView listview; //Somehow you get a handle for it
ArrayList <Integer> list=new ArrayList<Integer>([0 6 1 2 3]);
ArrayAdapter <Integer> mAdapter=new ArrayAdapter<Integer>(this,list);
listview.setListAdapter(mAdapter);
mAdapter.remove(2); //Removes the number 2
madapter.notifyDataSetChanged();

If you are using some other sort of adapter, you might need to create a new Adapter that has been revised, and set it as above to the listView.

Community
  • 1
  • 1
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
1

you might be setting the items to the listview using an arrayadapter. If you are doing it use arrayadapterObject.remove(the item you need to remove) and call arrayadapterObject.notifyDAtaSetChanged() to refresh the items that are related to your arrayadapter

for example you need to remove the item 0 from your list view and refresh the list view Then simply use arrayadapterObject.remove(0) and next to this call arrayadapterObject.notifyDAtaSetChanged()

G_S
  • 7,068
  • 2
  • 21
  • 51