I have 2 APIs. From API1 I'm fetching a list of category names and setting it on spinner. From API2 I'm getting a category name, which I want to set as selected item in the spinner. But the problem is that the first time I'm not getting any position. When I go back to previous activity and come back to my activity, this time I'm getting the position.
private int getIndex(Spinner spinner, String myString){
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).equals(myString)){
index = i;
}
}
return index;
}
// API2's JSON parsing
if(newobj.getString("cat_name")!=null){
int spinnerPosition = getIndex(catspin, newobj.getString("cat_name"));
catspin.setSelection(spinnerPosition);
}