I'm new to android and would like to know where the error is here. I'm trying to display list data with a Spinner component.
The Activity is like this:
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 looks 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)