简体   繁体   中英

Delete item from listview

I have listview which contains textview and buttons. When i delete listview item and i try to scroll down, i get exception on this:

BuildQueue eile = countryList.get(position);

Exception:

02-08 19:11:04.279: E/AndroidRuntime(10509): java.lang.IndexOutOfBoundsException: Invalid index 15, size is 15

Seems i do not updating something when i delete item from listview. I think i have problem with ViewHolder, but i do not know what kind of...

My ArrayAdapter code:

public class MyCustomAdapter extends ArrayAdapter<BuildQueue> {

        private ArrayList<BuildQueue> countryList;

        public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<BuildQueue> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<BuildQueue>();
            this.countryList.addAll(countryList);
        }

        private class ViewHolder {
            TextView code;
            TextView field;
            Button del;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.queue_buildings, null);

                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.field = (TextView) convertView.findViewById(R.id.field_text);
                holder.del = (Button) convertView.findViewById(R.id.del_button);
                convertView.setTag(holder);

                holder.del.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        Button del_button = (Button) v;
                        BuildQueue building = (BuildQueue) del_button.getTag();
                        countryList.remove(building);
                        dataAdapter.notifyDataSetChanged();
                    }
                });
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            BuildQueue eile = countryList.get(position);
            holder.code.setText(" ( Level: " + eile.getOld_level() + " to "+eile.getNew_level()+")");

            holder.field.setText(eile.getNameSort());
            holder.field.setTag(eile);

            holder.del.setText("Delete");
            holder.del.setTag(eile);

            return convertView;

        }
    }

You are using a two arrays in your Adapter, but only changing one of them.

Every Adapter uses getCount() to determine how many row should be drawn. ArrayAdapter's getCount() simply asks for the size of the array that you pass to the super constructor here: super(context, textViewResourceId, countryList); . But you are also using a second, local array and when you delete a value from this countryList getCount() has no idea this happened which results in getView() throwing an IndexOutOfBoundsException...

Either extend BaseAdapter, or use ArrrayAdapter's methods like getItem() , add() , and remove() and remove your local data set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM