使用通用适配器和放大器时,Recyclerview不显示数据。 viewholder

时间:2018-03-20 08:49:51

标签: java android android-recyclerview android-arrayadapter android-viewholder

在我的android项目中,我创建了Generic RecyclerView的Adapter类& Viewholder类如下,

适配器类,

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类

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;
    }
}

这是我实现上述适配器和放大器的方式。我的活动类中的查看者

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。

按照以下方式,实施recyclerview,

layoutManager = new LinearLayoutManager(this);
dispatchMaterialAdapter =
                new DispatchMaterialAdapter(itemClickListener, dispatchMaterialMds);
            recycleListDetail.setLayoutManager(layoutManager);
            recycleListDetail.setAdapter(dispatchMaterialAdapter);

dispatchMaterialMds是模型类的arraylist,它是从数据库的数据中填充的。

你能帮助我,为什么我的数据没有显示出来?

4 个答案:

答案 0 :(得分:1)

你让BaseViewHolder做了通货膨胀,然后在DispatchViewHolder中重做它,不知道这是否能解决问题,但是重复的代码。

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中:

AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
    super(itemView, itemClickListener);
    //Initiliazation of new view?
}

答案 1 :(得分:0)

更改设置的适配器代码,如下所示。

        recycleListDetail.setLayoutManager(new LinearLayoutManager(this));
    dispatchMaterialAdapter =
            new DispatchMaterialAdapter(itemClickListener, dispatchMaterialMds);
    recycleListDetail.setLayoutManager(layoutManager);
    recycleListDetail.setAdapter(dispatchMaterialAdapter);
    dispatchMaterialAdapter.notifyDataSetChanged();

并在绑定视图中检查你的适配器获取arraylist值并检查arraylist类型以获取和传递给相同或不同的适配器。

答案 2 :(得分:0)

上面的讨论我正在为回收器视图制作演示,并且使用简单的字符串数组正在进行演示。 适配器类。

公共类RecyclerViewAdpater扩展了RecyclerView.Adapter {     列表mStringList = new ArrayList&lt;&gt;(); //听说你可以传递任何pojo类对象。     上下文mContext;

public RecyclerViewAdpater(List<String> mStringList, Context mContext) {
    this.mStringList = mStringList;
    this.mContext = mContext;
}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,parent,false);
    return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    String data=mStringList.get(position); // if you pass object of class then create that class object.
    holder.textView.setText(data);
 }

@Override
public int getItemCount() {
    return mStringList.size();
}

public class ItemViewHolder extends RecyclerView.ViewHolder {
    TextView textView;
    public ItemViewHolder(View itemView) {
        super(itemView);
        textView=itemView.findViewById(R.id.timeData);
    }
}

}

然后在onCreate视图方法中实现类似下面代码的活动...

 recyclerViewAdpater=new RecyclerViewAdpater(mStingList,this);
    mRvData.setLayoutManager(new LinearLayoutManager(this));
    mRvData.setAdapter(recyclerViewAdpater);
    chatAdapter.notifyDataSetChanged();

答案 3 :(得分:-1)

请试试这个

@Override
public int getItemCount() {
    return objectArrayList.size();
}