Answer the question
In order to leave comments, you need to log in
A class inherited from a generic class how to pass it through a function parameter?
There is a class of generic type:
template<class T = ViewHolder>
class Adapter {
public:
virtual T *onCreateViewHolder(ViewGroup *parent, int viewType) = 0;
virtual void onBindViewHolder(T *holder, int position) = 0;
T *createViewHolder(ViewGroup *parent, int viewType) {
return onCreateViewHolder(parent, viewType);
}
void bindViewHolder(T *holder, int position) {
onBindViewHolder(holder, position);
}
virtual int getItemViewType(int position) { return 0; }
virtual int getItemCount() = 0;
virtual void onViewRecycled(T *holder) {};
}
class MainHolder:public ViewHolder{
public:
MainHolder(View* itemView):ViewHolder(itemView){
pool_id = (TextView*)itemView->findById("pool_id");
}
};
class TestAdapter:public Adapter<MainHolder>
{
public:
TestAdapter(Context* ctx);
MainHolder* onCreateViewHolder(ViewGroup* parent, int viewType) override;
int getItemCount() override;
void onBindViewHolder(MainHolder* holder, int position) override;
~TestAdapter();
};
Adapter *mAdapter = nullptr;
void setAdapter(Adapter *adapter){
mAdapter = adapter;
mAdapter->getItemCount()
}
Adapter<ViewHolder> *mAdapter = nullptr;
void setAdapter(Adapter<ViewHolder> *adapter){
//....
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question