-1

Please look at Case 300:

I am trying to edit a contact so I'm passing the old contact and the new updated one to delete and add correspondingly
I just end up adding the objects but never deleting

old is an object I receive from a bundle from another activity

Why am I not able to match and delete the object?
Equals and Hash have been generated.

Contact has bitmap and string fields

public class Contact implements Parcelable {
    String first, last, company, email, phone, URL, address, nickname, facebook_url, twitter_url, skype, youtube;
    String birthday;
    Bitmap photo;


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

        Contact contact = (Contact) o;

        if (first != null ? !first.equals(contact.first) : contact.first != null) return false;
        if (last != null ? !last.equals(contact.last) : contact.last != null) return false;
        if (company != null ? !company.equals(contact.company) : contact.company != null)
            return false;
        if (email != null ? !email.equals(contact.email) : contact.email != null) return false;
        if (phone != null ? !phone.equals(contact.phone) : contact.phone != null) return false;
        if (URL != null ? !URL.equals(contact.URL) : contact.URL != null) return false;
        if (address != null ? !address.equals(contact.address) : contact.address != null)
            return false;
        if (nickname != null ? !nickname.equals(contact.nickname) : contact.nickname != null)
            return false;
        if (facebook_url != null ? !facebook_url.equals(contact.facebook_url) : contact.facebook_url != null)
            return false;
        if (twitter_url != null ? !twitter_url.equals(contact.twitter_url) : contact.twitter_url != null)
            return false;
        if (skype != null ? !skype.equals(contact.skype) : contact.skype != null) return false;
        if (youtube != null ? !youtube.equals(contact.youtube) : contact.youtube != null)
            return false;
        if (birthday != null ? !birthday.equals(contact.birthday) : contact.birthday != null)
            return false;
        return photo != null ? photo.equals(contact.photo) : contact.photo == null;

    }

    @Override
    public int hashCode() {
        int result = first != null ? first.hashCode() : 0;
        result = 31 * result + (last != null ? last.hashCode() : 0);
        result = 31 * result + (company != null ? company.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (phone != null ? phone.hashCode() : 0);
        result = 31 * result + (URL != null ? URL.hashCode() : 0);
        result = 31 * result + (address != null ? address.hashCode() : 0);
        result = 31 * result + (nickname != null ? nickname.hashCode() : 0);
        result = 31 * result + (facebook_url != null ? facebook_url.hashCode() : 0);
        result = 31 * result + (twitter_url != null ? twitter_url.hashCode() : 0);
        result = 31 * result + (skype != null ? skype.hashCode() : 0);
        result = 31 * result + (youtube != null ? youtube.hashCode() : 0);
        result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
        result = 31 * result + (photo != null ? photo.hashCode() : 0);
        return result;
    }

    public Contact(Bitmap photo, String first, String last, String company, String email, String phone, String URL, String address, String nickname, String facebook_url, String twitter_url, String skype, String youtube, String birthday) {
        this.photo = photo;
        this.first = first;
        this.last = last;
        this.company = company;
        this.email = email;
        this.phone = phone;

        this.URL = URL;
        this.address = address;
        this.nickname = nickname;
        this.facebook_url = facebook_url;
        this.twitter_url = twitter_url;
        this.skype = skype;
        this.youtube = youtube;
        this.birthday = birthday;
    }

    protected Contact(Parcel in) {
        photo = in.readParcelable(Bitmap.class.getClassLoader());
        first = in.readString();
        last = in.readString();
        company = in.readString();
        email = in.readString();
        phone = in.readString();
        URL = in.readString();
        address = in.readString();
        nickname = in.readString();
        facebook_url = in.readString();
        twitter_url = in.readString();
        skype = in.readString();
        youtube = in.readString();
        birthday = in.readString();


    }

    public static final Creator<Contact> CREATOR = new Creator<Contact>() {
        @Override
        public Contact createFromParcel(Parcel in) {
            return new Contact(in);
        }

        @Override
        public Contact[] newArray(int size) {
            return new Contact[size];
        }
    };

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(photo, flags);
        dest.writeString(first);
        dest.writeString(last);


        dest.writeString(company);
        dest.writeString(email);
        dest.writeString(phone);
        dest.writeString(URL);
        dest.writeString(address);
        dest.writeString(nickname);
        dest.writeString(facebook_url);
        dest.writeString(twitter_url);
        dest.writeString(skype);
        dest.writeString(youtube);
        dest.writeString(birthday);

    }


    @Override
    public String toString() {
        return "Contact{" +
                "first='" + first + '\'' +
                ", last='" + last + '\'' +
                ", company='" + company + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", URL='" + URL + '\'' +
                ", address='" + address + '\'' +
                ", nickname='" + nickname + '\'' +
                ", facebook_url='" + facebook_url + '\'' +
                ", twitter_url='" + twitter_url + '\'' +
                ", skype='" + skype + '\'' +
                ", youtube='" + youtube + '\'' +
                ", birthday=" + birthday +
                ", photo=" + photo +
                '}';
    }
}

