1

I am just trying to get state name using state id, for an example: my strId is 1, but not getting state name which belongs to strId 1, everytime getting last state name found in stateModelArrayList

for (int i=0; i<stateModelArrayList.size(); i++) {
    String strId = stateModelArrayList.get(i).getId().toString();
    if(strId.equals(strState)) {
        Toast.makeText(MainActivity.this, strId+" : "+strState, Toast.LENGTH_SHORT).show();
        strStateName = stateModelArrayList.get(strId).getName().toString();         
    }
        editState.setText(strStateName);
}
5
  • Try Loging your strId inside your loop,and make sure that your condition mets. Commented Apr 16, 2016 at 12:16
  • condition mets perfectly... as you can I am using Toast to show strId if equals to strState... and getting exact result, I supposed to get Commented Apr 16, 2016 at 12:17
  • 3
    strStateName = stateModelArrayList.get(strId).getName().toString(); this is weird, why are you passing strId to get and not i? Commented Apr 16, 2016 at 12:19
  • Yes, Jorn Vernee is correct. Commented Apr 16, 2016 at 12:20
  • thank you your solution worked for me @JornVernee Commented Apr 16, 2016 at 12:46

1 Answer 1

1

I guess that your mistake is here:

strStateName = stateModelArrayList.get(strId).getName().toString(); 

It should be:

strStateName = stateModelArrayList.get(i).getName().toString(); 

If you don't want to get the last value don't forget to call break to stop the loop

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

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.