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 selected item of Spinner by value, not by position
You have to take one more string array for selected country respective value :
<string-array name="country">
<item>India</item>
</string-array>
<string-array name="country_value">
<item>91</item>
</string-array>
Initialize value array :
String[] countryValue = getResources().getStringArray(R.array.country_value);
Get spinner selected item respective value from value array:
selectionarea = countryValue[position];
Check Example : accessing item name in string-array (Android)
Set selected spinner item based on a name, not a position in Android
All you can do is identifying the position of the item in the spinner and the setting the selection of spinner.
int pos=yourlist.indexOf("2000");
spinner.setSelection(pos);
Edit:
ArrayList<String> list=new ArrayList( Arrays.asList(getResources().getStringArray(R.array.years)) ); // your array id of string resource
int pos= list.indexOf("2000");
spinner.setSelection(pos);
To set selected item of Spinner by value, returns -1
int a = adapter.getPosition(b);
replace adapter with adapter1 in the above line, because you have initialized adapter as "adapter1"
Set selected item of spinner programmatically
Use the following:spinnerObject.setSelection(INDEX_OF_CATEGORY2)
.
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 :)
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 selected item for Spinner in Android
You can do similar to the following. I am just giving you an idea here.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.setSelection(position + 1);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
how to set selection on spinner based on ID, not index or position
oke I had to do the same thing what i did is made an extra int called SpinnerPosition and added that to my model (like your Message class)
then did something like this:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Message mSelected = mSelected.setSpinnerPosition(pos);
Log.i("Id:", mSelected.getId());
}
}
so now you can do something like (if you get the id from somewhere)
ArrayAdapter myAdap = (ArrayAdapter) spinner1.getAdapter();
if (Message.getID() == id) {
previousSelected = Message.getSpinnerPosition();
int spinnerPosition = myAdap.previousSelected;
spinner1.setSelection(spinnerPosition);
}
i quickly wrote the code so it probably is going to have some mistakes but I hope get get the idea behind it
Related Topics
How to Compare Two Double Values in Java
Platform.Runlater and Task in Javafx
Get Driving Directions Using Google Maps API V2
Using Static Variables in Android
The Following Classes Could Not Be Instantiated: - Android.Support.V7.Widget.Toolbar
Why This Javac: File Not Found Error
Benefits/Drawbacks to Running 64-Bit Jvm on 64-Bit Linux Server
How to Set Classpath in Linux to Let Java Find Jar File
Java Installation Issues on Ubuntu
Why Do I Get "Exception; Must Be Caught or Declared to Be Thrown" When I Try to Compile My Java Code
Can You Use @Autowired With Static Fields
Set Margins in a Linearlayout Programmatically
Java.Lang.Numberformatexception: Invalid Double: " "