亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數據傳遞給 RecyclerView 適配器

將數據傳遞給 RecyclerView 適配器

江戶川亂折騰 2021-06-07 17:24:08
我調用了改造,響應的大小就是我想作為 RecyclerView 適配器的主要數據集傳遞的。我可以確認我已成功調用 Retrofit。我很好奇如何將數據集的大小傳遞給 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());        }    });我的適配器:public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {private List<Recipe> mRecipeDataSet;public class ViewHolder extends RecyclerView.ViewHolder{    protected CardView mCardView;    protected TextView mTextView;    public ViewHolder(View itemView) {        super(itemView);        mCardView = itemView.findViewById(R.id.recipe_cardview);        mTextView = itemView.findViewById(R.id.id_of_recipe_item);    }}@NonNull@Overridepublic MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item, parent,false);    return new ViewHolder(v);}@Overridepublic void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {        String id = mRecipeDataSet.get(position).getId();        holder.mTextView.setText(id);}@Overridepublic int getItemCount() {    return mRecipeDataSet.size();}public List<Recipe> setDataSet(List<Recipe> recipeList){    return mRecipeDataSet;}public List<Recipe> getmRecipeDataSet() {    return mRecipeDataSet;}}
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

我的猜測是你MyAdapter用 null啟動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;

}


查看完整回答
反對 回復 2021-06-10
  • 1 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號