无法使用服务器响应

时间:2017-11-23 18:00:27

标签: java android autocomplete android-volley

所以我一直在尝试为android中的自动完成文本视图添加建议列表。我已经添加了一个onClickListener。每当onCLick被触发时。我创建了一个适配器和一个名为mylist(ArrayList)的数据结构。我看不到任何错误,但同时自动完成功能无法正常工作。我很确定我找不到一些小故障。请告诉我哪里出错了。 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);
            }
        });
    }

我已经尝试手动向myList添加元素,它就像魅力一样,但是一旦我在查询到后端后尝试添加它,下拉列表就不会出现了。我的后端工作正常。我已经验证了。

1 个答案:

答案 0 :(得分:0)

您必须迭代元素并将每个元素添加到列表中,然后设置适配器。

 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();
                            }
                        }
                    },

更新 - 使用OnClickListener触发事件

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);
        }
    });