Android : Google API 在自定义适配器中

Android : Google API inside a custom adapter

我正在使用一个名为 CustomQuestionAdapter 的自定义适配器来在列表视图中显示问题列表,因为我们知道该适配器从 ArrayAdapter 扩展而来,然后我需要有关我在适配器中的位置的信息,因此我实现了所需的接口( GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener )并实现了它的方法,但是它在重写方法 onStart() 和 onStop() 中显示 "method does not override method from its superclass" 错误,但是当我尝试从AppCompatActivity 而不是 ArrayAdapter 错误消失。

这是我的签名 class :

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

此处出错"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();
    }
}

那么如何正确解决这个问题呢?

要实现 GoogleApiClient.ConnectionCallbacks 和 GoogleApiClient.OnConnectionFailedListener,您必须覆盖这些方法

@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");
}

编辑: onStart() 和 onStop() 方法不属于 arrayAdapter class。 在适配器的构造函数中连接 google api 客户端,或者您可以创建两种方法来连接和断开 googleApiClient 并从您所在的 activity 调用它们设置适配器。 在适配器的构造函数中从 activity

传递所需的值总是一个更好的主意