【发布时间】:2014-08-25 15:53:41
【问题描述】:
我有兴趣创建一个通用的 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 中
提前致谢
【问题讨论】:
-
如果您不想编写自定义适配器,请考虑使用现有库,例如 github.com/ribot/easy-adapter 或 amigold.github.io/FunDapter
-
看到这个Generic adapter
标签: android baseadapter layout-inflater