如何仅在用户输入文本时才能获得自动完成结果

时间:2016-03-11 08:47:18

标签: android autocomplete

这听起来很简单但对我来说很难。我有一个显示用户位置地址的自动完成文本视图。如果用户输入的位置不同,我还实现了一个地方api来获取地址。一切都按照预期的方式工作,但即使已有地址,地方结果仍然显示。为了降低成本,我希望仅在用户输入地址时才能获得地址结果。我创建了一个全局布尔值,并在文本更改时将其设置为true:

autocomplete.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                isTextEntered = true;  //to get autocomplete get results only after user enters text
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

然后我在设置我的适配器时检查布尔值是否为真:

if (isTextEntered) {
            autocomplete.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.search_results_list_item, R.id.tvSearchResultItem));
            autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    final String selectedPlace = (String) parent.getItemAtPosition(position);
                    autocomplete.setText(selectedPlace);
                }
            });
        }

但是在我的活动的oncreate方法中执行此操作只会阻止自动完成显示任何位置提示结果。我怎样才能在这里实现我的目标?一如既往,我们非常感谢任何帮助。

这是我的自定义适配器类:

class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {

        private ArrayList resultList;
        //private ArrayList<HashMap<String, Place>> results = new ArrayList<>();

        public GooglePlacesAutocompleteAdapter(Context context, int list, int textViewResourceId) {

            super(context, list, textViewResourceId);
        }

        @Override
        public int getCount() {
            return resultList.size();
        }

        @Override
        public String getItem(int index) {
            return resultList.get(index).toString();
        }

        //@Override
        //public HashMap<String, Place>  getItem(int index) {return results.get(index);}

        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        resultList = autocomplete(constraint.toString());
                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };
            return filter;
        }
    }

2 个答案:

答案 0 :(得分:1)

我的想法是实现这一目标。

complete.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                  // 1. init autocomplete 
                  // 2. show autocomplete
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               // 1. get enter text
               // 2. update the datasource of autocomplete adapter
            }

            @Override
            public void afterTextChanged(Editable s) {
               // hide the autocomplete
            }
        });

希望有所帮助

答案 1 :(得分:0)

我建议你只是将它重构为像这样的方法

public void setSelectedPlace() {
            autocomplete.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.search_results_list_item, R.id.tvSearchResultItem));
            autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    final String selectedPlace = (String) parent.getItemAtPosition(position);
                    autocomplete.setText(selectedPlace);
                }
            });
  }

并在此处afterTextChanged方法中调用它,并在此之后隐藏自动完成

@Override
 public void afterTextChanged(Editable s) {
     setSelectedPlace();
  // hide the autocomplete
 }