Error while removing Listview item - Android

Asked

Viewed 38 times

1

Well, I am working on a system that puts some items in a list, in sequence, using a Dialogfragment with the list of added items I would give the option to remove these items, the problem is there. I declared in Onitemclicklistener that the item should be removed when clicked, the item is removed from the list normally, but soon after it pops an Indexoutofboundsexception, it seems a silly mistake but I am not locating where the problem is. follow my codes:

DIALOGFRAGMENT

public static class AlertDialogMyItems extends DialogFragment{


        MyAdapter adapter;
        ListView lvMyItems;
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            Bundle args = getArguments();
            final List<Item>myItens = (ArrayList<Item>)args.getSerializable("TAG");            
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View view = inflater.inflate(R.layout.my_layout,null);
            lvMyItems = view.findViewById(R.id.lvMyItems);          
            adapter = new MyAdapter(getContext(),myItens);          
            lvMyItems.setAdapter(adapter);          
            final TextView txtHeader = new TextView(getContext());          
            txtHeader.setBackgroundColor(Color.GRAY);
            txtHeader.setTextColor(Color.WHITE);
            txtHeader.setText(R.string.header_items);
            txtHeader.setPadding(8,8,0,8);
            txtHeader.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            lvMyItems.addHeaderView(txtHeader);

            lvMyItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                    Item item = (Item)adapterView.getItemAtPosition(position);
                    adapter.removeItem(item);
                }
            });

            builder .setTitle(getText(R.string.items_tittle))
                    .setView(view)
                    .setPositiveButton("PAY", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int id) {

                        }
                    })
                    .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            ;

            return builder.create();
        }


    }

ADAPTER

public class MyAdapter extends BaseAdapter{

     ...

        public void removeItem(Item item){

            for(Item i:items){

                if(i.getIdItem()==item.getIdItem())         
                   items.remove(i); 
            }
        }

    }

ERROR

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
  at java.util.ArrayList.get(ArrayList.java:437)
  at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
  at android.widget.ListView.dispatchDraw(ListView.java:3503)
  at android.view.View.draw(View.java:19195)
  at android.widget.AbsListView.draw(AbsListView.java:4310)
  at android.view.View.updateDisplayListIfDirty(View.java:18142)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4220)
  at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4200)
  at android.view.View.updateDisplayListIfDirty(View.java:18101)
  at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:669)
  at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:675)
  at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:783)
  at android.view.ViewRootImpl.draw(ViewRootImpl.java:2992)
  at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2806)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2359)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
  at android.view.Choreographer.doCallbacks(Choreographer.java:723)
  at android.view.Choreographer.doFrame(Choreographer.java:658)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
  at android.os.Handler.handleCallback(Handler.java:790)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:164)
  at android.app.ActivityThread.main(ActivityThread.java:6494)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  • I had forgotten to use Adapter.notifyDataSetChanged() after removal, problem solved!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.