Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android clear in costom arrayadapter java.lang.UnsupportedOperationException

i want to use clear() in my CostomArrayAdapter, but i get always this error:

09-08 18:15:01.960: E/AndroidRuntime(27192): FATAL EXCEPTION: main
09-08 18:15:01.960: E/AndroidRuntime(27192): java.lang.UnsupportedOperationException
09-08 18:15:01.960: E/AndroidRuntime(27192):    at java.util.AbstractList.remove(AbstractList.java:638)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at java.util.AbstractList.removeRange(AbstractList.java:658)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at java.util.AbstractList.clear(AbstractList.java:466)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.ArrayAdapter.clear(ArrayAdapter.java:258)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at de.fterhorst.pictoriusvertretungsplan.SimpleArrayadapterDay.dayempty(SimpleArrayadapterDay.java:235)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at de.fterhorst.pictoriusvertretungsplan.SimpleArrayadapterDay.getView(SimpleArrayadapterDay.java:177)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.AbsListView.obtainView(AbsListView.java:2177)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.ListView.makeAndAddView(ListView.java:1840)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.ListView.fillDown(ListView.java:675)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.ListView.fillFromTop(ListView.java:736)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.ListView.layoutChildren(ListView.java:1655)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.AbsListView.onLayout(AbsListView.java:2012)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.Choreographer.doFrame(Choreographer.java:532)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.os.Handler.handleCallback(Handler.java:730)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.os.Looper.loop(Looper.java:137)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at android.app.ActivityThread.main(ActivityThread.java:5103)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at java.lang.reflect.Method.invokeNative(Native Method)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at java.lang.reflect.Method.invoke(Method.java:525)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-08 18:15:01.960: E/AndroidRuntime(27192):    at dalvik.system.NativeStart.main(Native Method)

when i make it so:

this.clear(); this.notifyDataSetChanged();

on my ArrayAdapter:

public class CostomArrayAdapter extends ArrayAdapter<String>

can i use clear() on a arrayadapter, i hope someone can help me.

like image 760
johan fra Avatar asked Nov 30 '22 20:11

johan fra


1 Answers

The error you are getting isn't from calling clear() on the ArrayAdapter. You are using a List implementation (possibly the default) which doesn't implement a clear() method and therefore the parent class', ie. AbstractList implementation is used, which throws UnsupportedOperationException.

If you pass an array to the constructor

public ArrayAdapter(Context context, int resource, T[] objects) {
    init(context, resource, 0, Arrays.asList(objects));
}

it calls Arrays.asList() which returns a List which you cannot add() to, remove() from or clear().

Use this constructor

public ArrayAdapter(Context context, int resource, List<T> objects) {
    init(context, resource, 0, objects);
}

by passing a LinkedList or ArrayList containing your objects.

like image 72
Sotirios Delimanolis Avatar answered Dec 06 '22 06:12

Sotirios Delimanolis