0

I have problem in my code.I want remove all items in custom listview with this code :

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

but the result, just items visible where deleted , example : there count = 5 item , the result just 3 items visible are deleted, and 2 items in not deleted,

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

i try too change with data.remove(i); i same result;

and logcat of that code is

> 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
Lucifer
  • 29,392
  • 25
  • 90
  • 143
bukanamay
  • 577
  • 5
  • 11
  • 26
  • Take a look at this link..May it will help you...[link1][1] and [link2][2] [1]: http://stackoverflow.com/questions/5497580/how-to-dynamically-remove-items-from-listview-on-a-button-click [2]: http://stackoverflow.com/questions/11112953/android-remove-item-listview – AndiM Apr 15 '13 at 06:20

1 Answers1

7

If you want to remove all Item then just call clear() instead of remove(). Like this

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

Note: Never ever remove item in loop with using same object size as like means it will change you size of you list/arraylist so you getting unpredictable size on each iterator. You can either use store in another list and then use

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
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • Thank's mr.pratik , i have change my code with `if(count>0) { // data.removeAll(); if i change with this, is error, sugesstion avaible data.removeAll(data); notifyDataSetChanged(); }` but still other error in logcat my code :( , – bukanamay Apr 15 '13 at 06:33
  • I think you still doing some mistake don't use the same list object and when you want to remove all just use clear() and for selected item like user select by tap or by checked boxed that you have to stored in another list object and passed to the original list in removeAll() – Pratik Apr 15 '13 at 06:35
  • Hai.... Thank... i other mistake other code... data.clear() is work :) THANKS :) – bukanamay Apr 16 '13 at 05:49