Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnItemClickListener of spinner

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?

like image 332
bShah Avatar asked Feb 06 '14 10:02

bShah


People also ask

What is OnItemClickListener in Android?

android.widget.AdapterView.OnItemClickListener. Known indirect subclasses. CharacterPickerDialog, PreferenceScreen. CharacterPickerDialog. Dialog for choosing accented characters related to a base character.

What is spinner explain?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object.

What is the example of spinner?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.


1 Answers

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);
    }
like image 75
Biraj Zalavadia Avatar answered Oct 22 '22 17:10

Biraj Zalavadia