0

I would like to call an action every time my spinner selects a new item. The current setup is that an action is called when a button is pressed.

My approach:

Create a new function: public void onSpinnerItemSelection(View V){...}

Then in content_home.xml I would add onSpinnerItemSelection to onClick for Spinner.

This isn't working. I'm not familiar with Android errors and I'm struggling to interpret these errors.

Unfortunately, myApp has stopped

Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
Marcus
  • 9,032
  • 11
  • 45
  • 84

2 Answers2

1

Step by step:

1ªDeclare variables from the layoutSpinner sp; the arraylist in this case ArrayList listTeams; and the adapter Adapter adapter;

2ªinstances

private void instances() {

    sp = (Spinner) findViewById(R.id.spinner);
    listaTeam = new ArrayList();

3º add items for the adapter

private void listAdapter() {

    listaTeam.add(new Equipo(R.string.Atleti));
    adaptador = new Adaptador(MainActivity.this, listaEquipo);
    sp.setAdapter(adapter);

now we make actions, use onItemSelected.

public void onItemSelected(AdapterView parent, View view, int position, long id) {

    Toast.makeText("this is my team", Toast.LENGTH_LONG).show();
}

https://www.youtube.com/watch?v=H3W0-Nv664I

Sergio H.R
  • 29
  • 5
0

Here is an example:

mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        Toast.makeText("log", "position = " position, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
        Toast.makeText("log", "onNothingSelected", Toast.LENGTH_LONG).show();
    }
});
meda
  • 45,103
  • 14
  • 92
  • 122