Error grabbing the selected item in a Spinner and showing in a Toast

Asked

Viewed 665 times

0

I’m new on android and would like to know where is the error here. I’m trying to display list data with a Spinner component.

So is the Activity:

package sucesso.com.br.testecomponentes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private Spinner cidade;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cidade = (Spinner) findViewById(R.id.cidades);
    final ArrayList<String> cidades = new ArrayList<>();
    cidades.add("Selecione");
    cidades.add("São Paulo");
    cidades.add("Rio de Janeiro");

    cidades.add("Campinas");

    cidades.add("Salvador <3");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, cidades );
    cidade.setAdapter(adapter);

    cidade.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String nome = cidades.get(position);
            Toast.makeText(getBaseContext(), nome, Toast.LENGTH_SHORT).show();
        }
    });

}
}

And the xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sucesso.com.br.testecomponentes.MainActivity">

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="107dp"
    android:id="@+id/cidades"/>

</LinearLayout>

Could someone tell me why?

: FATAL EXCEPTION: main
     Process: sucesso.com.br.testecomponentes, PID: 13353
     java.lang.RuntimeException: Unable to start activity ComponentInfo{sucesso.com.br.testecomponentes/sucesso.com.br.testecomponentes.MainActivity}: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
         at android.app.ActivityThread.-wrap11(ActivityThread.java)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:148)
         at android.app.ActivityThread.main(ActivityThread.java:5443)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
      Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
         at android.widget.Spinner.setOnItemClickListener(Spinner.java:559)
         at sucesso.com.br.testecomponentes.MainActivity.onCreate(MainActivity.java:33)
         at android.app.Activity.performCreate(Activity.java:6245)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:148) 
         at android.app.ActivityThread.main(ActivityThread.java:5443) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

1 answer

3


setOnItemClickListener cannot be used in Spinner

Use setOnItemSelectedListener in place

Example:

cidade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String nome = cidades.get(position);
                Toast.makeText(getBaseContext(), nome, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
  • Mister, you solved my problem, thank you. But if it’s not too much to ask, you could tell me how I do it so the first option doesn’t show up on Toast. Pq already displays a value when it is not selected.

  • 1

    Try placing position with -1, I have no way to test now, but should solve String name = cities.get(position-1);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.