4

我想在我的中使用 clear() CostomArrayAdapter,但我总是得到这个错误:

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)

当我这样做时:

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

在我的ArrayAdapter

public class CostomArrayAdapter extends ArrayAdapter<String>

我可以在 arrayadapter 上使用 clear(),我希望有人可以帮助我。

4

4 回答 4

15

您得到的错误不是来自调用clear(). ArrayAdapter您正在使用的List实现(可能是默认值)没有实现clear()方法,因此没有实现父类,即。AbstractList使用实现,它抛出UnsupportedOperationException.

如果将数组传递给构造函数

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

它调用Arrays.asList()which 返回一个List你不能add()to、remove()from 或clear().

使用这个构造函数

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

通过传递LinkedListArrayList包含您的对象。

于 2013-09-08T16:21:54.820 回答
3

如果一个数组被用来初始化和ArrayAdaptor,则该数组被转换成一个AbstractList。AbstractList 不支持remove()clear()

您可以使用List公开 clear.remove 的 a ,如ArrayList, 而不是要初始化的数组。

编辑:按照建议说清楚,它并没有真正转换为AbstractList. 它转换为一个特殊 ArrayList的扩展AbstractList并且没有清除/删除方法。

于 2013-09-08T16:21:44.787 回答
1

在 Kotlin 中, 的结果 toList()也不会响应clear()函数。

您应该toMutableList()在设置您ArrayAdaptor的另一个集合或文字时使用。

于 2020-07-20T21:06:31.327 回答
0

除了什么Sotirios Delimanolisrocketboy说,我想你应该在你的自定义adatper的引用上触发清除操作,例如,

ArrayAdapter<String> adapter = (ArrayAdapter<String>) Listview.getAdapter();

if(adapter!= null)
{
    adapter.clear();
    adapter.notifyDataSetChanged();
}
于 2013-09-08T16:27:54.473 回答