繁体   English   中英

无法获得android的自动完成与服务器响应一起使用

[英]Cant get android's autocomplete working with server response

因此,我一直在尝试为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中,它的工作原理类似于魅力,但是一旦我在查询到后端后尝试添加元素,下拉列表就不会出现。 我的后端工作正常。 我已经核实了。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM