0

So I have been trying to add a suggestion list for my auto-complete text view in android. I have added an onClickListener to it. Whenever onCLick is triggered. I have created an adapter and a data structure called mylist (ArrayList). I can't see any error but at the same time the autocomplete feature is not working. I am pretty sure there is some small glitch I am unable to find. Please let me know where am I going wrong. TIA.

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user_input = findViewById(R.id.autoCompleteTextView1);
        Log.i("here", "something");
        user_input.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("here", "something");
                String symbol_auto = String.valueOf(user_input.getText());
                String company_auto = "http://us-east-2.elasticbeanstalk.com/autocomplete/"+symbol_auto;
                requestQueue = Volley.newRequestQueue(MainActivity.this);
                JsonArrayRequest arrayreq = new JsonArrayRequest(company_auto+symbol_auto,
                        new Response.Listener<JSONArray>() {

                            // Takes the response from the JSON request
                            @Override
                            public void onResponse(JSONArray response) {
                                try {
                                    JSONObject jsonobj = response.getJSONObject(0);
                                    data = jsonobj.getString("Name");
                                    mylist.add(data);
                                    Log.i("here", data);
                                    ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.select_dialog_item, mylist);
                                    user_input.setThreshold(1);
                                    user_input.setAdapter(adapter);
                                }
                                // Try and catch are included to handle any errors due to JSON
                                catch (JSONException e) {
                                    // If an error occurs, this prints the error to the log
                                    e.printStackTrace();
                                }
                            }
                        },
                        // The final parameter overrides the method onErrorResponse() and passes VolleyError
                        //as a parameter
                        new Response.ErrorListener() {
                            @Override
                            // Handles errors that occur due to Volley
                            public void onErrorResponse(VolleyError error) {
                                Log.e("Volley", "Error");
                            }
                        }
                );
                // Adds the JSON array request "arrayreq" to the request queue
                requestQueue.add(arrayreq);
            }
        });
    }

I have tried adding elements to myList manually and it works like charm but the dropdown list just doesnt appear once I try adding it after querying to my back-end. My back-end is working fine. I have verified.

jyotirmaya ojha
  • 73
  • 1
  • 13
  • are you sure that your server is working? did you receive response from your server? what is the data? – diegoveloper Nov 23 '17 at 18:05
  • I altered the url for this question inorder to prevent multiple visits to server (its on free tier of AWS) but yeah the url is working. The symbol_auto should be getting the symbol from the autocompletetextview after each event. I manually typed the url with appended parameter and it works fine. It returns a JSON object. – jyotirmaya ojha Nov 23 '17 at 18:07
  • put the json object in your question – diegoveloper Nov 23 '17 at 18:08
  • [{"Symbol":"ABC","Name":"AmerisourceBergen Corp","Exchange":"NYSE"},{"Symbol":"ABC","Name":"AmerisourceBergen Corp","Exchange":"BATS Trading Inc"},{"Symbol":"ABCB","Name":"Ameris Bancorp","Exchange":"NASDAQ"}] – jyotirmaya ojha Nov 23 '17 at 18:13
  • @diegoveloper This is the response for abc as the parameter – jyotirmaya ojha Nov 23 '17 at 18:13
  • do you receive any error log ? what is the result of this : Log.i("here", data); ? – diegoveloper Nov 23 '17 at 18:14
  • do you want to put only the first element in your autocomplete or do you want to put the complete array? – diegoveloper Nov 23 '17 at 18:17
  • @diegoveloper it seems the code is not reaching Log.i("here", data); Now I see E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length as the error – jyotirmaya ojha Nov 23 '17 at 18:23
  • so inside `onItemClick` you call web API? what for? – pskink Nov 23 '17 at 18:23
  • @diegoveloper I would want the complete. I intend on looping through the entire object to extract necessary data – jyotirmaya ojha Nov 23 '17 at 18:24
  • @pskink I did that so that API could return me back a json object which I could parse in order to get relevant data, store that data into mylist so that it appears in the autotextcompleteview – jyotirmaya ojha Nov 23 '17 at 18:25
  • see https://stackoverflow.com/a/19860624/2252830 – pskink Nov 23 '17 at 18:26
  • @pskink I have chosen to use Volley – jyotirmaya ojha Nov 23 '17 at 18:28
  • and? does it change anything? just make sync request inside `runQuery` method, did you run my code? if so, whats unclear? – pskink Nov 23 '17 at 18:39
  • @pskink I dont know how filterQueryProvider works. I need to understand your code first. I have invested 2 days already in Volley. SO thought of debugging it first. Hope you understand. – jyotirmaya ojha Nov 23 '17 at 18:43
  • https://developer.android.com/reference/android/widget/FilterQueryProvider.html – pskink Nov 23 '17 at 18:45

