我正在尝试将header
添加到我的recylerview中,以使其看起来像<thead>
(在HTML表中)。因此,我正在关注本教程https://en.proft.me/2017/11/25/how-add-custom-header-recylerview/
这是我的适配器
public class Sales_header_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Sales_header_model> salesheaderlist;
public Sales_header_adapter(List<Sales_header_model> salesheaderlist) {
this.salesheaderlist = salesheaderlist;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == ListHeader.TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_header_rec_header, parent, false);
return new VHHeader(v);
} else if(viewType == ListHeader.TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_header_rec, parent, false);
return new VHItem(v);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof VHHeader) {
Header header = salesheaderlist.get(position);
VHHeader VHheader = (VHHeader)holder;
VHheader.nonota_h.setText(header.getNoNota());
VHheader.tanggal_h.setText(header.getTanggalTransaksi());
VHheader.total_h.setText("Rp. 0");
VHheader.outlet_h.setText(header.getOutletCode());
} else if(holder instanceof VHItem) {
Sales_header_model as = salesheaderlist.get(position);
VHItem VHitem = (VHItem)holder;
VHitem.nonota.setText(as.getNoNota());
VHitem.tanggal.setText(as.getTanggalTransaksi());
VHitem.total.setText("Rp. 0");
VHitem.outlet.setText(as.getOutletCode());
}
}
class VHHeader extends RecyclerView.ViewHolder{
public TextView outlet_h, tanggal_h, total_h,nonota_h;
public VHHeader(View itemView) {
super(itemView);
outlet_h = itemView.findViewById(R.id.tanggalnota);
tanggal_h = itemView.findViewById(R.id.total);
total_h = itemView.findViewById(R.id.nonota);
nonota_h = itemView.findViewById(R.id.outletcode);
}
}
class VHItem extends RecyclerView.ViewHolder{
public TextView outlet, tanggal, total,nonota;
public VHItem(View itemView) {
super(itemView);
tanggal = itemView.findViewById(R.id.tanggalnota);
total = itemView.findViewById(R.id.total);
nonota = itemView.findViewById(R.id.nonota);
outlet = itemView.findViewById(R.id.outletcode);
}
}
@Override
public int getItemViewType(int position) {
return salesheaderlist.get(position).getItemType();
}
@Override
public int getItemCount() {
return salesheaderlist.size();
}
}
这是我的
sales_header_model
public class Sales_header_model implements ListHeader {
String OutletCode,TanggalTransaksi,NoNota,CreatedBy,Seller;
String CreatedDate;
Integer Status;
public Sales_header_model(String outletCode, String tanggalTransaksi, String noNota, String createdby, String Seller, String CreatedDate, Integer Status) {
this.OutletCode = outletCode;
this.TanggalTransaksi = tanggalTransaksi;
this.NoNota = noNota;
this.CreatedBy = createdby;
this.Status = Status;
this.Seller = Seller;
this.CreatedDate = CreatedDate;
}
public String getOutletCode() {
return OutletCode;
}
public void setOutletCode(String outletCode) {
OutletCode = outletCode;
}
public String getTanggalTransaksi() {
return TanggalTransaksi;
}
public void setTanggalTransaksi(String tanggalTransaksi) {
TanggalTransaksi = tanggalTransaksi;
}
public String getNoNota() {
return NoNota;
}
public void setNoNota(String noNota) {
NoNota = noNota;
}
public String getCreatedBy() {
return CreatedBy;
}
public void setCreatedBy(String createdBy) {
CreatedBy = createdBy;
}
public String getSeller() {
return Seller;
}
public void setSeller(String seller) {
Seller = seller;
}
public String getCreatedDate() {
return CreatedDate;
}
public void setCreatedDate(String createdDate) {
CreatedDate = createdDate;
}
public Integer getStatus() {
return Status;
}
public void setStatus(Integer status) {
Status = status;
}
public Sales_header_model() {
}
@Override
public int getItemType() {
return ListHeader.TYPE_HEADER;
}
}
我的
header
班
public class Header implements ListHeader {
String OutletCode,TanggalTransaksi,NoNota,Seller;
public String getOutletCode() {
return OutletCode;
}
public void setOutletCode(String outletCode) {
OutletCode = outletCode;
}
public String getTanggalTransaksi() {
return TanggalTransaksi;
}
public void setTanggalTransaksi(String tanggalTransaksi) {
TanggalTransaksi = tanggalTransaksi;
}
public String getNoNota() {
return NoNota;
}
public void setNoNota(String noNota) {
NoNota = noNota;
}
public String getSeller() {
return Seller;
}
public void setSeller(String seller) {
Seller = seller;
}
@Override
public int getItemType() {
return ListHeader.TYPE_HEADER;
}
}
和听众
ListHeader
public interface ListHeader {
int TYPE_ITEM = 0;
int TYPE_HEADER = 1;
int getItemType();
}
错误部分在
if(holder instanceof VHHeader) {
Header header = salesheaderlist.get(position);
VHHeader VHheader = (VHHeader)holder;
VHheader.nonota_h.setText(header.getNoNota());
VHheader.tanggal_h.setText(header.getTanggalTransaksi());
VHheader.total_h.setText("Rp. 0");
VHheader.outlet_h.setText(header.getOutletCode());
}
这是我的错误信息
错误:类型不兼容:Sales_header_model无法转换为Header
我该如何解决?预先感谢
答案 0 :(得分:1)
您错过了本教程中编写的铸造零件。我已经添加了您需要更改的注释。
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof VHHeader) {
//cast the item to respective type
Header header = (Header) salesheaderlist.get(position);
VHHeader VHheader = (VHHeader)holder;
VHheader.nonota_h.setText(header.getNoNota());
VHheader.tanggal_h.setText(header.getTanggalTransaksi());
VHheader.total_h.setText("Rp. 0");
VHheader.outlet_h.setText(header.getOutletCode());
} else if(holder instanceof VHItem) {
//cast the item to respective type
Sales_header_model as = (Sales_header_model) salesheaderlist.get(position);
VHItem VHitem = (VHItem)holder;
VHitem.nonota.setText(as.getNoNota());
VHitem.tanggal.setText(as.getTanggalTransaksi());
VHitem.total.setText("Rp. 0");
VHitem.outlet.setText(as.getOutletCode());
}
}