Item Selected in Spinner does not appear in Toast

Asked

Viewed 67 times

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

1 answer

0

First of all, remove the line below and use the direct arrayAdapter;

ArrayAdapter<String> spinnerArrayAdapter = arrayAdapter;

In place of

cliente = spinnerCliente.getSelectedItem().toString();

place

cliente = arrayAdapter.getItem(position);
  • I made the modifications you mentioned above and still not appearing Toast. I put a System.out.println to see what would appear on the console, the client name does not appear. Only the error appears below: 07-13 16:09:30.284 11179-11225/com.caixinhsmotoristas.caixamotoristasapp E/Surface: getSlotFromBufferLocked: Unknown buffer: 0xaaf09960

  • Toast only does not appear when I do it through an Arraylist, if I have done an Array and already set the strings within it type String test[] = { "Option1", "Option2", "Option3" }, so when I click on 1 Spinner item, Toast appears normally...

Browser other questions tagged

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