没有自定义项的自定义 ArrayAdapter data-type
Custom ArrayAdapter without custom item data-type
我尝试在每个项目上实现一个带有特定图标、标题和副标题的 ListView
。
所有项目的数据都在 ArrayList
的 objects 中,来自以下 class
:
class ItemObject{
String title="";
String subTitle="";
String unit="";
int icon;
int quantity;
int parentID;
int orderInList;
}
ArrayList<ItemObject> listViewData;
这是我首先使用的代码:
class ExtendedArrayAdapter<String> extends ArrayAdapter<String>{
private Context context;
private String[] items;
public ExtendedArrayAdapter(Context context, String[] items){
super(context,-1,items);
this.context = context;
this.items=items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.lv_item_bg,parent,false);
TextView itemTitle = (TextView)itemView.findViewById(R.id.itemTitle);
TextView itemSubTitle = (TextView)itemView.findViewById(R.id.itemSubTitle);
ImageView itemIcon = (ImageView)itemView.findViewById(R.id.itemIcon);
itemTitle.setText(listViewData.get(position).title);
itemSubTitle.setText(listViewData.get(position).subTitle);
itemIcon.setBackgroundResource(listViewData.get(position).icon);
return itemView;
}
}
但是关于此自定义 ArrayAdapter 的问题是 ExtendedArrayAdapter
需要 items
的 String[]
才能获得 ListView
项目的数量。
问题:
是否可以更改 ExtendedArrayAdapter
class 以直接获取 ListView
的长度而不是 String[] items
?
编辑 1:
在 super()
中,我只能使用以下参数集:
并且都需要 Array<String>
或 List<String>
。所以我不能在这里使用 objects 的 ArrayList。
Is it possible to change the ExtendedArrayAdapter class to get
directly the length of ListView instead of String[] items?
ListView
处理的项目的长度由 Adapter.getCount()
给出。如果我理解正确的话,你的问题的答案是否定的。您的适配器的定义应该更改为
class ExtendedArrayAdapter extends ArrayAdapter<ItemObject>{
您还必须更改 constructor
和 items
的类型
我尝试在每个项目上实现一个带有特定图标、标题和副标题的 ListView
。
所有项目的数据都在 ArrayList
的 objects 中,来自以下 class
:
class ItemObject{
String title="";
String subTitle="";
String unit="";
int icon;
int quantity;
int parentID;
int orderInList;
}
ArrayList<ItemObject> listViewData;
这是我首先使用的代码:
class ExtendedArrayAdapter<String> extends ArrayAdapter<String>{
private Context context;
private String[] items;
public ExtendedArrayAdapter(Context context, String[] items){
super(context,-1,items);
this.context = context;
this.items=items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.lv_item_bg,parent,false);
TextView itemTitle = (TextView)itemView.findViewById(R.id.itemTitle);
TextView itemSubTitle = (TextView)itemView.findViewById(R.id.itemSubTitle);
ImageView itemIcon = (ImageView)itemView.findViewById(R.id.itemIcon);
itemTitle.setText(listViewData.get(position).title);
itemSubTitle.setText(listViewData.get(position).subTitle);
itemIcon.setBackgroundResource(listViewData.get(position).icon);
return itemView;
}
}
但是关于此自定义 ArrayAdapter 的问题是 ExtendedArrayAdapter
需要 items
的 String[]
才能获得 ListView
项目的数量。
问题:
是否可以更改 ExtendedArrayAdapter
class 以直接获取 ListView
的长度而不是 String[] items
?
编辑 1:
在 super()
中,我只能使用以下参数集:
并且都需要 Array<String>
或 List<String>
。所以我不能在这里使用 objects 的 ArrayList。
Is it possible to change the ExtendedArrayAdapter class to get directly the length of ListView instead of String[] items?
ListView
处理的项目的长度由 Adapter.getCount()
给出。如果我理解正确的话,你的问题的答案是否定的。您的适配器的定义应该更改为
class ExtendedArrayAdapter extends ArrayAdapter<ItemObject>{
您还必须更改 constructor
和 items
的类型