0

我的代码有问题。我想listview用这段代码删除所有自定义项:

public void delete_all()
{
    int count = getCount();
    if(count>0)
      {
         for (int i = 0; i < count; i++) 
          {
              data.remove(data.get(i));
          }
       notifyDataSetChanged();
    }
}

public Object getItem(int position) 
{
    return position;
}

但结果,只有删除的项目可见,例如:count = 5 item,结果只有 3个可见的项目被删除,2 个项目没有被删除,

data.remove(data.get(i)); 

我也尝试改变,data.remove(i); 结果相同;

该代码的logcat是

> 04-15 13:07:58.340: E/AndroidRuntime(2111): FATAL EXCEPTION: main
04-15 13:07:58.340: E/AndroidRuntime(2111): java.lang.IllegalStateException: Could not execute method of the activity
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.view.View$1.onClick(View.java:3044)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.view.View.performClick(View.java:3511)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.view.View$PerformClick.run(View.java:14105)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.os.Handler.handleCallback(Handler.java:605)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.os.Looper.loop(Looper.java:137)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.app.ActivityThread.main(ActivityThread.java:4456)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at dalvik.system.NativeStart.main(Native Method)
04-15 13:07:58.340: E/AndroidRuntime(2111): Caused by: java.lang.reflect.InvocationTargetException
04-15 13:07:58.340: E/AndroidRuntime(2111):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at android.view.View$1.onClick(View.java:3039)
04-15 13:07:58.340: E/AndroidRuntime(2111):     ... 11 more
04-15 13:07:58.340: E/AndroidRuntime(2111): **Caused by: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2**
04-15 13:07:58.340: E/AndroidRuntime(2111):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at java.util.ArrayList.get(ArrayList.java:304)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at com.droidersuin.imagelistfromurl.LazyAdapter.delete_all(LazyAdapter.java:60)
04-15 13:07:58.340: E/AndroidRuntime(2111):     at com.droidersuin.app.SearchActivity.search(SearchActivity.java:306)
04-15 13:07:58.340: E/AndroidRuntime(2111):     ... 14 more
4

1 回答 1

7

如果您想删除所有 Item 然后只需调用clear()而不是remove(). 像这样

data.clear(); // this will clear your list
yourAdapter.notifyDataSetChanged();

注意:永远不要使用相同的对象大小来删除循环中的项目,这意味着它会改变你的大小,list/arraylist所以你在每个迭代器上都会得到不可预测的大小。您可以在另一个列表中使用商店,然后使用

List<String> yourSelectData; store your data in this at iterate time then remove after loop complete


data.removeAll(yourselectData); // using this you can remove collection of element from list
于 2013-04-15T06:16:27.843 回答