如何从android中的listview中删除多条记录?

时间:2016-07-02 13:15:31

标签: android listview

嗨,我是Android的新手,在我的应用程序中,我使用list-view,我在列表视图中显示每行的复选框。

这里我的主要问题是当我从列表视图中选择多个记录时,所有记录都没有删除。

我的arraylist大小为“4”,在我的下面“检查值”在我的日志猫中显示为“true”,但为什么它们没有删除我无法理解。

请帮我找错。

代码: -

尝试{

    for (int i = 0; i < raisePoBeanArrayList.size(); i++) {

        System.out.println("checking value ====>" + raisePoBeanArrayList.get(i).isCheck());

        if (raisePoBeanArrayList.get(i).isCheck()) {

            RaisePoBean  raisePoBean = raisePoBeanArrayList.get(i);
            raisePoBeanArrayList.remove(raisePoBean);
            adapter.notifyDataSetChanged();

        }
    }

} catch (ArrayIndexOutOfBoundsException exception) {
    exception.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

RaisePoBean: -

public static final Parcelable.Creator<RaisePoBean> CREATOR = new Parcelable.Creator<RaisePoBean>() {
        public RaisePoBean createFromParcel(Parcel in) {
            return new RaisePoBean(in);
        }

        public RaisePoBean[] newArray(int size) {
            return new RaisePoBean[size];
        }
    };

    /**
     * Create Friend from Parcel object.
     *
     * @param in
     */
    public RaisePoBean(Parcel in) {

        this.srNo = in.readInt();
        this.itemCode = in.readString();
        this.qty = in.readDouble();
        this.itemName = in.readString();
        this.rate = in.readDouble();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeInt(this.srNo);
        dest.writeString(this.itemCode);
        dest.writeDouble(this.qty);
        dest.writeString(this.itemName);
        dest.writeDouble(this.rate);
    }

      public RaisePoBean(int srNo, String itemCode, Double qty, String itemName, Double rate) {
            this.srNo = srNo;
            this.itemCode = itemCode;
            this.qty = qty;
            this.itemName = itemName;
            this.rate = rate;
        }

        public Double getQty() {
            return qty;
        }

        public void setQty(Double qty) {
            this.qty = qty;
        }

        public int getSrNo() {
            return srNo;
        }

        public void setSrNo(int srNo) {
            this.srNo = srNo;
        }

        public String getItemCode() {
            return itemCode;
        }

        public void setItemCode(String itemCode) {
            this.itemCode = itemCode;
        }

        public String getItemName() {
            return itemName;
        }

        public void setItemName(String itemName) {
            this.itemName = itemName;
        }

        public Double getRate() {
            return rate;
        }

        public void setRate(Double rate) {
            this.rate = rate;
        }


        public boolean isCheck() {
            return check;
        }

        public void setCheck(boolean check) {
            this.check = check;
        }

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    RasiePoBean that = (RasiePoBean) o;

    return !(itemCode != null ? !itemCode.equals(that.itemCode) : that.itemCode != null);

}

@Override
public int hashCode() {
    return itemCode != null ? itemCode.hashCode() : 0;
}

2 个答案:

答案 0 :(得分:1)

您只需在类中添加equals和hashcode方法,如下所示: -

public class RasiePoBean {

private int srNo;
private String itemCode;
private Double qty;
private String itemName;
private Double rate;
public RasiePoBean(int srNo, String itemCode, Double qty, String itemName, Double rate) {
    this.srNo = srNo;
    this.itemCode = itemCode;
    this.qty = qty;
    this.itemName = itemName;
    this.rate = rate;
}

public Double getQty() {
    return qty;
}

public void setQty(Double qty) {
    this.qty = qty;
}

public int getSrNo() {
    return srNo;
}

public void setSrNo(int srNo) {
    this.srNo = srNo;
}

public String getItemCode() {
    return itemCode;
}

public void setItemCode(String itemCode) {
    this.itemCode = itemCode;
}

public String getItemName() {
    return itemName;
}

public void setItemName(String itemName) {
    this.itemName = itemName;
}

public Double getRate() {
    return rate;
}

public void setRate(Double rate) {
    this.rate = rate;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    RasiePoBean that = (RasiePoBean) o;

    return !(itemCode != null ? !itemCode.equals(that.itemCode) : that.itemCode != null);

}

@Override
public int hashCode() {
    return itemCode != null ? itemCode.hashCode() : 0;
}
}

并在for循环外通知适配器:

try {
ArrayList<RaisePoBean> templist = new ArrayList(raisePoBeanArrayList);
for (int i = 0; i < templist.size(); i++) {

    System.out.println("checking value ====>" + templist.get(i).isCheck());

    if (templist.get(i).isCheck()) {

        RaisePoBean  raisePoBean = templist.get(i);
        raisePoBeanArrayList.remove(raisePoBean);


    }

}
adapter.notifyDataSetChanged();

} catch (ArrayIndexOutOfBoundsException exception) {
    exception.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

答案 1 :(得分:1)

试试这个,

创建如下所示的界面并在活动中实施,

interface SelectedListner{
    void selectItem(String position);
}

在此方法

中的Adapter中创建方法deleteItem()
raisePoBeanArrayList.remove(raisePoBean);
adapter.notifyDataSetChanged(); 

在适配器中获取侦听器实例,在适配器

SelectedListener listener;

在构造函数中,

listener = (SelectedListener)context;

onclickListener中查看项目时获取所选项目的位置。现在使用实例侦听器调用方法selectItem。如下所示

listener.selectItem (position);

在Activity中,在覆盖方法中只需调用适配器中的deleteItem()