4

I'm trying to remove an item from listview by onclick action. But i'm getting arrayindexoutofbound exception. I deleted item from 11 size array, but it still trying to get 11. item.

        public void onClick(View v) {
            data.remove(getItemIndexByID(filteredData.get(position).getID()));
            filteredData.remove(position);      
            notifyDataSetChanged();

            flipMenu(lastHolder, 1);
            showPopup("Item Deleted"); 


        }

LOG:

    11-26 17:16:32.159: E/AndroidRuntime(26991): FATAL EXCEPTION: main
11-26 17:16:32.159: E/AndroidRuntime(26991): java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
11-26 17:16:32.159: E/AndroidRuntime(26991):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at java.util.ArrayList.get(ArrayList.java:304)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at com.innovationbox.passlocker.adapters.ListViewAdapter.getView(ListViewAdapter.java:100)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.widget.AbsListView.obtainView(AbsListView.java:2143)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.widget.ListView.fillDown(ListView.java:674)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.widget.ListView.fillGap(ListView.java:638)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4087)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.view.Choreographer.doFrame(Choreographer.java:531)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.os.Handler.handleCallback(Handler.java:725)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.os.Looper.loop(Looper.java:137)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at android.app.ActivityThread.main(ActivityThread.java:5039)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at java.lang.reflect.Method.invokeNative(Native Method)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at java.lang.reflect.Method.invoke(Method.java:511)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-26 17:16:32.159: E/AndroidRuntime(26991):    at dalvik.system.NativeStart.main(Native Method)
ChuKoNu
  • 477
  • 4
  • 20

1 Answers1

16

I noticed that ArrayAdapter's getcount method always returns same value, i overrided getCount() method like this, it works.

@Override
public int getCount() {
    return filteredData.size();
}
ChuKoNu
  • 477
  • 4
  • 20
  • 5
    In my case that was not enough, getItem has to be overriden as well, an example here: http://stackoverflow.com/a/17045297/891479 – L. G. Jun 11 '13 at 13:28
  • Override and return filteredData.size(), main thing to do – Nabin Aug 18 '14 at 08:08
  • Thanks, that was it. Lesson learned, when we override the filter, then we override the getFilter() AND getCount() method. – Shoaib Apr 17 '16 at 10:51