Abdenaceur Lichiheb Abdenaceur Lichiheb - 1 month ago 9
Android Question

Android : Google API inside a custom adapter

I'm using a custom adapter called CustomQuestionAdapter, to display a list of question inside a listview, as we know the adapter extends from an ArrayAdapter, then i needed info about my location inside the adapter so i implemented the needed interfaces ( GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener ) and implemented it's methods, however it show me an error saying "method does not override method from its superclass" in the Overrided methods onStart() and onStop() , but when i tried to extends from AppCompatActivity instead of ArrayAdapter the error disappear.

This is the signature of my class :

public class CustomQuestionAdapter extends ArrayAdapter<Question> implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener


Error here "method does not override method from its superclass"

@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}

@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}


So how to fix this properly ?

Isj Isj
Answer

For implementing GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener, you have to override these methods

@Override
public void onConnected(@Nullable Bundle bundle) {

   Log.d("isj","connected");
}

@Override
public void onConnectionSuspended(int i) {

    Log.d("isj","connection susspended");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    Log.d("isj","connection failed");
}

EDIT: onStart() and onStop() methods dont belong to the arrayAdapter class. Connect the google api client in the constructor of your adapter or you could create two methods to connect and disconnect the googleApiClient and call them from the activity in which you are setting the adapter.