1 Answers1

0

You have to iterate over your elements and add each one to your list, then set the adapter.

 new Response.Listener<JSONArray>() {

                        // Takes the response from the JSON request
                        @Override
                        public void onResponse(JSONArray response) {
                            try {
                                 mylist = new ArrayList();   
                                for (int i = 0; i< response.length(); i++){
                                    JSONObject jsonobj = response.getJSONObject(i);
                                    String value = jsonobj.getString("Name");
                                    mylist.add(value);
                                    Log.i("here", value);
                                }

                                ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.select_dialog_item, mylist);
                                user_input.setThreshold(1);
                                user_input.setAdapter(adapter);
                            }
                            // Try and catch are included to handle any errors due to JSON
                            catch (JSONException e) {
                                // If an error occurs, this prints the error to the log
                                e.printStackTrace();
                            }
                        }
                    },

UPDATE - use OnClickListener to fire the event

user_input.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     Log.i("here", "something");
            String symbol_auto = String.valueOf(user_input.getText());
            String company_auto = "http://us-east-2.elasticbeanstalk.com/autocomplete/"+symbol_auto;
            requestQueue = Volley.newRequestQueue(MainActivity.this);
            JsonArrayRequest arrayreq = new JsonArrayRequest(company_auto+symbol_auto,
                    new Response.Listener<JSONArray>() {

                        // Takes the response from the JSON request
                        @Override
                        public void onResponse(JSONArray response) {
                            try {
                                 mylist = new ArrayList();   
                                for (int i = 0; i < response.length(); i++){
                                    JSONObject jsonobj = response.getJSONObject(i);
                                    String value = jsonobj.getString("Name");
                                    mylist.add(value);
                                    Log.i("here", value);
                                }

                                ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.select_dialog_item, mylist);
                                user_input.setThreshold(1);
                                user_input.setAdapter(adapter);
                            }
                            // Try and catch are included to handle any errors due to JSON
                            catch (JSONException e) {
                                // If an error occurs, this prints the error to the log
                                e.printStackTrace();
                            }
                        }
                    },
                    // The final parameter overrides the method onErrorResponse() and passes VolleyError
                    //as a parameter
                    new Response.ErrorListener() {
                        @Override
                        // Handles errors that occur due to Volley
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Volley", "Error");
                        }
                    }
            );
            // Adds the JSON array request "arrayreq" to the request queue
            requestQueue.add(arrayreq);
        }
    });
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • I get an error stating response expected boolean not int. length() returns int, right ? – jyotirmaya ojha Nov 23 '17 at 18:31
  • Still not getting any response from the server. I do see E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length when I completely empty my autocomplete. – jyotirmaya ojha Nov 23 '17 at 18:44
  • Its not entering user_input.setOnItemClickListener(new OnItemClickListener() { Because on entering debug mode, I cant find my log.d() but I wonder why because on my phone (not using an emulator) I am tapping on autocomplete to start the event. Really confused. – jyotirmaya ojha Nov 23 '17 at 18:54
  • @jyotirmayaojha do you know what `OnItemClickListene` is for? – pskink Nov 23 '17 at 18:57
  • Its used when an item in a particular adapter view is clicked. I am really sorry if I am wasting your time but I started android dev two days back. – jyotirmaya ojha Nov 23 '17 at 18:59
  • I updated my answer, use onclicklistener to fire the event and onItemClickListener to get which item of the autocompletextview was clicked – diegoveloper Nov 23 '17 at 19:00