Set selected item of spinner programmatically
Use the following:spinnerObject.setSelection(INDEX_OF_CATEGORY2)
.
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) {
}
});
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 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);
}
Set Spinner selection programmatically
It seems that your spinner2 is not in the second activity but in a completely different layout. Check and see if you have an adapter. If you have a custom adapter, then you have to pass that value of position to the constructor of your custom adapter.
In your second activity, pass the position value to the CustomAdapter constructor:
Intent intent = getIntent();
position = intent.getIntExtra("position", 0);
CustomAdapter jsonCustomAdapter = new CustomAdapter(SecondActivity.this, list, position);
In your CustomAdapter, set it up like this:
public class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<ItemObject> list;
private int spinnerPosition;
public CustomAdapter(Context context, List<ItemObject> list, int position) {
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list = list;
spinnerPosition = position;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.list, parent, false);
listViewHolder.spinner = (Spinner)convertView.findViewById(R.id.spinner2);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.spinner.setSelection(spinnerPosition);
return convertView;
}
static class ViewHolder{
Spinner spinner;
}
}
Android Spinner Selected Item
I think, as you are using a customadapter and giving three lists in adapter...
you can't get the selected text simply by calling the getSelectedItem()..
Use this:
Spinner mySpinner = (Spinner)findViewbyId(R.id.spinner);
int position = mySpinner.getSelectedItemPosition();
String Text = yourCityList[position].toString(); // dont know which list is holding the city list...
// i didn't use any db in any of my app so cant tell you how can you get list...
// leaving it to you... :)
Hope it helps....
Related Topics
Android Studio 0.4 Duplicate Files Copied in APK Meta-Inf/License.Txt
How to Build an Executable for Android Shell
Which Is the Best Way to Add a Button
How to Resize a Custom View Programmatically
Android Spinner with Multiple Choice
Android: Getting a File Uri from a Content Uri
Enable/Disable Data Connection in Android Programmatically
Calling Startactivity() from Outside of an Activity Context
How to Mock Location on Device
Android New Bottom Navigation Bar or Bottomnavigationview
Android Sdk Manager Won't Open
How to Provide Shadow to Button