【问题标题】:hide RecyclerView when item clicked in the list and get the clicked address在列表中单击项目时隐藏 RecyclerView 并获取单击的地址
【发布时间】:2016-09-16 06:20:42
【问题描述】:

placeResult 可以给我点击位置的纬度和经度,但是如何获取列表中显示的实际地址?感谢帮助

公共类 MainContentFragment 扩展 Fragment 实现 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();
}

}

【问题讨论】:

  • 没关系,我解决了
  • 那么你应该在这篇文章中添加一个答案,解释你是如何解决它的,以便其他有同样问题的人可以向你学习。
  • 在 OnResult 方法中,只需通过调用 settext(item.description) 将文本设置为 edittext 即可检索您单击的地址。但是有谁知道我点击后如何隐藏recyclerview?

标签: android android-recyclerview google-places-api


【解决方案1】:

使用以下方法从Place 中检索数据:

getName() – 地点名称。

getAddress() – 地点的地址,采用人类可读的格式。

getID() – 地点的文本标识符。在本页的其余部分阅读有关地点 ID 的更多信息。

getPhoneNumber() – 该地点的电话号码。

getWebsiteUri() – 地点网站的 URI(如果已知)。这是由与该地点相关的企业或其他实体维护的网站。如果没有网站,则返回 null。

getLatLng() – 地点的地理位置,指定为经纬度坐标。

getViewport() – 视口,以 LatLngBounds 对象形式返回,可用于在地图上显示该地点。如果地点的大小未知,则可能返回 null。

getLocale() – 名称和地址已本地化的语言环境。 getPlaceTypes() – 代表该地点的地点类型列表。有关可用地点类型的列表,请参阅地点界面的文档。

getPriceLevel() – 这个地方的价格水平,以整数形式返回,值范围从 0(最便宜)到 4(最贵)。

getRating() – 地点的综合评分,以浮点数形式返回,值范围为 1.0 到 5.0,基于汇总的用户评论。

在这里https://developers.google.com/places/android-api/place-details了解更多详情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多