我有兴趣创建一个通用的BaseAdapter,它将应用于任何具有任何list_item的ListView.它将为list_view设置item_row.
public class GenericAdapter extends BaseAdapter { Context context; ArrayList<HashMap<String,String>> list; LayoutInflater inflater; public GenericAdapter (Context context,ArrayList<HashMap<String,String>> list) { this.context = context; this.list = list; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { View view = convertView; if(view == null) { view = inflater.inflate(R.layout.item_of_any_list,parent,false); // i have to do something here like there may be a method which must be call } /*HashMap<String,String> map = list.get(position); TextView textViewName = (TextView) view.findViewById(R.id.name); TextView textViewDate = (TextView) view.findViewById(R.id.date); //TextView textViewDesc = (TextView) view.findViewById(R.id.desc); textViewName.setText(map.get("name")); textViewDate.setText(map.get("date")); //textViewDesc.setText(map.get("description")); */ return view; }
我想我必须创建一个接口,为单个项目设置item_row.但我不知道这可能是可能的.有人能告诉我如何实现它.
我的想法是创建一个接口,GenericAdapter实现该接口.在该界面中,有一种获取和设置item_row的方法.任何扩展GenericAdapter的类都必须实现该方法.它将帮助我们制作通用适配器.
当我们扩展该适配器时,我们不需要编写所有代码,如getCount,getItem,getItemId,我们只需要覆盖一个inflateLayout函数.必须在getView中
提前致谢
解决方法@H_301_14@
两年后回答我自己的问题.
为了制作通用适配器,您需要父类中的抽象方法,这些方法应该由子实现.因此,GenericAdapter必须是Abstract,并为要更改的数据创建抽象方法.
我有不同类型的ArrayList在Adapter中显示它的模型类是不同的.所以我决定写一个genericAdapter.如果someOne对listview感兴趣,这适用于recyclerview.让我知道.
GenericAdapter
public abstract class GenericAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ArrayList<T> items;
public abstract RecyclerView.ViewHolder setViewHolder(ViewGroup parent);
public abstract void onBindData(RecyclerView.ViewHolder holder,T val);
public GenericAdapter(Context context,ArrayList<T> items){
this.context = context;
this.items = items;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
RecyclerView.ViewHolder holder = setViewHolder(parent);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) {
onBindData(holder,items.get(position));
}
@Override
public int getItemCount() {
return items.size();
}
public void addItems( ArrayList<T> savedCardItemz){
items = savedCardItemz;
this.notifyDataSetChanged();
}
public T getItem(int position){
return items.get(position);
}
}
而且我喜欢它
adapter = new GenericAdapter<DataModel>(context,modelList) {
@Override
public RecyclerView.ViewHolder setViewHolder(ViewGroup parent) {
final View view = LayoutInflater.from(context).inflate(R.layout.item_view_holder,false);
ItemViewHolder viewHolder = new ItemViewHolder(context,view);
return viewHolder;
}
@Override
public void onBindData(RecyclerView.ViewHolder holder1,DataModel val) {
DataModel userModel = val;
ItemViewHolder holder = (ItemViewHolder)holder1;
holder.name.setText(userModel.getName());
holder.fatherName.setText(userModel.getFatherName());
}
};
mRecyclerView.setAdapter(adapter);
为了制作通用适配器,您需要父类中的抽象方法,这些方法应该由子实现.因此,GenericAdapter必须是Abstract,并为要更改的数据创建抽象方法.
我有不同类型的ArrayList在Adapter中显示它的模型类是不同的.所以我决定写一个genericAdapter.如果someOne对listview感兴趣,这适用于recyclerview.让我知道.
GenericAdapter
public abstract class GenericAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private ArrayList<T> items; public abstract RecyclerView.ViewHolder setViewHolder(ViewGroup parent); public abstract void onBindData(RecyclerView.ViewHolder holder,T val); public GenericAdapter(Context context,ArrayList<T> items){ this.context = context; this.items = items; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { RecyclerView.ViewHolder holder = setViewHolder(parent); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) { onBindData(holder,items.get(position)); } @Override public int getItemCount() { return items.size(); } public void addItems( ArrayList<T> savedCardItemz){ items = savedCardItemz; this.notifyDataSetChanged(); } public T getItem(int position){ return items.get(position); } }
而且我喜欢它
adapter = new GenericAdapter<DataModel>(context,modelList) { @Override public RecyclerView.ViewHolder setViewHolder(ViewGroup parent) { final View view = LayoutInflater.from(context).inflate(R.layout.item_view_holder,false); ItemViewHolder viewHolder = new ItemViewHolder(context,view); return viewHolder; } @Override public void onBindData(RecyclerView.ViewHolder holder1,DataModel val) { DataModel userModel = val; ItemViewHolder holder = (ItemViewHolder)holder1; holder.name.setText(userModel.getName()); holder.fatherName.setText(userModel.getFatherName()); } }; mRecyclerView.setAdapter(adapter);