Skip to content Skip to sidebar Skip to footer

Android Spinner Setting Value When Re

Solution 1:

That can be possible only when you store the array of the items that are set as entries of Spinner. You can set the position of the selected item in spinner. So you have to find out index of the item that you query from the database. Load the StringArray of categorylist and iterate the array and compare each item with the item fetched from the database. Then set the index of the found item as selected item in spinner.

    String[] list = getResources().getStringArray(R.array.categorylist);
    intindex = 0;
    for(String item : list) {
        if(item.equals("your_item")){
            break;
        }
        index++;
    }
    spinner.setSelection(index);

Solution 2:

Well by using setSelection() you can set particular selection for the spinner rather than the first one (by default). Now your question would be how to get position from the value that you have. So here is the code to do that:

StringyourString="some value"; //the value you want the position forArrayAdapteryourAdap= (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapterintspinnerPosition= yourAdap.getPosition(yourString);

//set the default according to value
spnr.setSelection(spinnerPosition);

Post a Comment for "Android Spinner Setting Value When Re"