How to (Re)use Googleapiclient in recyclerview and Adapter

Asked

Viewed 42 times

0

I’m using Google Places API for Android for suggestion of addresses in input fields. This API uses Googleapiclient to operate.

The app has a fragment composed of a recyclerview and a FAB.

On touching the FAB, the user opens a dialog with form. input. In this form. has a field AutoCompleteTextView to enter the address. This field has a Adapter to the Googleapiclient and displays address suggestions as the user enters the data.

This part is ready.

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

And here’s the problem. I don’t know how to use the Googleapiclient in the recyclerview Adapter.

I tried to create another Googleapiclient in the class extending RecyclerView.Adapter, but did not know how to do or is not around.

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 onCreateView from Fragment. I tried to instantiate mGoogleApiClient on onCreateViewHolder and in the onBindViewHolder, ele não aceita nada como 1º parametro deenableAutoManage`.

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

1 answer

0


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

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.