Krish Krish - 5 months ago 18
Android Question

How to Delete multiple records from listview in android

Hi i am new for android and in my app i am using list-view and i am showing check box on list-view each row

Here my main problem is when i select multiple records from list-view all records are not deleting

My arraylist size is "4" and in my below "checking value" is coming as "true" in my log cat but why they are not deleting i am not understand

please help what is my mistack

code:-



try {

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;
}

Answer

Blockquote

You just need to add those equals and hashcode methods in your class as below:-

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;
}
}

And notify the adapter outside the for loop:-

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();
}
Comments