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) 
12
  • from where position comes ? Commented Nov 26, 2012 at 15:47
  • position seems to be wrong the second time around. Is it updated properly? Commented Nov 26, 2012 at 15:48
  • position is here; @Override public View getView(final int position, View convertView, ViewGroup parent) Commented Nov 26, 2012 at 15:50
  • onclick method is in getView method.i'm using getView's position value Commented Nov 26, 2012 at 15:52
  • How is data declared? If you have a List<Integer> descendant, it utterly confuses the List.remove method. Commented Nov 26, 2012 at 15:59

1 Answer 1

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(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

In my case that was not enough, getItem has to be overriden as well, an example here: stackoverflow.com/a/17045297/891479
Override and return filteredData.size(), main thing to do
Thanks, that was it. Lesson learned, when we override the filter, then we override the getFilter() AND getCount() method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.