jean d'arme
jean d'arme

Reputation: 4353

ArrayList.indexOf() is not recognizing Objects

I have a ParseObject subclass , but everytime I want to get index of it it returns 0 so mListSectionPos returns an array of zero's (hachCode and equals methd implemented thanks to Apache Commons Utils).

It should be String.valueOf(mListItems.indexOf(beer_section)), but instead I'm using mListSectionPos.add(mListItems.indexOf(current_item) - 1); because it's working (more or less). Sometimes it cracks on getCurrentSectionPosition() that also works on indexOf() method.

So my question is: why indexOf() always return 0 in this piece of code?

It's based on https://github.com/bhavyahmehta/ListviewFilter - just adapted for ParseObject lists. Code below is my adaptation of his MainActivity.java that can be found here:

@Override
    protected Void doInBackground(ArrayList<PiwoSubclass>... params) {
        mListItems.clear();
        mListSectionPos.clear();
        ArrayList<PiwoSubclass> items = params[0];
        if(mItems != null) {
            if (mItems.size() > 0) {

                String prev_section = "";

                for (PiwoSubclass current_item : items) {
                    if (isCancelled()) break;
                    String current_section = current_item.getBeerName().substring(0, 1).toUpperCase(Locale.getDefault());

                    if (!prev_section.equals(current_section)) {
                        PiwoSubclass beer_section = null;
                        beer_section = new PiwoSubclass();
                        beer_section.setBeerName(current_section);

                        Log.i("ASD-current", beer_section.getBeerName());

                        mListItems.add(beer_section);
                        mListItems.add(current_item);

                        // array list of section positions
                        mListSectionPos.add(mListItems.indexOf(current_item) - 1); // that want works although it's way around



                        // TODO why is that error?
                        Log.i("ASD-listSectionSize", String.valueOf(mListItems.indexOf(beer_section)));

                        prev_section = current_section;
                    } else {
                        mListItems.add(current_item);
                    }
                }
            }
        }
        return null;
    }

PiwoSubclass

public class PiwoSubclass extends ParseObject {

private String objectIdP;
private String marka;
private String marka_lowercase;

public PiwoSubclass() {
}

public String getObjectIdfromParse() {
    return this.getObjectId();
}

public String getMarka(){
    return this.getString("marka");
}

public String getBrewery(){
    return this.getString("brewery");
}

public String getBeerName(){
    return this.getString("beer_name");
}

public String getMarka_lowercase() {
    return this.getString("marka_lowercase");
}

public void setMarka(String value){
    put("marka", value);
}

public void setBeerName(String value){
    put("beer_name", value);
}

public void setMarka_lowercase(String value){
    put("marka_lowercase", value);
}


@Override
public int hashCode() {
    return new HashCodeBuilder(17, 31) // two randomly chosen prime numbers
            // if deriving: appendSuper(super.hashCode()).
            .append(getObjectIdfromParse())
            .toHashCode();
}

@Override
public boolean equals(Object obj) {
    //return super.equals(obj);
    if (!(obj instanceof PiwoSubclass))
        return false;
    if (obj == this)
        return true;

    marka_lowercase = getMarka_lowercase();
    PiwoSubclass rhs = (PiwoSubclass) obj;
    //Log.i("ASD-subclass", marka + "/" + rhs.getMarka());
    return new EqualsBuilder()
            // if deriving: appendSuper(super.equals(obj)).
            .append(marka_lowercase, rhs.getMarka_lowercase())
            .isEquals();
}

Now I have IndexOutOfBounds exception from PinnedHeaderAdapter:

public int getCurrentSectionPosition(int position) {
    //String listChar = mListItems.get(position).getBeerName().substring(0, 1).toUpperCase(Locale.getDefault());
    PiwoSubclass ps = mListItems.get(position); // TODO errorrrrrrrrr
    return mListItems.indexOf(ps);

}

Upvotes: 0

Views: 70

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74078

First, you check for mItems

if(mItems != null) {
    if (mItems.size() > 0) {

but then you work with items

for (PiwoSubclass current_item : items) {
    /* ... */
}

and ignore mItems for the rest of the method. I don't see any connection between these two.


It seems indexOf() doesn't return 0 but 1, otherwise you would get an ArrayList full of -1s

mListSectionPos.add(mListItems.indexOf(current_item) - 1);

I guess, somehow you always check for the first current_item, which is the second element in mListItems. If you would check for the beer_section - as it does for current_section in the original code - the code would work as expected.


After looking into ArrayList.indexOf(), the most likely reason is your PiwoSubclass.equals() method compares always equal to the first non-section element, because it hasn't set a beer name or some similar condition.

So, fixing the equals method might work as well.

Upvotes: 1

Related Questions