除去具有时滞的ListView项时抛出IndexOutOfBoundsException [英] IndexOutOfBoundsException when removing a listView item with delay

查看:486
本文介绍了除去具有时滞的ListView项时抛出IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的项目包含删除按钮的适配器。当用户点击按钮的项目应该被删除。
如果我用这个code:

I created an adapter with items that contains remove buttons. When the user click the button the item should be removed. If I use this code:

list.remove(item);
notifyDataSetChanged();

它成功。但是,当我添加了动画应用程序崩溃与此异常:

it succeeds. But when I added animation the application crashes with this exception:

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控件同时具有页眉和页脚的看法。

The ListView has both header and footer views.

推荐答案

尝试更换这行:

holder.remove.setTag( item );

这一点:

holder.remove.setTag(i);

您也把这个行:

holder.remove.setOnClickListener( removeClick ); outside if else code

这篇关于除去具有时滞的ListView项时抛出IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