我创建了一个包含删除按钮的项目的适配器。当用户单击按钮时,应删除该项目。 如果我使用此代码:
list.remove(item);
notifyDataSetChanged();
成功了。但是当我添加动画时,应用程序崩溃时出现以下异常:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
at android.widget.ListView.dispatchDrawWithBounce(ListView.java:3324)
at android.widget.ListView.dispatchDraw(ListView.java:3073)
at android.view.View.draw(View.java:6936)
at android.widget.AbsListView.draw(AbsListView.java:3005)
at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6936)
at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:870)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6936)
at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6936)
at android.widget.FrameLayout.draw(FrameLayout.java:357)
at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6936)
at android.widget.FrameLayout.draw(FrameLayout.java:357)
at
com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1917)
at android.view.ViewRoot.draw(ViewRoot.java:1530)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1266)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)
适配器来源:
public class SearchHistoryAdapter extends BaseAdapter {
private List<SearchItem> list;
public SearchHistoryAdapter( List<SearchItem> list ){
this.list = list;
}
@Override
public int getCount(){
return list.size();
}
@Override
public Object getItem( final int i ){
return list.get( i );
}
@Override
public long getItemId( final int i ){
return 0;
}
@Override
public View getView( final int i, final View convertView, final ViewGroup parent ){
SearchItem item = list.get( i );
View row = convertView;
final ContentHolder holder;
if( convertView == null ){
holder = new ContentHolder();
row = LayoutInflater.from( parent.getContext() ).inflate( R.layout.item_search_history, null );
holder.remove = (ImageButton)row.findViewById( R.id.ib_search_history_remove );
…
holder.remove.setFocusable( false );
holder.remove.setOnClickListener( removeClick );
row.setTag( holder );
} else {
holder = (ContentHolder)row.getTag();
}
holder.remove.setTag( item );
…
return row;
}
private View.OnClickListener removeClick = new View.OnClickListener() {
@Override
public void onClick( final View view ){
final SearchItem item = (SearchItem)view.getTag();
final View itemView = (View)view.getParent().getParent();
Animation animation = AnimationUtils.loadAnimation( view.getContext(), R.anim.attributes_disappear );
animation.setAnimationListener( new Animation.AnimationListener() {
@Override
public void onAnimationStart( final Animation animation ){ }
@Override
public void onAnimationEnd( final Animation animation ){
list.remove( item );
notifyDataSetChanged();
}
@Override
public void onAnimationRepeat( final Animation animation ){ }
} );
itemView.startAnimation( animation );
}
};
static class ContentHolder {
TextView name;
TextView category;
TextView description;
TextView number;
ImageButton remove;
}
public static class SearchItem {
...
}
}
ListView有页眉和页脚视图。
答案 0 :(得分:0)
尝试替换此行:
holder.remove.setTag( item );
有了这个:
holder.remove.setTag(i);
你也把这一行:
holder.remove.setOnClickListener( removeClick ); outside if else code
答案 1 :(得分:0)
你需要更新计数以及notifydatasetchange调用只有getview方法,所以我建议无论如何也尝试更新计数,你得到这个例外,因为没有调用getCount方法