Android: setSelection having no effect on Spinner
Try moving the call to setSelection()
after the call to setAdapter()
.
Android Spinner.setSelection() doesn't work
Try Spinner#setSelection (int position, boolean animate)
with animate = false. I remember a while back I had a similar problem and this did the trick. The internal implementation seems to differ apart from the difference coming from the animate part.
Spinner setSelection() not work
Have you tried to set the spinner by using two arguments, the second using a boolean:
detailsOptionSpinner.setSelection(finalCount, true);
From the developers page it shows:
setSelection(int position, boolean animate)
//Jump directly to a specific item in the adapter data.
spinner set selection doesn't working
Make sure your setSelection()
is called after you done with spinner's setAdapter()
.
Spinner methods (setSelection) not working for only one spinner in activity
Put your setSelection before setting adapter.
After seeing your code you forgot to put the second parameter of setSelection()
.
It accepts two selection setSelection(position in int, Boolean)
E.g.
setSelection(boardNumber - 7, true)
Spinner.setSelection doesn't trigger OnItemSelectedListener properly
Have you tried to set the spinner by using two arguments, the second using a boolean:
.setSelection(mProvincesCode.indexOf(mUser.getProvince()), true);
From the developers page it shows:
setSelection(int position, boolean animate)
//Jump directly to a specific item in the adapter data.
SetSelection on a spinner crashes when layout_weight is assigned to spinner
spin1.setSelection(0); //does not crash
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1);
change to
spin1.setSelection(0, true);
spin1.setSelection(getResources().getStringArray(R.array.feet).length - 1, true);
I also had this question and solved
Spinner selection not working Android
I'll solve like this.Try this. This is just example Activity.
public class MainActivity extends Activity {
SpinnerAdapter ratingAdapter;
ListView listView;
List<String> listString;
ArrayAdapter<String> dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listString = new ArrayList<String>();
for(int listCount = 0; listCount < 20 ; listCount++){
listString.add("ListCount"+listCount);
}
List<String> spinnerData = new ArrayList<String>();
for (int i = 0; i < listString.size(); i++) {
spinnerData.add("MyTest="+i);
}
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fillAdapter();
}
private void fillAdapter() {
ratingAdapter = new SpinnerAdapter(getApplicationContext());
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(ratingAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ViewHolder {
Spinner spinner;
}
class SpinnerAdapter extends ArrayAdapter {
Context context;
HashMap<Integer,Integer> selectedItems = new HashMap<Integer, Integer>();
public SpinnerAdapter(Context context) {
super(context, R.layout.item_list_spinner_adaoter, listString);
this.context = context;
}
public int getCount() {
return listString.size();
}
public Object getItem(int position) {
return listString.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.spinner.setAdapter(dataAdapter);
if ( selectedItems.get( position ) != null ) {
holder.spinner.setSelection( selectedItems.get( position ) );
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedItems.put( position, arg2 );
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return convertView;
}
}
}
In activity_main.xml:
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:stackFromBottom="false"
android:transcriptMode="disabled" >
</ListView>
</LinearLayout>
In item_list_spinner_adaoter.xml:
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Related Topics
How Does Native Android Code Written for Arm Run on X86
Android Detect Phone Lock Event
How to Center Progress Indicator in Progressdialog Easily (When No Title/Text Passed Along)
Handling Back Button in Android Navigation Component
Handlers, Messagequeue, Looper, Do They All Run on the UI Thread
How to Control the Scrolling Speed of Recyclerview.Smoothscrolltoposition(Position)
Get All Child Views Inside Linearlayout at Once
Android Webview Click Open Within Webview Not a Default Browser
Programmatically Add View One Below Other in Relative Layout
What Is Setcontentview(R.Layout.Main)
Change Keyboard Input Language
Users Does Not Define No Argument Constructor
Streaming Audio from a Url in Android Using Mediaplayer
How to Get Toolbar from Fragment
When to Request Permissions with Facebook's New Android Sdk 3.0