gpt4 book ai didi

java - Recyclerview 不显示项目?

转载 作者:行者123 更新时间:2023-11-30 12:02:39 25 4
gpt4 key购买 nike

我想在 RecyclerView 上显示下面的布局。此布局根据商店的数量重复。假设我们有 2 家商店,那么用户将看到这两种布局,具有不同的商店名称。

Screenshot of Android Studio Design Editor on <code>item_checkout_delivery_slots</code> layout

适配器类:

public class DeliverySlotsAdapter extends RecyclerView.Adapter<DeliverySlotsAdapter.ViewHolder> {


private Context context;
LayoutInflater layoutInflater;
List<Object> objects;
private LayoutInflater inflater;
private CallbackListener.SimpleCallback listener;

public DeliverySlotsAdapter(Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
this.inflater = LayoutInflater.from(context);
}

public void setListener(CallbackListener.SimpleCallback listener) {
this.listener = listener;
}

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
RecyclerView.ViewHolder viewHolder;
View view= inflater.inflate(R.layout.item_checkout_delivery_slots, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.bind(objects.get(position));

}

@Override
public int getItemCount() {
return objects.size() != 0 ? objects.size() : 0;
}

class ViewHolder extends RecyclerView.ViewHolder {
// @BindView(R.id.rlSchedule)
// RelativeLayout rlSchedule;
@BindView(R.id.tvDeliveryScheduleLabel)
TextView tvDeliveryScheduleLabel;
// @BindView(R.id.tvDeliverySchedule)
// TextView tvDeliverySchedule;

public ViewHolder(View itemView) {
super(itemView);

itemView.setOnClickListener(v -> {
if (listener!=null && getAdapterPosition() != RecyclerView.NO_POSITION) {
listener.onItemClick(objects.get(getAdapterPosition()));
}
});
}

public void bind(Object object){
DeliverySlotItem deliverySlotItem = (DeliverySlotItem) object;
// DeliverySlotItem item = new DeliverySlotItem();
// Log.e("", "adfdfsf" + object);
// tvDeliveryScheduleLabel.setText(item.shop_name);
tvDeliveryScheduleLabel.setText(deliverySlotItem.shop_name + " Delivery");
}

}

}

tvDeliveryScheduleLabel 正在获取商店名称,但使用以下代码,它返回 null:

tvDeliveryScheduleLabel.setText(deliverySlotItem.shop_name + "Delivery");

这就是我获取值的方式。

Entity area = new Gson().fromJson(Preferences.getString(Prefkey.selected_area), Entity.class);
this.controller.getItems(area.id, new CallbackListener.MultiEntityListener() {
@Override
public void onSuccess(List<Object> data, List<Object> included) {

List<Entity> include = AppUtil.cast(included);
List<Entity> shops = AppUtil.getEntitiesByType("shops", include);


for (Entity shop : shops) {
DeliverySlotItem deliverySlotItem = new DeliverySlotItem();
deliverySlotItem.shop_name = shop.attributes.name;
Preferences.setString(Prefkey.selected_shop, shop.toString());
objects.add(deliverySlotItem);
deliverySlotItems.add(deliverySlotItem);

}
}

@Override
public void onFailed(Object object) {

}
});

deliverySlotsAdapter = new DeliverySlotsAdapter(CheckoutActivity.this, objects);

rv_deliveryslots.setLayoutManager(new LinearLayoutManager(CheckoutActivity.this));
rv_deliveryslots.setAdapter(deliverySlotsAdapter);

最佳答案

在您的 ViewHolder 类中添加此进行这些更改 -

    public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, view); // To bind view with ButterKnife

itemView.setOnClickListener(v -> {
if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {
listener.onItemClick(objects.get(getAdapterPosition()));
}
});
}

关于java - Recyclerview 不显示项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com