无法在 Android 中使用 Volley 响应执行 AutoCompleteTextView

Unable to execute AutoCompleteTextView using Volley response in Android

我一直在尝试在我一直在使用的应用程序之一上实现自动完成功能。 服务器发回 json 响应。我试着用 Volley 解析它。它有效,但我未能将响应与自动完成文本视图集成。

MainActivity.java

        user_input = findViewById(R.id.autoCompleteTextView1);
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item, mylist);

        String symbol_auto = String.valueOf(user_input.getText());


        requestQueue = Volley.newRequestQueue(this);
        //results = findViewById(R.id.jsonData);
        mylist.add("india");
        mylist.add("iran");
        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);
                            //Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
                        }
                        // 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);

        user_input.setThreshold(1);
        user_input.setAdapter(adapter);

当我尝试手动添加 "India" 和 "Iran" 等字符串时。它有效,您可以在建议下拉列表中看到它们,但我看不到从服务器返回的任何添加数据。

这是我的自动完成文本视图

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/stockLabel"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:completionThreshold="3"
    android:ems="10"
    android:text="@string/symbol" />

我一直在思考这个问题。我在控制台上找不到任何错误。我想到的一件事是,也许 Volley 没有及时回应。 如果我尝试在其他一些小部件中打印 Volley 的响应,那么它就像魅力一样。任何帮助表示赞赏。 TIA.

您的请求是异步的,您必须等待响应才能更新您的 UI

像这样修改你的方法:

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

在 Volley 响应中,您正在将数据填充到数组列表中。现在就这样做。

 ArrayAdapter<String> adapter;
 @Override
 public void onResponse(JSONArray response) {
    try {
         JSONObject jsonobj = response.getJSONObject(0);
         data = jsonobj.getString("Name");
         mylist.add(data);
         String[] nameData = new String[mylist.size()];
         nameData = mylist.toArray(nameData );
          adapter = new ArrayAdapter<String>  
            (MainActivity.this,android.R.layout.select_dialog_item,mylist);              
           AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
           actv.setThreshold(1);//will start working from first character  
           actv.setAdapter(adapter)
           Log.i("here", data);                               
           }                          
          catch (JSONException e) {                            
           e.printStackTrace();

            }
        }

编码愉快!!