Ich brauche etwas Hilfe mit Generics, das Ziel ist es, eine onSuccess/onError Callbacks, die ein Objekt als Argumente erhalten, der Objekttyp wird beim Erstellen der anonymen Klasse, die die onSuccess umschließt gesetzt/onFehlerfunktionen.Funktion Argument Generika in anonyme Klasseninstanz
Die Schnittstelle
public interface OnDataResponse<T> {
void onSuccess(T response);
void onError(String data, ResponseError error);
}
Der Aufruf, der den Hörer erzeugt, das Argument Typ onSuccess Einstellung CreateUserResponse
createUser(new Operation.OnDataResponse<CreateUserResponse>() {
@Override
public void onSuccess(CreateUserResponse response) {
}
@Override
public void onError(String data, ResponseError error) {
}
};)
Umsetzung create()
public void createUser(OnDataResponse responseListener) {
//performing the task asynchronously
String data
// if successfull, create an object of type T
//(CreateUserResponse in this case)
// try to parse data from json to object
//create instance of T
T tInstance = new Gson().fromJson(data, type);
// call the listener, providing the expected object
responseListener.onSuccess(tInstance)
}
sein T ist undefiniert in Seite createUser()
Irgendwelche Vorschläge zur Verbesserung und zum Funktionieren?
Versuchen Sie es mit der folgenden Signatur: 'public Leere create (OnDataResponse Response, Klasse Typ)'. –
sp00m