2

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 can not understand.

Please help me find my mistake.

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

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

can you please share the RaisePoBean class code here first ?
then remaining everything is ok?
i will let check and tell u result
yea everything is fine just paste those 2 functions equals and hashcode.
1

Try like this,

Create interface like below and implement in the activity,

interface SelectedListner{
    void selectItem(String position);
}

Create a method deleteItem() in Adapter,inside this method

raisePoBeanArrayList.remove(raisePoBean);
adapter.notifyDataSetChanged(); 

Get the instance of listener in adapter,In Adapter

SelectedListener listener;

Inside the Constructor,

listener = (SelectedListener)context;

Get the position of the selected items while checking the item in onclickListener. Now call the method selectItem using the instance listener. Like below

listener.selectItem (position);

In Activity, in override method just call the deleteItem() in adapter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.