问题描述
在我的android项目中,我创建了通用RecyclerView的Adapter类&如下所示的Viewholder类,
In my android project, I created Generic RecyclerView's Adapter class & Viewholder class like below,
适配器类
public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private ItemClickListener itemClickListener;
private List<? extends Object> objectArrayList;
private int layout;
private BaseViewHolder baseViewHolder;
public BaseRecyclerViewAdapter(int layout, ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
this.layout = layout;
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
public BaseRecyclerViewAdapter(BaseViewHolder baseViewHolder, int layout,
ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
this.baseViewHolder = baseViewHolder;
this.layout = layout;
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new BaseViewHolder(itemView, itemClickListener);
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return objectArrayList.size();
}
}
ViewHolder类
ViewHolder class
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private CardView cardView;
private AppCompatImageView imgEdit;
private AppCompatImageView imgDelete;
private ItemClickListener itemClickListener;
public BaseViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView);
cardView = itemView.findViewById(R.id.cardView);
imgEdit = itemView.findViewById(R.id.imgEdit);
imgDelete = itemView.findViewById(R.id.imgDelete);
imgDelete.setOnClickListener(this);
cardView.setOnClickListener(this);
cardView.setOnLongClickListener(this);
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View view) {
if (view.getId() == cardView.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_CLICK);
} else if (view.getId() == imgDelete.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_DELETE);
} else if (view.getId() == imgEdit.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_EDIT);
}
}
@Override
public boolean onLongClick(View view) {
itemClickListener.onLongClick(view, getLayoutPosition());
return false;
}
}
这是我实现上述适配器&的方式我的活动课程中的观看者
Here is the way I implement the above adapter & viewholder in my activity class
private class DispatchViewHolder extends BaseViewHolder {
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
}
}
private class DispatchMaterialAdapter extends BaseRecyclerViewAdapter {
ItemClickListener itemClickListener;
List<? extends Object> objectArrayList;
DispatchMaterialAdapter(ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
super(R.layout.dispatch_material_row, itemClickListener, objectArrayList);
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
@NonNull
@Override
public DispatchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dispatch_material_row, parent, false);
return new DispatchViewHolder(itemView, itemClickListener);
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
try {
DispatchViewHolder dispatchViewHolder = (DispatchViewHolder) holder;
DispatchMaterialMd dispatchMaterialMd = (DispatchMaterialMd) objectArrayList.get(position);
dispatchViewHolder.txtInvoiceNo.setText(dispatchMaterialMd.getInvoiceNo());
dispatchViewHolder.txtVehicleNo.setText(dispatchMaterialMd.getVehicleNo());
dispatchViewHolder.txtPartyName.setText(dispatchMaterialMd.getPartyName());
dispatchViewHolder.txtNoOfBags.setText(dispatchMaterialMd.getNoOfBags());
dispatchViewHolder.txtMaterialType.setText(dispatchMaterialMd.getMaterialType());
dispatchViewHolder.txtWeight.setText(dispatchMaterialMd.getWeight());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return dispatchMaterialMds.size();
}
}
我的objectArrayList列表中有数据.我可以在控制台中打印它,但是recyclerview中什么都没有.在recyclerview中,它仅显示空的Textview.
I have data in my objectArrayList list. I can print it in console, but nothing is coming in recyclerview. In recyclerview, it is only displaying empty Textview.
以下方法,实现recyclerview,
Following way, implement recyclerview,
layoutManager = new LinearLayoutManager(this);
dispatchMaterialAdapter =
new DispatchMaterialAdapter(itemClickListener, dispatchMaterialMds);
recycleListDetail.setLayoutManager(layoutManager);
recycleListDetail.setAdapter(dispatchMaterialAdapter);
dispatchMaterialMds是由数据库数据填充的模型类的数组列表.
dispatchMaterialMds is an arraylist of model class which is filled from the database's data.
请帮我,为什么我的数据没有显示出来?
Can you please help me, why my data is not getting displayed ?
推荐答案
您使执行充气的BaseViewHolder,然后在DispatchViewHolder中重做它,不确定是否能解决问题,但是重复了代码.
You made BaseViewHolder that did the inflation then redone it in the DispatchViewHolder not sure if this will solve the problem but there was repeated code.
private class DispatchViewHolder extends BaseViewHolder {
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
}
}
private class DispatchMaterialAdapter extends BaseRecyclerViewAdapter {
ItemClickListener itemClickListener;
List<? extends Object> objectArrayList;
DispatchMaterialAdapter(ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
super(R.layout.dispatch_material_row, itemClickListener, objectArrayList);
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
@Override
public DispatchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dispatch_material_row, parent, false);
return new DispatchViewHolder(itemView, itemClickListener);
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
try {
DispatchViewHolder dispatchViewHolder = (DispatchViewHolder) holder;
DispatchMaterialMd dispatchMaterialMd = (DispatchMaterialMd) objectArrayList.get(position);
dispatchViewHolder.txtInvoiceNo.setText(dispatchMaterialMd.getInvoiceNo());
dispatchViewHolder.txtVehicleNo.setText(dispatchMaterialMd.getVehicleNo());
dispatchViewHolder.txtPartyName.setText(dispatchMaterialMd.getPartyName());
dispatchViewHolder.txtNoOfBags.setText(dispatchMaterialMd.getNoOfBags());
dispatchViewHolder.txtMaterialType.setText(dispatchMaterialMd.getMaterialType());
dispatchViewHolder.txtWeight.setText(dispatchMaterialMd.getWeight());
} catch (Exception e) {
e.printStackTrace();
}
}
}
在DispatchViewHolder中:
In the DispatchViewHolder:
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
//Initiliazation of new view?
}
这篇关于使用通用适配器&时,Recyclerview不显示数据.观看者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!