1
When clicking on the item in Spinner, Toast is not called, nothing appears.
public class RegistrarActivity extends AppCompatActivity {
private ImageView botaoVoltar;
private EditText textoCPF, textoEmail;
private Spinner spinnerCliente;
private List<String> clientes = new ArrayList<String>();
private String cliente;
private RequestQueue requestQueue;
private static final String URL = "http://www.caixinhadosmotoristas.com.br/spinner-cliente.php";
private StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registrar);
botaoVoltar = (ImageView) findViewById(R.id.botaoVoltarId);
textoCPF = (EditText) findViewById(R.id.textoCpfId);
textoEmail = (EditText) findViewById(R.id.textoEmailId);
spinnerCliente = (Spinner) findViewById(R.id.spinnerClienteId);
requestQueue = Volley.newRequestQueue(this);
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for(int i = 0; i < jsonArray.length(); i++){
clientes.add(jsonArray.getJSONObject(i).getString("nome"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, clientes);
ArrayAdapter<String> spinnerArrayAdapter = arrayAdapter;
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinnerCliente.setAdapter(spinnerArrayAdapter);
spinnerCliente.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cliente = spinnerCliente.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "Nome: " + cliente, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SimpleMaskFormatter mascaraCPF = new SimpleMaskFormatter("NNN.NNN.NNN-NN");
MaskTextWatcher maskCPF = new MaskTextWatcher(textoCPF, mascaraCPF);
textoCPF.addTextChangedListener(maskCPF);
botaoVoltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent voltaLogin = new Intent(RegistrarActivity.this, LoginActivity.class);
startActivity(voltaLogin);
}
});
}
}
I was able to solve my problem through this link below: https://answall.com/questions/33548/spinner-n%C3%A3o-show-item-selected
– Thiago Mourão Peres