How to Set Spinner Default by Its Value Instead of Position

How to set Spinner Default by its Value instead of Position?

Finally, i solved the problem by using following way, in which the position of the spinner can be get by its string

private int getPostiton(String locationid,Cursor cursor)
{
int i;
cursor.moveToFirst();
for(i=0;i< cursor.getCount()-1;i++)
{

String locationVal = cursor.getString(cursor.getColumnIndex(RoadMoveDataBase.LT_LOCATION));
if(locationVal.equals(locationid))
{
position = i+1;
break;
}
else
{
position = 0;
}
cursor.moveToNext();
}

Calling the method

    Spinner location2 = (Spinner)findViewById(R.id.spinner1);
int location2id = getPostiton(cursor.getString(3),cursor);
location2.setSelection(location2id);

I hope it will help for some one..

How to set default value for spinner in android

Use this

spinner.setSelection(position);//to set default values

To get selected value use this

spinner.getSelectedItem();

Hi in your library you can use this

spinner.setSelectedIndex(2); 

Spinner Default Value / Change position

It looks like you're never setting the spinner to the selected index. Your if statements in onItemSelected aren't doing anything since "position" is a value that gets passed in from the spinner and not the previous intent.

Try putting this

s1.setSelection(Integer.parseInt(selected));

after you set your adapter.

How to set selected item of Spinner by value, not by position?

Suppose your Spinner is named mSpinner, and it contains as one of its choices: "some value".

To find and compare the position of "some value" in the Spinner use this:

String compareValue = "some value";
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
if (compareValue != null) {
int spinnerPosition = adapter.getPosition(compareValue);
mSpinner.setSelection(spinnerPosition);
}

How to set a default text to a Spinner

Use this code

declaration

String selected, spinner_item;

spinner code

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
selected = sp.getSelectedItem().toString();
if (!selected.equals("Country"))
spinner_item = selected;
System.out.println(selected);

setid();
}

private void setid() {
sp.setSelection(sp_position);
t.setText(spinner_item);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
});

Android Spinner: Set selected item as default

You can use SharedPreferences to store the selection the first time that the user selects a country, and then use SharedPreferences again for the app to remember the selection, when the user returns a second time.

To store the selection in a SharedPrefence:

SharedPreferences.Editor editor = getPreferences(0).edit();
int selectedPosition = yourSpinner.getSelectedItemPosition();
editor.putInt("spinnerSelection", selectedPosition);
editor.apply();

To load the selection onto the spinner when reusing the app:

SharedPreferences prefs = getPreferences(0);
yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));

Hope this solves your issue :)



Related Topics



Leave a reply



Submit