自动完成 TextView 在 1 个字符后不显示下拉列表

Auto Complete TextView not showing drop down after 1 character

我正在使用自动完成 textview 来显示 google 个地方 api.I 也将阈值设置为 1 如果用户键入一个字符,下拉菜单会出现 occur.It当用户键入 2 character.I 不知道为什么会这样 happening.Please 请帮帮我

代码

public class GetPlaces extends AsyncTask<String, String, String> {

    private Context context;
    private GetPlaces getPlaces;

    private AutoCompleteTextView autoCompleteTextView;

    private ProgressBar progressBar;

    private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
    private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
    private static final String OUT_JSON = "/json";

    private static final String API_KEY = "key=MY KEY";

    private String INPUT = "INPUT";

    private HttpURLConnection urlConnection = null;
    private StringBuilder stringBuilder = null;
    private URL url;
    private InputStream iStream = null;

    private double latitude;
    private double longitude;

    private ArrayAdapter<String> adapter;

    private JSONObject jsonObject = null;
    private JSONArray jsonArray = null;

    private ArrayList<String> address = null;


    public GetPlaces(Context context, String INPUT, AutoCompleteTextView autoCompleteTextView, ProgressBar progressBar, double latitude, double longitude) {
        this.context = context;
        this.INPUT = INPUT;
        this.autoCompleteTextView = autoCompleteTextView;
        this.progressBar = progressBar;
        this.latitude = latitude;
        this.longitude = longitude;
        address = new ArrayList<>();
        this.execute(INPUT);


    }

    @Override
    protected String doInBackground(String... params) {

        String response = "";

        try {
            INPUT = "input=" + URLEncoder.encode(params[0], "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        }


        String types = "types=geocode";

        // Sensor enabled
        String sensor = "sensor=false";

        String parameters = "";


        if (latitude != 0 && longitude != 0) {
            // Building the parameters for destionation to the web service
            parameters = INPUT + "&" + types + "&location=" + latitude + "," + longitude + "&radius=500" + "&" + API_KEY + "&" + sensor;
        } else {
            // Building the parameters for source to the web service
            parameters = INPUT + "&" + types + "&" + API_KEY + "&" + sensor;
        }


        stringBuilder = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        stringBuilder.append("?" + parameters);


        //connections is established here
        try {
            url = new URL(stringBuilder.toString());

            urlConnection = (HttpURLConnection) url.openConnection();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb = new StringBuffer();

            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            response = sb.toString();
            Log.e("Response", response);

            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                iStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            urlConnection.disconnect();
        }

        return response;
    }

    @Override
    protected void onPostExecute(String s) {


        try {
            jsonObject = new JSONObject(s);

            if (jsonObject.get("predictions") instanceof JSONObject) {


            } else if (jsonObject.get("predictions") instanceof JSONArray) {
                jsonArray = jsonObject.getJSONArray("predictions");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    String addressValue = object.getString("description");
                    address.add(addressValue);
                }


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

        if (address != null) {
            adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, address);
            autoCompleteTextView.setThreshold(1);
            autoCompleteTextView.setAdapter(adapter);
            progressBar.setVisibility(View.GONE);
        }
    }
}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0dffb7">


    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/act_get_source"
        android:layout_margin="10dp"
        android:hint="SOURCE"
        android:gravity="center"
        android:completionThreshold="1" />


    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/act_get_destination"
        android:layout_margin="10dp"
        android:hint="DESTINSTION"
        android:layout_below="@+id/act_get_source"
        android:gravity="center"
        android:completionThreshold="1" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="Clear"
        android:visibility="gone"
        android:id="@+id/bt_source_clear"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/act_get_source"
        android:layout_alignEnd="@+id/act_get_source" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="Clear"
        android:id="@+id/bt_destination_clear"
        android:visibility="gone"
        android:layout_below="@+id/act_get_source"
        android:layout_alignRight="@+id/act_get_destination"
        android:layout_alignEnd="@+id/act_get_destination" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pb_source"
        android:visibility="gone"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/act_get_source"
        android:layout_alignStart="@+id/act_get_source"
        android:layout_alignBottom="@+id/act_get_source" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pb_destination"
        android:visibility="gone"
        android:layout_below="@+id/act_get_source"
        android:layout_alignLeft="@+id/act_get_destination"
        android:layout_alignStart="@+id/act_get_destination" />


</RelativeLayout>

在 onPostExecute 中试试这个

  if (address != null) {
        adapter = new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line, address);
        autoCompleteTextView.setThreshold(1);
        autoCompleteTextView.setAdapter(adapter);
        progressBar.setVisibility(View.GONE);
        autoCompleteTextView.showDropDown();
    }