IndexOutOfBoundsException when deleting listView item with delay

I created an adapter with items containing delete buttons. When the user clicks the button, the item should be removed. If I use this code:

list.remove(item);
notifyDataSetChanged();

      

he is doing well. But when I added animation, the app 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)

      

Adapter source:

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 has both header and footer.

+2


source to share


2 answers


Try replacing this line:

holder.remove.setTag( item );

      

Wherein:



holder.remove.setTag(i);

      

You will also put this line:

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

      

0


source


you need to update the counter and notifydatasetchange will only call the getview method, so I suggest trying to update the account as well, you get this exception because the getCount method is not called



0


source







All Articles