请帮助我如何返回我用于设置数据的数据 在recyclerview中的cardview。 我不明白为什么arraylist大小返回零。请解释。
private List<NotificationData> prepareData() {
data = new ArrayList<>();
data.clear();
SharedPreferences prfs = getSharedPreferences("userdetails", Context.MODE_PRIVATE);
String outletid = prfs.getString("outletid", "");
String authkey = prfs.getString("authkey", "");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://xxxxxx-yyyyy.zzzz.com/api/v1/outlet/cpf-ghh/" + outletid + "/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
FuelwareInterface fuelwareInterface = retrofit.create(FuelwareInterface.class);
Call<ResponseBody> responce_outlets;
Log.d("authkeyssss", "onClick: "+authkey);
responce_outlets = fuelwareInterface.getallindents(authkey);
responce_outlets.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String result = response.body().string();
Log.d("like", "*** " + result);
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonObject1=jsonObject.getJSONArray("data");
Log.d("ghllwnmm", "________"+jsonObject1.length());
int i=jsonObject1.length();
data.clear();
for (int j=0;j<i;j++){
JSONObject jsonObject2=jsonObject1.getJSONObject(j);
NotificationData notificationData = new NotificationData(jsonObject2.getString("vehicle_number"),
jsonObject2.getString("indent_number"),jsonObject2.getString("id"),
jsonObject2.getString("fill_type"),jsonObject2.getString("product"),
jsonObject2.getString("fill_date"),jsonObject2.getString("status"),
jsonObject2.getString("litre"),jsonObject2.getString("amount"));
data.add(notificationData);
}
Log.d("lk000jhgfs", "prepareMovieData: "+data.size());
adapter.notifyDataSetChanged();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}else {
Toast.makeText(getApplicationContext(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("failuremethods", "content " + call.toString());
}
});
Log.d("lkjhgfs", "prepareMovieData: "+data.size());
return data;
}
我想要返回的数据显示我不想要的大小为零。
答案 0 :(得分:1)
问题是return
语句不会等到调用onResponse
方法,所以如果你想获得return 0
,它会立即data
传递一个interface
至method
并使用callback
发布
<强>接口强>
public interface ResultCallback{
void onResult(List<NotificationData> data);
}
方式强>
private void prepareData(ResultCallback callback) { //< make it void
// some code
responce_outlets.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String result = response.body().string();
Log.d("like", "*** " + result);
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonObject1=jsonObject.getJSONArray("data");
Log.d("ghllwnmm", "________"+jsonObject1.length());
int i=jsonObject1.length();
data.clear();
for (int j=0;j<i;j++){
JSONObject jsonObject2=jsonObject1.getJSONObject(j);
NotificationData notificationData = new NotificationData(jsonObject2.getString("vehicle_number"),
jsonObject2.getString("indent_number"),jsonObject2.getString("id"),
jsonObject2.getString("fill_type"),jsonObject2.getString("product"),
jsonObject2.getString("fill_date"),jsonObject2.getString("status"),
jsonObject2.getString("litre"),jsonObject2.getString("amount"));
data.add(notificationData);
}
callback.onResult(data); //<this will push data back to caller
Log.d("lk000jhgfs", "prepareMovieData: "+data.size());
adapter.notifyDataSetChanged();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}else {
Toast.makeText(getApplicationContext(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("failuremethods", "content " + call.toString());
}
});
//some code
}
调用获取数据的方法
prepareData(new ResultCallback(){
@Override
public void onResult(List<NotificationData> data){
// log data.size you will get everything
}
});