0

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

}
3
  • 2
    indexOf returns an int, not an array Commented Oct 11, 2015 at 17:55
  • Yes, I know - I meantmListSectionPos when called returns arrays of zeros. Updated my answer to be more clear. Commented Oct 11, 2015 at 18:10
  • @Rami sure, updated! Commented Oct 11, 2015 at 23:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I suspect equals method as well.
First, I would check the values of marka and marka_lowercase. This is independent of Apache Commons Utils.
Okay, I found that (stupid!) error - in equals I checked marka_lowercase instead of beerName from getBeerName() which now works and mListSectionPos have proper values - so thanks for that one! It's my mistake of not checking things enough. I updated my question with another problem that resolves around that one - I'd be grateful for checking that as well :)
I'm glad this works now. :-) For me it is a bit late today, so maybe tomorrow. To get more eyes looking into this, I would rather not mix this here, but ask a separate question.
I'll do that. Thanks again :)

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.