尝试从ListView中删除项目始终会删除第一个项目

时间:2016-10-12 13:24:32

标签: android listview

我有一个自定义适配器和一个列表,我可以在按钮点击时动态添加项目。每个列表行都有一个删除按钮(或图像)。这是我的自定义适配器类:

package com.ameer.easycooking;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by ameer on 12/10/2016.
 */
public class IngredientAdapter extends ArrayAdapter<String> {
    private List<String> ingredientList;
    private Context context;

    public IngredientAdapter(List<String> ingredientList, Context context) {
        super(context, R.layout.ingredient_list_item, ingredientList);
        this.ingredientList = ingredientList;
        this.context = context;
    }

    public static class IngredientHolder {
        private TextView name;
        private ImageView remove;

        public IngredientHolder(TextView name, ImageView remove) {
            this.name = name;
            this.remove = remove;
        }

        public TextView getName() {
            return name;
        }

        public ImageView getRemove() {
            return remove;
        }
    }

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

        IngredientHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.ingredient_list_item, null);

            holder = new IngredientHolder((TextView) v.findViewById(R.id.ingredientName), (ImageView) v.findViewById(R.id.remove));

            holder.getRemove().setTag(position);

            holder.getRemove().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = (int) v.getTag();
                    Toast.makeText(context, ingredientList.get(index) + " removed", Toast.LENGTH_SHORT).show();
                    ingredientList.remove(index);
                    notifyDataSetChanged();
                }
            });

            v.setTag(holder);
        } else {
            holder = (IngredientHolder) v.getTag();
        }

        String ingredient = ingredientList.get(position);
        holder.name.setText(ingredient);

        return v;
    }
}

我的问题是,无论我添加多少项或我选择删除哪一项,它总是删除ListView中的第一行而不是单击的那一行。

3 个答案:

答案 0 :(得分:1)

您只会点击convertView == null一次,这就是您遇到此问题的原因。您应在holder.getRemove().setTag(position);语句后设置if/else

答案 1 :(得分:1)

您不应在if语句中设置Click侦听器,因为您无法知道何时将使用转换后的视图,并且单击的位置可能是错误的。因此,只需将click侦听器移出if语句,然后删除holder.getRemove().setTag(position)语句:

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

    IngredientHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.ingredient_list_item, null);
        holder = new IngredientHolder((TextView) v.findViewById(R.id.ingredientName), (ImageView) v.findViewById(R.id.remove));
        v.setTag(holder);
    } else {
        holder = (IngredientHolder) v.getTag();
    }

    String ingredient = ingredientList.get(position);
    holder.name.setText(ingredient);

    holder.getRemove().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = (int) v.getTag();
            Toast.makeText(context, ingredientList.get(index) + " removed", Toast.LENGTH_SHORT).show();
            ingredientList.remove(index);
            notifyDataSetChanged();
        }
    });

    return v;
}

编辑:或者正如Blackbelt所说,你可以简单地将holder.getRemove().setTag(position);语句移出if块。

答案 2 :(得分:0)

更好的方法是在getView()final中传递位置 public View getView(final int position,View convertView,ViewGroup parent) 并在点击监听器中使用该位置。