在arraylist data.size()中显示我想要返回的0

时间:2018-04-04 08:53:40

标签: android android-recyclerview retrofit

请帮助我如何返回我用于设置数据的数据     在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;
}

我想要返回的数据显示我不想要的大小为零。

1 个答案:

答案 0 :(得分:1)

问题是return语句不会等到调用onResponse方法,所以如果你想获得return 0,它会立即data传递一个interfacemethod并使用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
   }
});