Deleting items from a ListView using a custom BaseAdapter

后端 未结 3 1561
日久生厌
日久生厌 2021-02-06 05:17

I am using a customised BaseAdapter to display items on a ListView. The items are just strings held in an ArrayList.

The list items have a delete button on them (big red

3条回答
  •  借酒劲吻你
    2021-02-06 05:47

    You have to set the position each time. Your implementation only sets the position on the creation of the view. However when the view is recycled (when convertView is not null), the position will not be set to the correct value.

        public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.language_link_row, null);
            holder = new ViewHolder();
            holder.lang = (TextView)convertView.findViewById(R.id.language_link_text);
    
            final ImageView deleteButton = (ImageView) 
                    convertView.findViewById(R.id.language_link_cross_delete);
            deleteButton.setOnClickListener(this);
    
            convertView.setTag(holder);
            deleteButton.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
    
        holder.lang.setText(mLanguages.get(position));
        holder.position = position;
        return convertView;
    }
    

提交回复
热议问题