placeResult可以給我點擊位置的經度和緯度,但如何獲得列表中顯示的實際地址?感謝幫助隱藏RecyclerView當物品在列表中點擊並獲得點擊地址
公共類MainContentFragment擴展片段實現GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {
protected GoogleApiClient mGoogleApiClient;
private static final LatLngBounds myBounds = new LatLngBounds(
new LatLng(-0, 0), new LatLng(0, 0));
EditText mAutocompleteView;
RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private PlacesAutoCompleteAdapter mAutoCompleteAdapter;
ImageView delete;
public MainContentFragment() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_main_content, container, false);
buildGoogleApiClient();
mAutocompleteView = (EditText)v.findViewById(R.id.autocomplete_tv);
delete = (ImageView)v.findViewById(R.id.clear_text);
mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(getActivity(),
R.layout.searchview_adapter, mGoogleApiClient, myBounds, null);
mRecyclerView = (RecyclerView)v.findViewById(R.id.recycleView);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mAutoCompleteAdapter);
delete.setOnClickListener(this);
mAutocompleteView.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) {
if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
mAutoCompleteAdapter.getFilter().filter(s.toString());
}else if(!mGoogleApiClient.isConnected()){
Toast.makeText(getActivity(), Constants.API_NOT_CONNECTED, Toast.LENGTH_SHORT).show();
Log.e(Constants.PlacesTag, Constants.API_NOT_CONNECTED);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mAutoCompleteAdapter.getItem(position);
final String placeId = String.valueOf(item.placeId);
Log.i("TAG", "Autocomplete item selected: " + item.description);
/*
Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place.
*/
final PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if(places.getCount()==1){
//Do the things here on Click.....
Toast.makeText(getActivity(),String.valueOf(places.get(0).getLatLng()),Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getActivity(),Constants.SOMETHING_WENT_WRONG,Toast.LENGTH_SHORT).show();
}
}
});
Log.i("TAG", "Clicked: " + item.description);
Log.i("TAG", "Called getPlaceById to get Place details for " + item.placeId);
}
})
);
return v;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();
}
@Override
public void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()){
Log.v("Google API","Connecting");
mGoogleApiClient.connect();
}
}
@Override
public void onPause() {
super.onPause();
if(mGoogleApiClient.isConnected()){
Log.v("Google API","Dis-Connecting");
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.v("Google API Callback", "Connection Done");
}
@Override
public void onConnectionSuspended(int i) {
Log.v("Google API Callback", "Connection Suspended");
Log.v("Code", String.valueOf(i));
}
@Override
public void onClick(View v) {
if(v==delete){
mAutocompleteView.setText("");
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.v("Google API Callback","Connection Failed");
Log.v("Error Code", String.valueOf(connectionResult.getErrorCode()));
Toast.makeText(getActivity(), Constants.API_NOT_CONNECTED,Toast.LENGTH_SHORT).show();
}
}
沒關係,我解決了它 – Tuan
那麼你應該回答你是如何解決它,這樣其他人同樣的問題,可以學習到這個崗位解釋。 – Shadow
在OnResult方法中,只需通過調用settext(item.description)來設置文本來編輯文本,以查找您點擊的地址。但是,點擊後,任何人都知道如何隱藏recyclerview? – Tuan