Search code examples
androidautocompletetextview

GetSelectedItem/value from a AutoCompleteTextView [Not Working]


i have been trying to get a selectedItem from one of my AutoCompleteTextView(sp_Especie) and pass it to a string variable so i can use it in if´s conditions in the other one(sp_Raza) and had no success.

Heres my code:

final ArrayList Perros = new ArrayList();

 String[] datos = new String[]{"Perro", "Gato"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(CrearPost.this, android.R.layout.simple_dropdown_item_1line, datos);
        sp_Especie.setAdapter(adapter);
        sp_Especie.setThreshold(1);


        sp_Raza.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AutoCompleteTextView sp_Especie = (AutoCompleteTextView) findViewById(R.id.sp_Especie);
                String text = sp_Especie.toString();
                System.out.println("COMES DATA " + text);




 if(text=="Perro"){
                    AsyncHttpClient client = new AsyncHttpClient();
                    client.get("domain+php", new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                            if(statusCode==200){
                                try {

                                    JSONArray jsonArray = new JSONArray(new String(responseBody));

                                    for (int i=0; i<jsonArray.length();i++){
                                        System.out.println("COMES DATA " + jsonArray.getJSONObject(i).getString("Nombre"));
                                        Perros.add(jsonArray.getJSONObject(i).getString("Nombre"));

                                    }



                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                        }
                    });

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(CrearPost.this, android.R.layout.simple_dropdown_item_1line, Perros);
                    sp_Raza.setAdapter(adapter);
                    sp_Raza.setThreshold(1);

                }


            }

        });

i have checked the logcat and it seems that the text doesnt give anything back. But im new to AutoCompleteTextViews so i dont know what method i could use to solve this problem.

Any help would be highly apreciated


Solution

  • To get a text from AutoCompleteTextView you should use

    String text = sp_Especie.getText().toString();
    

    instead of

    String text = sp_Especie.toString();
    

    Your code will be

    sp_Raza.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AutoCompleteTextView sp_Especie = (AutoCompleteTextView) findViewById(R.id.sp_Especie);
                    String text = sp_Especie.getText().toString();
                    System.out.println("COMES DATA " + text);
    
                    if ("Perro".equals(text)) {
                        // TODO: Write your code here
                    }
                }
    });