Sun
Sun

Reputation: 6888

For Loop to get Data from custom ArrayList

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

Upvotes: 1

Views: 65

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

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

Upvotes: 1

Related Questions