0

I want to match index values of ArrayList elements with values from an int array. Pos array contains selected items, while the ArrayList object contains all items.

I want a new ArrayList containing selected items only, but the if condition turns out to be always false

        for (int i = 0; i < Pos.length; i++) {
            Category state = stateList.get(i);

            if(stateList.indexOf(i)==Pos[i]) {
                Toast.makeText(getApplicationContext(),"match",Toast.LENGTH_LONG).show();
                Drawer.add(state);
            }
        }
        Toast.makeText(getApplicationContext(),Drawer.size(),Toast.LENGTH_LONG).show();
11
  • So what issue you are facing? Commented Feb 11, 2016 at 10:12
  • its can't give output , Don't match index value and Array Value Commented Feb 11, 2016 at 10:15
  • Try to print the position values. Commented Feb 11, 2016 at 10:20
  • @AanalShah What are you trying to do? Commented Feb 11, 2016 at 10:24
  • statelist.indexof(i) give -1 value and, position give proper values Commented Feb 11, 2016 at 10:26

2 Answers 2

1

You have selected indices from stateList in the Pos array. So just get the items in stateList using the Pos array. Try this code.

for (int i = 0; i < Pos.length; i++) {
    Drawer.add(stateList.get(Pos[i]));
}
Toast.makeText(getApplicationContext(),Drawer.size(),Toast.LENGTH_LONG).show();
Sign up to request clarification or add additional context in comments.

Comments

1

PLease try this -

     if (i == Pos[i]) {
    Toast.makeText(getApplicationContext(), "match", Toast.LENGTH_LONG).show();
    Drawer.add(state);
}

1 Comment

yeah Pleasure. :)

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.