How (Re) use GoogleApiClient in recyclerview and adapter

0

I'm using Google Places API for Android to suggest addresses in input fields. This API uses GoogleAPIClient to operate.

The app has a fragment consisting of recyclerview and FAB .

When you touch FAB , the user opens a dialog with form. input. In this form. has a AutoCompleteTextView field to enter the address. This field has a adapter for the GoogleAPIClient and shows suggestions for addresses as the user enters the data.

This part is ready.

As data is entered, recyclerview will display this data on cards. When you click on a card, another dialog is opened, this time for editing the data.

And here's the problem. I am not sure how to use the GoogleAPIClient in the recyclerview adapter.

I tried to create another GoogleAPIClient in the class that extends RecyclerView.Adapter , but did not know how to do it or not.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_recycler, container, false);

    mGoogleApiClient = new GoogleApiClient.Builder(getContext())
                .addApi(Places.GEO_DATA_API)
                .enableAutoManage(getActivity(), GOOGLE_API_CLIENT_ID, this)
                .addConnectionCallbacks(this)
                .build();

    mPlaceArrayAdapter = new PlaceArrayAdapter(getContext(), android.R.layout.simple_list_item_1,
            BOUNDS_AUTOCOMPLETE, null);

This is the% code of the% fragment. I tried to instantiate mGoogleApiClient in onCreateView and onCreateViewHolder enableAutoManage '.

The question is: How do I use GoogleApiClient in a fragment that has recyclerciew and also in events of the items in this same recyclerview?

    
asked by anonymous 03.02.2017 / 18:52

1 answer

0

As there was no answer - I even won a medal for it - and I solved the problem of app , here is my conclusion.

Can not reuse connections with GoogleApiClient . Each class must make its connection, unless the client is created as a static goal. But using static objects is counter-indicated due to the side effects that may occur in thread processing.

Therefore, each application thread must create an API client. And avoid using the enableAutoManage() method by manually controlling the state of the object.

If someone wants to correct me or add more information, feel free to.

    
11.02.2017 / 14:23