public class ContactsHome extends AppCompatActivity {

    ArrayList<Contact> contactArrayList = new ArrayList<>();

    public static String ACTION = "";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 200:
                if (data.getExtras() != null) {
                    Contact c = (Contact) data.getExtras().get("contactClass");
                    contactArrayList.add(c);
                }
                break;
            case 300:
                Bundle extras = data.getExtras();
                **Contact old =(Contact) extras.getParcelable("old");
                Contact nu = extras.getParcelable("new");

                contactArrayList.add(nu);

               for(int i=0;i<contactArrayList.size();i++)
                   if(contactArrayList.get(i).equals(old))
                       contactArrayList.remove(i);**
                break;
            case 400:
                Contact delete = (Contact) data.getExtras().get("contactDelete");
                    contactArrayList.remove(delete);
                break;

        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts_home);

        findViewById(R.id.buttonCreateNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ContactsHome.this, CreateNewContact.class);
                startActivityForResult(intent, 200);
            }
        });
        findViewById(R.id.buttonDisplayContact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ACTION = "DISPLAY";
                Intent intent = new Intent(ContactsHome.this, DisplayContacts.class);
                intent.putExtra("contactList", contactArrayList);
                startActivity(intent);
            }
        });
        findViewById(R.id.buttonDeleteContact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ACTION = "DELETE";
                Intent intent = new Intent(ContactsHome.this, DisplayContacts.class);
                intent.putExtra("contactList", contactArrayList);
                startActivityForResult(intent,400);
            }
        });
        findViewById(R.id.buttonEditContact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ACTION = "EDIT";
                Intent intent = new Intent(ContactsHome.this, DisplayContacts.class);
                intent.putExtra("contactList", contactArrayList);
                startActivityForResult(intent,300);
            }
        });

        findViewById(R.id.buttonFinish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });


    }
}
11
  • You can't remove an object from an array during the iteration. You should probably see a "ConcurrentModificationException" when this is running. Commented Sep 19, 2017 at 22:28
  • 2
    Probably because it's not a valid statement. Commented Sep 19, 2017 at 22:28
  • 2
    @Karis nope, there's no iterator here. Commented Sep 19, 2017 at 22:31
  • What's the actual problem you've got here? Is it that you're deleting some but not all things equal to old? If that's the case, see my answer here. Commented Sep 19, 2017 at 22:32
  • 1
    If list has two consecutive elements matching old, then only the first will be removed, as you're skipping the second. Three ways to fix: 1) Use an Iterator and its remove() method. 2) Count i down, not up. 3) Decrement i when removing. --- If that is not the issue, then equals() is likely not implemented correctly, or old does not contain the values you think it does. Since you haven't shared any of that, we can't help there. Commented Sep 19, 2017 at 22:38

1 Answer 1

-1

To remove items while iterating a list, you should use an iterator.

For example:

Iterator it = contactArrayList.iterator();
while(it.hasNext()) {
    Object obj = it.next();
    if(obj.equals(old)) {
        it.remove();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What does that have to do with code in question not working? Doing it differently with an Iterator makes no difference.
@Andreas, you are right. I assumed too much with too little. Vikhyat, if no exceptions are being thrown, then its probably the equals method that is not working or the data in your list does not contain what you expected to contain. As Andreas said, you'll have to provide further details.
Yup the equals was not implemented. That was the problem. Thanks guys!

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.