从我的 CustomArrayAdapter 中删除一个项目

Remove an item from my CustomArrayAdapter

首先,抱歉我的英语不好,我是法国人。

所以,我有一个 CustomArrayAdapter (ReservationAdapter),我会从这个 class 中删除一个项目。我尝试了很多东西,但都没有用。

public class ReservationAdapter extends ArrayAdapter<Reservation> {
public ReservationAdapter(Context context, ArrayList<Reservation> reservations) {
    super(context, 0, reservations);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Reservation reservation = getItem(position);

    // Check if an existing view is being reused, otherwise inflate the view
    ReservationHolder holder;
    if (convertView == null) {
        holder = new ReservationHolder();

        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.reservation_fragment, parent, false);

        holder.date = (EditText) convertView.findViewById(R.id.dateReserv);
        holder.imgDelete = (ImageButton) convertView.findViewById(R.id.less);

        convertView.setTag(holder);
    } else {
        holder = (ReservationHolder) convertView.getTag();
    }

    // Populate the data into the template view using the data object
    holder.date.setText(reservation.date);
    holder.imgDelete.setOnClickListener(deleteReservListener);

    // Return the completed view to render on screen
    return convertView;
}

private View.OnClickListener deleteReservListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Where I try to remove an item
        notifyDataSetChanged();
    }
};

public static class ReservationHolder {
    private EditText date;
    private ImageButton imgDelete;
}

您必须从 reservations 中删除项目,然后调用 notifyDataChanged()

首先你必须在你的ListView上实现一个OnItemClickListener(当然在你的适配器之外):

        AdapterView.OnItemClickListener itemCLickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            reservations.remove(i);
            ((ReservationAdapter)listView.getAdapter()).notifyDataChanged();
        }
    }

瞧瞧

有效,谢谢! 同时,我仍在寻找解决方案,并找到了这个:

holder.date.setText(reservation.date);
    holder.imgDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            remove(reservation);
            notifyDataSetChanged();
        }
    });

我只是将 OnClickListener 放入 GetView 方法中。 再次感谢 ! :)

在您的 ReservationHolder 中添加 position vatiable,并构造为:

public ReservationHolder(int position)
{
    this.position = position;
}

public int getCurrentPosition()
{
    return position;
}

在 getView() 中:ReservationHolder holder = new ReservationHolder(position);

改变你的方法:

private View.OnClickListener deleteReservListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
    //Where I try to remove an item
    notifyDataSetChanged();
}
};

至:

private View.OnClickListener deleteReservListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
    //Where I try to remove an item

    ReservationHolder holder = (ReservationHolder)v.getTag();
    reservations.remove(holder.getCurrentPosition());

    notifyDataSetChanged();
}
};