Reputation: 39
i have a spinner in an app im doing, and when it goes to OnItemSelcted it automatically selects the first option, how do i stop that in a simple way?
spinner2.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent,
View view,
int pos, long id) {
Upvotes: 2
Views: 4682
Reputation: 1
Just add an if
Statement to check the position of the item. Add a hint on item at position 0, when item other than position 0 is selected then perform your function as shown below:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position != 0)
{
String val = spinner.getSelectedItem().toString();
// Toast.makeText(activity, formid, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Upvotes: 0
Reputation: 21
This question has been asked many times on SO and elsewhere. For example:
onNavigationItemSelected in ActionBar is being called at startup how can avoid it?
Infinite loop when using onNavigationItemSelected and invalidateOptionsMenu
Activity loop using onNavigationItemSelected on an ActionBar
Several answers and comments suggest ignoring (some) callbacks. This would work for very simple uses, but it ignores the fact that most people asking this question want a menu not the spinner-based tab-like navigation provided by the standard ActionBar in list mode. Some comments suggest using a PopupMenu, but none that I saw provided details. So here's what to do:
Create the View that will be displayed in the ActionBar and on which the user will click to see the menu:
View view = this.getLayoutInflater().inflate(R.layout.actionbar_view, null);
Create a PopupMenu anchored to that View:
PopupMenu popup = new PopupMenu(this, view);
popup.getMenuInflater().inflate(R.menu.actionbar_menu, popup.getMenu());
Add an OnClickListener
to the View that shows the PopupMenu:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.show();
}
});
Add an OnMenuItemClickListener
to the PopupMenu to receive notification when the user selects from the menu:
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
Finally, use the following to configure the ActionBar in your Activity:
ActionBar ab = this.getActionBar();
ab.setCustomView(view);
ab.setDisplayShowCustomEnabled(true);
ab.setDisplayShowTitleEnabled(false);
One comment is that it can be a bit tricky to get the main View styled to match the default ActionBar. Try investigating the following style resources:
@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title
@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title.Inverse
@android:style/TextAppearance.Holo.Widget.ActionBar.Title
@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse
Otherwise you need at least the right color and text size probably medium. The view itself should obviously fill_parent
and have center_vertical
gravity.
'Nuff said.
Upvotes: 2
Reputation: 314
You can use a boolean variable to check if it is first time, do not execute the code. Execute your code second time, otherwise it is the default behavior.
Upvotes: 1
Reputation: 1007534
how do i stop that in a simple way?
You don't. A Spinner
always has a selection, unless the adapter is empty and no selection is possible. Your app needs to always handle a selection, by one means or another.
Upvotes: 0