OnItemClickListener of spinner
30,674
try this may helps you
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
final Spinner spinner = (Spinner) menu.getItem(0).getActionView().findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
Log.i("Selected item : ", items);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return super.onCreateOptionsMenu(menu);
}
Author by
bShah
Updated on July 31, 2022Comments
-
bShah almost 2 years
I am able to get Spinner in action bar this way; array of items in re/values/languages.xml
<string-array name="languages"> <item>Finnish</item> <item>French</item> <item>German</item> <item>Slovakian</item> <item>Polish</item> </string-array>
In res/menu/main.xml
<item android:id="@+id/menuSort" android:actionLayout="@layout/spinner" android:showAsAction="ifRoom" android:title="@string/choose"/>
In res/layout/spinner.xml
<Spinner android:id="@+id/spinner" android:layout_width="150dp" android:layout_height="wrap_content" android:entries="@array/languages"/>
And finally activity class;
public class Base_Activity extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); final Spinner spinner = new Spinner(this); Log.i("DEBUG1", "CHECKPOINT1"); ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerArrayAdapter); Log.i("DEBUG2", "CHECKPOINT2"); spinnerArrayAdapter.setDropDownViewResource(0); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { String items=spinner.getSelectedItem().toString(); Log.i("Selected item : ",items); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); return true; }
}
What I am not getting is on ItemSelected of spinner items nothing happens. As you can see from my xml file that I do not even need Adapter to get the spinner. But I am using adapter to so something to get OnItemSelected. Please help me what has happened here?
-
bShah about 10 yearsTried that also but did not help. :(
-
bShah about 10 yearsgives {java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Spinner}
-
bShah about 10 yearsNow the strings(items) are missing from spinner.
-
bShah about 10 yearsI marked your answer as correct but I edited a bit to reach my goal.
-
bShah about 10 yearsDone now!! check the ticked answer. Thanks for the support.
-
Roel over 8 yearsGreat arguments names arg0, arg1, arg2 and arg3. Very readable! Maybe you should add the Android sourcecode to eclipse or start using Android Studio.
-
LadyWoodi about 8 yearsspinner with OnItemClickListener is returing Runtime Exception that it cannot be used together. It looks like you should use onItemSelectedListener.
-
Daniel Handojo about 8 yearsI like the example. For some reason, the spinners guide on Android doesn't stress spinner.getSelectedItem(). They recomment you use arg0.getItemAtPosition(arg2): developer.android.com/guide/topics/ui/controls/spinner.html