[英]Android AutoCompleteTextView Refreshing
I am using Geocoder to fetch matching addresses from a String. 我正在使用Geocoder从字符串中获取匹配的地址。
I am trying to display the returned addresses to an AutoCompleteTextView. 我正在尝试将返回的地址显示到AutoCompleteTextView。
I can see the values properly when I Log.i("Result:"," "+list_of_addresses);
当我Log.i("Result:"," "+list_of_addresses);
时,我可以正确看到这些值Log.i("Result:"," "+list_of_addresses);
Since we are talking about dynamic list loading I am calling adapter.NotifyDataSetChanged();
由于我们在谈论动态列表加载,因此我调用adapter.NotifyDataSetChanged();
I am using addTextChangedListener to listen to the changes in the text input by the user 我正在使用addTextChangedListener侦听用户输入的文本中的更改
This is what the code looks like 这就是代码的样子
public class Map extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
final Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());
final ArrayList<String> address_name = new ArrayList<String>();
final AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.search);
search.setThreshold(1);
final ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,address_name);
search.setAdapter(adapter); //moved this line out of the try block
search.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
List<Address> list = null;
Address address = null;
try {
list = gc.getFromLocationName(s.toString(), 10);
Log.i("List:", ""+list); //CAN see this log
} catch (IOException e) {
e.printStackTrace();
}
for(int i=0; i<list.size(); i++){
address = list.get(i);
address_name.add(address.getFeatureName().toString());
Log.i("Address: ", address.getFeatureName().toString()); //CANto see this Log
}
if(!list.isEmpty()){
list.clear();
}
adapter.notifyDataSetChanged();
search.showDropDown();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
}
}
Inside the for loop I am unable to see the log.i("Addresses:",address.getFeatureName.toString());
在for循环中,我看不到log.i("Addresses:",address.getFeatureName.toString());
But when i change the for loop condition to i<=list.size()
I do get the Log output but the application forcecloses with this error java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
但是,当我将for循环条件更改为i<=list.size()
我确实获得了日志输出,但应用程序因以下错误而强制关闭java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
Maybe that's why I am unable to see the Address list? 也许这就是为什么我看不到地址列表的原因?
EDIT: I changed the for loop condition to i<list.size()
also added search.showDropDown()
after notifying dataset changed. 编辑:我将for循环条件更改为i<list.size()
并在通知数据集更改后还添加了search.showDropDown()
。
Any help would be appreciated! 任何帮助,将不胜感激! Thankyou. 谢谢。
You seem to have a problem with getting it to work. 您似乎无法正常使用它。 I'll paste what works for me really well, only relevant sections for AutoComplete. 我会粘贴对我非常有用的内容,仅粘贴AutoComplete的相关部分。
onCreate onCreate
pickUpAutoComplete = new AutoComplete(this, R.layout.pickupautocomplete);
pickUpAutoComplete.setNotifyOnChange(true);
locationText = (AutoCompleteTextView) findViewById(R.id.locationText);
locationText.setOnItemClickListener(this);
locationText.setOnFocusChangeListener(this);
locationText.setAdapter(pickUpAutoComplete);
layout xml 布局xml
<AutoCompleteTextView
android:id="@+id/locationText"
style="@style/registerLargestTextSize"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_weight="6"
android:background="#ffffff"
android:completionThreshold="3"
android:focusable="true"
android:gravity="center"
android:hint="home location"
android:lines="3"
android:maxLines="3"
android:minLines="3"
android:paddingBottom="3dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay"
android:textColor="#b2b2b2"
android:textStyle="bold" />
AutoComplete Class, Don't forget to replace your API KEY 自动完成类,不要忘记替换您的API KEY
public class AutoComplete extends ArrayAdapter<String> implements Filterable {
private static final String LOG_TAG = "carEgiri";
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 = "**YOUR API KEY HERE**";
private ArrayList<String> resultList;
public AutoComplete(IPostAutoCompleteUIChange context,
int textViewResourceId) {
super((Context) context, textViewResourceId);
}
@Override
public int getCount() {
if (resultList == null)
return 0;
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.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;
}
The problem is related to a wrong or empty override of adapter methods: 该问题与适配器方法的错误或空替代有关:
@Override
public UPorPackageItem getItem(int index) {
return mValues.get(index);
}
@Override
public int getCount() {
if (mValues == null)
return 0;
return mValues.size();
}
you must implement the above method. 您必须实现上述方法。 It will works 会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.