0

I got a button which opens up a spinner:

    butonLista.setOnClickListener(new View.OnClickListener() 

        {
            public void onClick(View v){

                spinnerLista.performClick();
            }  
        });

This is the spinner:

   spinnerLista = new Spinner(this);
               public void spinnerLista(){
       adapter = new ArrayAdapter<String>(this, 
             android.R.layout.simple_spinner_item, masini);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerLista.setAdapter(adapter);
            spinnerLista.setOnItemSelectedListener(new listaOnClickListener());
   }

In the listaOnClickListener i have a log:

        public class listaOnClickListener implements OnItemSelectedListener{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long arg3) {     

        String nrInmat = String.valueOf(spinnerLista.getItemAtPosition(pos));
        Log.w("Numar inmatriculare:", nrInmat+"");
        txtNrInmat.setText(nrInmat);
    }


    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub
        }
    }

After i click the button, the spinner list pops up, When i click an item from the list the log doesnt appear, the commands i put in the OnClickListener arent called, what can i do?

I finally want to set an EditText to spinnerLista.getItemAtPosition(pos);

victorholo
  • 105
  • 1
  • 11

4 Answers4

0
   spinnerLista.performClick();

will perform click on the Spinner, not on the spinner individual item view.

use

    spinner.getOnItemClickListener().onItemClick(parent, view, position, id);

to perform click on individual item view.

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • yes, spinnerLista.performCLick(); will perform a click on the spinner, after that i'll click an individual item from the spinner. so shouldnt the onclicklistener get called? – victorholo Nov 22 '13 at 10:27
  • did you mean onItemSelectedListener is not get called when you click on Spinner item? – Gopal Gopi Nov 22 '13 at 10:30
  • yes, public void onItemSelected(AdapterView> parent, View view, int pos,long arg3) – victorholo Nov 22 '13 at 10:32
  • this method will not be called when you select a item which is selected previously. – Gopal Gopi Nov 22 '13 at 10:35
  • where did i previously selected it? – victorholo Nov 22 '13 at 10:45
  • when you set Adapter to Spinner, by default it will select first item from Adapter and will call OnItemSelectedListener. Now when you click on Spinner, It will open list of items shown as ListView and if you select first item from that list, it will not call OnItemSelectedListener as it is selected previously. – Gopal Gopi Nov 22 '13 at 10:49
  • so when i set adapter to spinner, why dont i get a log or something? i dont have an actual spinner(spinnerLista = new Spinner(this);), just a button and an edittext. – victorholo Nov 22 '13 at 11:03
  • do you mean spinner is not a part of your Activity Window? – Gopal Gopi Nov 22 '13 at 11:06
  • no its not, i only have a button and an edittext, i press button the spinner list apears on screen, i press an item from list,the edittext gets the content of item from the selected position. Have you understood? But the onselecteditem doesnt get called when i push the item so i dont know what to put in the edittext. its that simple. sorry about my english if that was the problem. – victorholo Nov 22 '13 at 11:11
  • you have created spinner dynamically but you have not added that to Activity layout – Gopal Gopi Nov 22 '13 at 11:42
  • i solved it by adding a spinner with 50dp width, istead of the button, so it look the same as a button but its a spinner :) ty for the help – victorholo Nov 22 '13 at 11:57
0

You can use this for selected item from the spinner

String nrInmat = String.valueOf(spinnerLista.getSelectedItemPosition());

Or you may have to Implement OnItemSelectedListener in your Activity.

   {     adapter = new ArrayAdapter<String>(this, 
         android.R.layout.simple_spinner_item, masini);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerLista.setAdapter(adapter);           
        spinnerLista.setOnItemSelectedListener(this);
}
@Override

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3){     

    //Do what ever you want when you click on spinner       

}

 @Override

public void onNothingSelected(AdapterView<?> arg0) {}
0

Try this instead, something is probably wrong in your onItemSelectedListener subclass.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       Log.i("SPINNER-TRACE", "onItemSelected");
    }

    public void onNothingSelected(AdapterView<?> parent) {
       Log.i("SPINNER-TRACE", "onNothingSelected");
    }
});
0

To popup spinner on button click use spinner.performClick inside onclick event of button.

and have a look at below code to implement onItemSelectedListener for spinner.

    Spinner s1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.colors, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter);
    s1.setOnItemSelectedListener(
            new OnItemSelectedListener() {
                public void onItemSelected(
                        AdapterView<?> parent, View view, int position, long id) {
                    showToast("Spinner1: position=" + position + " id=" + id);
                }

                public void onNothingSelected(AdapterView<?> parent) {
                    showToast("Spinner1: unselected");
                }
            });

Also spinner.getSelectedItem() will give item which is currently selected.

You can ask if you have any further queries!

Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • it doesnt work, the onitemselectedlistener doesnt get called, i posted all my code, sorry if it was incomplete. – victorholo Nov 22 '13 at 10:50
  • Go through this tutorial and import this project in your eclipse and check weather it is running or not? http://www.androidhive.info/2012/04/android-spinner-dropdown-example/ – Jigar Pandya Nov 22 '13 at 10:53
  • i already got spinners in my app that work, i know how to make a spinner. This is what i want to be an editable spinner, edittext+button. – victorholo Nov 22 '13 at 10:59
  • please share your your xml and java file – Jigar Pandya Nov 22 '13 at 11:11