Android Google Places API - PlaceAutocompleteFragment clear button listener

99封情书 提交于 2020-05-25 04:51:05

问题


I am using Google Places API for Android - PlaceAutocompleteFragment API in my project to search locations, when user selects a location then get the records using location lat-long and display it to ListView.

Now the problem is whenever user taps on clear button (X) of PlaceAutocompleteFragment I want clear ListView items for which I need to listen to clear button onClick? How to do that?


Any help will be appreciated.

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        LatLng latLng=place.getLatLng();
        //hit web service, response parsing and add it to listview.
    }

    @Override
    public void onError(Status status) {
        Log.i(TAG, "An error occurred: " + status);
    }
});

回答1:


Looking at the source code of PlaceAutocompleteFragment I found those view ids and setup a onClickListener like this:

autocompleteFragment.getView().findViewById(R.id.place_autocomplete_clear_button)
    .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // example : way to access view from PlaceAutoCompleteFragment
            // ((EditText) autocompleteFragment.getView()
            // .findViewById(R.id.place_autocomplete_search_input)).setText(""); 
            autocompleteFragment.setText("");
            view.setVisibility(View.GONE);
        }
});



回答2:


The answer above did not solve my problem. I could solve it with a trick :)

    autocompleteFragment.getView().findViewById(R.id.place_autocomplete_clear_button).setPadding(0,0,0,55555);



回答3:


I create the findViewById in the onCreate of the activity, I was getting the following error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Because the fragment is not initialized/visible yet. I moved the listener inside the setOnPlaceSelectedListener and it's working fine now.

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        homeAddressPlaceID = place.getId();
        Log.i(TAG, "Place: " + place.getName() + ", " + place);

        autocompleteFragment.getView().findViewById(R.id.places_autocomplete_clear_button)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        autocompleteFragment.setText("");
                        homeAddressPlaceID = "";
                    }
                });
    }

    @Override
    public void onError(Status status) {
        Log.i(TAG, "An error occurred: " + status);
    }
});


来源:https://stackoverflow.com/questions/36130382/android-google-places-api-placeautocompletefragment-clear-button-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!