我的应用中有一个自动完成文本视图,可让用户输入地址,textview使用Places Api向他们显示以用户正在使用的字母开头的不同位置。我实现了Places Api并且用户能够看到地点,但是当他们选择一个地方时我想要检索那个地方并根据我的需要使用它(比如检索LatLng,以及类似的东西)但是place对象是String对象,我不知道如何将其检索为Place或将String对象转换为Place对象。我到目前为止尝试了以下无济于事。 这是我的适配器类。
class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {
private ArrayList resultList;
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 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;
}
}
msg.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.search_results_list_item, R.id.tvSearchResultItem));
msg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String selectedPlace = (String) parent.getItemAtPosition(position);
Place pl = new Place() {
@Override
public String getId() {
return null;
}
@Override
public List<Integer> getPlaceTypes() {
return null;
}
@Override
public CharSequence getAddress() {
return null;
}
@Override
public Locale getLocale() {
return null;
}
@Override
public CharSequence getName() {
return selectedPlace;
}
@Override
public LatLng getLatLng() {
return null;
}
@Override
public LatLngBounds getViewport() {
return null;
}
@Override
public Uri getWebsiteUri() {
return null;
}
@Override
public CharSequence getPhoneNumber() {
return null;
}
@Override
public float getRating() {
return 0;
}
@Override
public int getPriceLevel() {
return 0;
}
@Override
public Place freeze() {
return null;
}
@Override
public boolean isDataValid() {
return true;
}
};
LatLng selectedltlng = getSelectedLatLng(pl);
Log.e("selected", selectedltlng.toString());
//Place selectedPlace = Places.GeoDataApi.getPlaceById(mGoogleApiClient, );
//Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
private LatLng getSelectedLatLng(Place placeStringe) {
LatLng placeltlg = placeStringe.getLatLng();
Log.e("selcete", placeltlg.toString());
return placeltlg;
}
});
但是这会引发空指针异常。 我也尝试将字符串对象像这样
转换为ObjectObject selectedPlace = (Object) parent.getItemAtPosition(position);
Place plac = (Place)selectedPlace;
但这引发了施法异常。我怎样才能以有效的方式做到这一点?
答案 0 :(得分:0)
我一周前成了城市选择者。它使用自定义扩展的AutoCompleteTextView小部件。可能这对你有所帮助。此小组件允许用户输入字符(灰色),然后显示匹配城市的列表。用户从列表中选择一个城市(变为黑色),然后选择保存在cityData对象中的城市,并可以通过.getCity()调用进行恢复。
以下是代码:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
switch (status) {
case NotReachable: {
NSLog(@"no internet connection");
break;
}
case ReachableViaWiFi: {
NSLog(@"wifi");
break;
}
case ReachableViaWWAN: {
NSLog(@"cellurar");
break;
}
}
completionHandler(YES);
}
适配器:
public class CityAutoCompleteEditText extends AutoCompleteTextView implements View.OnFocusChangeListener, TextWatcherAdapter.TextWatcherListener {
// City object (code, name, type, level and so on)
private OKTMOUnit cityData;
// One of the constructors. You need to implement all with init() inside
public CityAutoCompleteEditText(Context context) {
super(context);
init(context, null);
}
// Initial data setter
public void setCity(OKTMOUnit city) {
setText(city.toString());
setTextColor(resource_black);
cityData = city;
}
// Data getter
public OKTMOUnit getCity() {
return cityData;
}
private void init(Context context, AttributeSet attrs) {
......
super.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// On click - set color black and init cityData
cityData = (OKTMOUnit) getAdapter().getItem(position);
setTextColor(resource_black);
}
});
.....
}
@Override
public void onTextChanged(EditText view, String text) {
// Text changed by hand - grey it and clear cityData
cityData = null;
setTextColor(resource_grey);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (cityData == null) {
setText("");
}
}
}
ADD:我从适配器中删除了public class CityAutoCompleteAdapter extends BaseAdapter implements Filterable {
private static final int MAX_RESULTS = 20;
private Context mContext;
private ArrayList<OKTMOUnit> mData;
public CityAutoCompleteAdapter(Context context) {
mData = new ArrayList<>();
mContext = context;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public OKTMOUnit getItem(int index) {
return mData.get(index);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.autocomplete_dropdown_item, parent, false);
}
if (convertView != null) {
((TextView) convertView.findViewById(R.id.autoCompleteCity)).setText(getItem(position).getFullCity());
((TextView) convertView.findViewById(R.id.autoCompleteRegion)).setText(getItem(position).getFullRegion());
}
return convertView;
}
}
代码以简化它。
答案 1 :(得分:0)
试试这个用模型类完成你可以得到地名,地址,纬度,我觉得很有用http://coderzpassion.com/android-working-google-places-api/