将数据传递到RecyclerView适配器

时间:2018-07-25 00:39:49

标签: java android android-recyclerview

我打电话进行改造,响应的大小就是我希望作为RecyclerView适配器的主要数据集传递的大小。我可以确认我已成功致电Retrofit。

我很好奇如何将数据集的大小传递给MyAdapter类。下面是我的代码:

在我的主要活动中:

require_once APPPATH.'vendor/autoload.php';

MyAdapter:

call.enqueue(new Callback<List<Recipe>>() {
        @Override
        public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) {
            Log.v("TAG", String.valueOf(response.isSuccessful()));
            mRecipeListResponse = response.body();
            for (Recipe recipe : mRecipeListResponse){
                Log.v("ID", recipe.getId().toString());
            }

            mAdapter.setDataSet(mRecipeListResponse);
            mRecipeList.setAdapter(mAdapter);
  ;            }

        @Override
        public void onFailure(Call<List<Recipe>> call, Throwable t) {
            Log.v("TAG", t.getMessage());
        }
    });

1 个答案:

答案 0 :(得分:3)

我的猜测是您以空MyAdapter来启动mRecipeDataSet。尝试以下代码。

private List<Recipe> mRecipeDataSet = new ArrayList<Recipe>();

并替换您的代码

//this is wrong, it doesn't do the right thing as the function name suggests.
public List<Recipe> setDataSet(List<Recipe> recipeList){
    return mRecipeDataSet;
}

public void setDataSet(List<Recipe> recipeList){
    mRecipeDataSet = recipeList;
}