当我收到响应时,我试图在同一类中使用setter方法初始化一个List,但是当我从其他类中调用getter时,它返回一个空列表。从技术上讲,我试图达到的目标是否可行?
public class CatagoryCommonAPI
{
private Context context;
private String endUrl;
private UserAccessToken userAccessToken;
private String ITV_BASE_URL;
private List<CatagoryCommonResponse> itemq;
public CatagoryCommonAPI(Context context,String endUrl, List<CatagoryCommonResponse> itemq)
{
this.context = context;
this.endUrl=endUrl;
this.itemq=itemq;
ITV_BASE_URL=context.getString(R.string.baseUrl);
userAccessToken=new UserAccessToken(context);
}
public void getAllCatagory()
{
Toast.makeText(context, "Iam thanos", Toast.LENGTH_SHORT).show();
String token ="Bearer "+userAccessToken.getAccessToken();
final ITV_API_Service itvApiService=RetrofitClient.getClient(ITV_BASE_URL).create(ITV_API_Service.class);
itvApiService.catagoriesItem(endUrl,token).enqueue(new Callback<List<CatagoryCommonResponse>>() {
@Override
public void onResponse(Call<List<CatagoryCommonResponse>> call, Response<List<CatagoryCommonResponse>> response)
{
if (response.isSuccessful())
{
List<CatagoryCommonResponse> item = response.body();
Toast.makeText(context, "Iam called", Toast.LENGTH_SHORT).show();
setItem(item);
}
}
@Override
public void onFailure(Call<List<CatagoryCommonResponse>> call, Throwable t)
{
Toast.makeText(context, "Image response failed", Toast.LENGTH_SHORT).show();
}
});
}
private void setItem(List<CatagoryCommonResponse> item)
{
this.itemq= item;
Log.d("List size", "setItem: "+item.size());
Log.d("Return List", "setItem: "+itemq.size());
}
public List<CatagoryCommonResponse> getItemq()
{
return this.itemq;
}
}
以及我在哪里叫它
CatagoryCommonAPI commonAPI=new CatagoryCommonAPI(this,endUrl,list);
commonAPI.getAllCatagory();
list=commonAPI.getItemq();
当我使用Toast或Log.d检查list.size()时,它显示0个项目。