0

I try to delete a clicked item from a listview but I have a Runtime error. I have my items in R.array.sections file.

 public class ListCategorieActivity extends Activity{
public static String RISULTATO = "RISULTATO";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] arrayList = getResources().getStringArray(R.array.sections);
    setContentView(R.layout.activity_list_categorie);
    ListView listview = (ListView) findViewById(R.id.listView1);

    final ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arrayList );


    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> a, View v,int position, long id)
        {
            Bundle bundle = new Bundle();
            Intent mIntent = new Intent();
            String[] some_array = getResources().getStringArray(R.array.sections);
            bundle.putString(RISULTATO,some_array[position]);
            mIntent.putExtras(bundle);
            setResult(RESULT_OK, mIntent);

            //try to remove clicked item
            String toRemove = adapter.getItem(position);
            adapter.remove(toRemove);
            finish();
        }
    });
}

/*public void onItemClick(AdapterView<?> l, View v, int position, long id) {
    Bundle bundle = new Bundle();
    Intent mIntent = new Intent();
    String[] some_array = getResources().getStringArray(R.array.sections);
    bundle.putString(RISULTATO,some_array[position]);
    mIntent.putExtras(bundle);
    setResult(RESULT_OK, mIntent);
    finish();
}*/

}

where is my error?

Logcat:

08-30 18:00:12.403  20111-20111/com.example.utente.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
        at java.util.AbstractList.remove(AbstractList.java:638)
        at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
        at java.util.AbstractCollection.remove(AbstractCollection.java:229)
        at android.widget.ArrayAdapter.remove(ArrayAdapter.java:244)
        at com.example.utente.myapplication.ListCategorieActivity$1.onItemClick(ListCategorieActivity.java:43)
        at android.widget.AdapterView.performItemClick(AdapterView.java:301)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1287)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3080)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:4176)
        at android.view.View.dispatchTouchEvent(View.java:7380)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
        at android.view.View.dispatchPointerEvent(View.java:7565)
        at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:125)
        at android.os.Looper.loop(Looper.java:124)
        at android.app.ActivityThread.main(ActivityThread.java:4947)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)
  • please paste logcat to see. – Mohammad Tauqir Aug 30 '15 at 16:40
  • Take a look at this, you need to construct a new List<> before setting the objects. http://stackoverflow.com/questions/2965747/why-i-get-unsupportedoperationexception-when-trying-to-remove-from-the-list – Ahmed Aug 30 '15 at 18:43

3 Answers3

1

Write

adapter.notifyDataSetChanged(); 

after

adapter.remove(toRemove);
0

Firstly remove it from arraylist and then update adapter

arraylist.remove(postion) //position of item want to delete

then notify adapter or reload as works

Androider
  • 3,833
  • 2
  • 14
  • 24
0

Try this;

  • convert your static Array to ArrayList e.g. ArrayList newList = ArrayList(Arrays.asList(arrayList));
  • remove element from your array as below newList.remove(position);
Want2bExpert
  • 527
  • 4
  • 11
  • I try but the item is not deleted. I think there is some of wrong here: `@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final String[] arrayList = getResources().getStringArray(R.array.sections); final ArrayList newList =new ArrayList(Arrays.asList(arrayList)); setContentView(R.layout.activity_list_categorie); ListView listview = (ListView) findViewById(R.id.listView1); ` – Daniel JD JD Aug 30 '15 at 18:10
  • Is there any reason why you make ArrayList final? – Want2bExpert Aug 30 '15 at 18:20