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;
}

ただし、結果は、削除された場所で表示されるアイテムのみです。例:カウント= 5 アイテムの場合、表示されるアイテムは 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

すべてのアイテムを削除したい場合は、clear()代わりにremove(). このような

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

注:同じオブジェクトサイズを使用してループ内のアイテムを削除しないでください。これは、サイズが変更されるlist/arraylistため、各イテレータで予測できないサイズになることを意味します。store in another list を使用してから使用することができます

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 に答える