猿问

Androidx RecycleView 不显示带有解析服务器的项目

这是我的回收视图适配器


类别适配器类


public class CategoryRAdapter extends RecyclerView.Adapter<CategoryRAdapter.MyViewHolder> {


private Context mcontext;

private List<Food> mData;

RequestOptions option;


public CategoryRAdapter(Context mcontext, List<Food> mData) {

    this.mcontext = mcontext;

    this.mData = mData;


    option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);

}


@Override

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_row, parent, false);


    return new MyViewHolder(view);

}


@Override

public void onBindViewHolder(MyViewHolder holder, int position) {

    Food current = mData.get(position);

    holder.tv_Name.setText(current.getName());

    holder.tv_Rating.setText(current.getRating());

    holder.tv_Descip.setText(current.getDescrip());

    holder.tv_Category.setText(current.getCateg());

    Glide.with(mcontext).load(mData.get(position).getImages()).apply(option).into(holder.img_Thumbnail);

}


@Override

public int getItemCount() {

    return mData.size();

}


public class MyViewHolder extends RecyclerView.ViewHolder {


    TextView tv_Name;

    TextView tv_Rating;

    TextView tv_Descip;

    TextView tv_Category;

    ImageView img_Thumbnail;


    public MyViewHolder(View itemView) {

        super(itemView);


        tv_Name = itemView.findViewById(R.id.food_name);

        tv_Rating = itemView.findViewById(R.id.rating);

        tv_Descip = itemView.findViewById(R.id.desc);

        tv_Category = itemView.findViewById(R.id.category);

        img_Thumbnail = itemView.findViewById(R.id.thumbnail);

    }

}

}

我不知道代码有什么问题,回收视图不工作,它不显示任何项目,如果有人能帮助我并注意到我的代码中有问题,我将不胜感激



慕哥6287543
浏览 151回答 2
2回答

宝慕林4294392

调用adapter.notifyDataSetChanged()您的查询完成回调,这将通知适配器列表已更改。发生这种情况是因为当您设置适配器时,列表是空的,因为查询在后台运行。完成后,您必须告诉适配器新数据已添加到列表中:if (e == null) {&nbsp; &nbsp; for (ParseObject obj : objList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }&nbsp; &nbsp; adapter.notifyDataSetChanged();}此外,在发送查询之前创建您的适配器,否则可能会导致NullPointerException在下一条语句执行之前加载数据(这种可能性很小,但绝对不会影响安全)。dbObjects = new ArrayList<>();adapter = new CategoryRAdapter(this, dbObjects);ParseQuery<ParseObject> query = ParseQuery.getQuery("Category");

莫回无

我建议您使用 RxJava2 来使用异步方法,这太棒了……关于这个问题,当您已经使用 ArrayList 创建适配器时,您的数据可能还没有准备好。尝试将 ) 移动到创建食物对象后的recyclerView.setAdapter(内部。FindCallback
随时随地看视频慕课网APP

相关分类

Java
我要回答