gpt4 book ai didi

java - 资源类和 null 参数

转载 作者:行者123 更新时间:2023-12-02 10:00:09 25 4
gpt4 key购买 nike

我有一个可变的实时数据:

private MutableLiveData<Resource<LoginResponse>> loginRequestData = new MutableLiveData<>();

我正在使用 Google 提供的资源类:

public class Resource<T> {

@NonNull
public final Status status;

@Nullable
public final T data;

@Nullable
public final String message;

public Resource(@NonNull Status status, @Nullable T data, @Nullable String message) {
this.status = status;
this.data = data;
this.message = message;
}

public static <T> Resource<T> success(@NonNull T data) {
return new Resource<>(Status.SUCCESS, data, null);
}

public static <T> Resource<T> error(@NonNull String msg, @Nullable T data) {
return new Resource<>(Status.ERROR, data, msg);
}

public static <T> Resource<T> loading(@Nullable T data) {
return new Resource<>(Status.LOADING, data, null);
}

public enum Status { SUCCESS, ERROR, LOADING}
}

我的LoginResponse类:

public class LoginResponse {

@SerializedName("status")
@Expose
private int status;

@SerializedName("key")
@Expose
private String key;

@SerializedName("anonid")
@Expose
private String anonid;

@SerializedName("username")
@Expose
private String username;

@SerializedName("photo")
@Expose
private String photo;

@SerializedName("isBanned")
@Expose
private int isBanned;


public int getStatus() {
return status;
}

public String getKey() {
return key;
}

public String getAnonid() {
return anonid;
}

public String getUsername() {
return username;
}

public String getPhoto() {
return photo;
}

public int getIsBanned() {
return isBanned;
}
}

正如您所猜测的,我正在使用此实时数据进行网络请求。在网络请求之前,我想通过以下行将其设置为加载模式:

loginRequestData.setValue(Resource.loading(null));

但是我收到以下编译器错误:

error: no suitable method found for setValue(Resource) method LiveData.setValue(Resource) is not applicable (argument mismatch; Resource cannot be converted to Resource) method MutableLiveData.setValue(Resource) is not applicable (argument mismatch; Resource cannot be converted to Resource)

为什么我无法为 Resource.loading 方法传递 null 参数?

最佳答案

LiveData您定义了期望 Resource<LoginResponse> 的实例,不是 Resource<?> 的实例,这就是您所提供的。试试这个:

loginRequestData.setValue(Resource.<LoginResponse>loading(null));

这样,您就可以确保 null Resource您使用loading加载方法的类型为Resource<LoginResponse> .

关于java - 资源类和 null 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55696560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com