2012-11-26 47 views
4

我試圖通過onclick操作從列表視圖中刪除項目。但我得到了arrayindexoutofbound異常。我從11大小的數組中刪除了項目,但它仍然試圖獲得11. item。ArrayAdapter IndexOutOfBoundsException

 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) 
+0

從哪裏'position'來? – triclosan

+0

位置似乎是錯誤的第二次。它是否正確更新? – keyser

+0

位置在這裏; \t @Override \t public View getView(final int position,View convertView,ViewGroup parent) – ChuKoNu

回答

13

我注意到ArrayAdapter的getCount將方法總是返回相同的值,我overrided getCount將()這樣的方法,它的工作原理。

@Override 
public int getCount() { 
    return filteredData.size(); 
} 
+4

在我的情況下,這還不夠,getItem也必須重寫,這裏的一個例子:http://stackoverflow.com/a/17045297/891479 –

+0

覆蓋並返回filteredData.size(),主要做的事 – Nabin

+0

解決了我的生活!謝謝。 – Mohammad