我正在使用Geocoder从String中获取匹配的地址。
我正在尝试将返回的地址显示给AutoCompleteTextView。
我可以在Log.i("Result:"," "+list_of_addresses);
由于我们正在讨论动态列表加载,因此我正在调用adapter.NotifyDataSetChanged();
我正在使用addTextChangedListener来监听用户输入的文本中的更改
这就是代码的样子
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) {
}
});
}
}
在for循环中,我无法看到log.i("Addresses:",address.getFeatureName.toString());
但是当我将for循环条件更改为i<=list.size()
时,我确实得到了Log输出,但是应用程序强制关闭了此错误java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
也许这就是我无法看到地址列表的原因?
编辑:在通知数据集更改后,我将for循环条件更改为i<list.size()
并添加了search.showDropDown()
。
任何帮助将不胜感激!三江源。
答案 0 :(得分:0)
你似乎遇到了让它运转起来的问题。我会很好地粘贴对我有用的东西,只有自动完成的相关部分。
<强>的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);
布局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" />
自动完成课程,不要忘记更换API密钥
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;
}
答案 1 :(得分:0)
问题与适配器方法的错误或空重写有关:
@Override
public UPorPackageItem getItem(int index) {
return mValues.get(index);
}
@Override
public int getCount() {
if (mValues == null)
return 0;
return mValues.size();
}
您必须实施上述方法。 它会起作用