我在向BaseAdapter添加项目时遇到问题,BaseAdapter是GridView的内容。如果我手动传递项目,它看起来像这样:
private class MyAdapter extends BaseAdapter
{
Context context;
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
this.context = context;
inflater = LayoutInflater.from(context);
items.add(new Item("Burton - Ruler", R.drawable.bt_burton_ruler));
items.add(new Item("DC - Ceptor", R.drawable.bt_dc_ceptor));
}
以及它在视觉上的外观:
Item
课程是:
private class Item
{
final String name;
final int picId;
Item(String name, int drawableId)
{
this.name = name;
this.picId = drawableId;
}
}
由于手动添加内容是一种糟糕的方法,我试图从解析的XML文件(使用Xstream)传递它。它解析好了。以下是解析的ArrayList的内容如下所示:
以下是我尝试将这些值传递给Adapter的方法:
public MyAdapter(Context context) throws Exception
{
inflater = LayoutInflater.from(context);
try {
FileReader reader = new FileReader("xml/boots.xml"); // load file
XStream xstream = new XStream();
xstream.processAnnotations(Boots.class);
xstream.processAnnotations(IdBoots.class);
Boots boots= (Boots) xstream.fromXML(reader);
for (int j = 0; j < boards.getIds().size(); j++) {
IdBoots idBoot= (IdBoots)boards.getIds().get(j);
int id = context.getResources().getIdentifier(idBoot.getPicture(), "drawable", context.getPackageName());
items.add(new Item(idBoot.getBoard(), id));
}
} catch (Exception e){
e.getMessage();
}
}
如你所见,我试图获得drawable/picture_name.jpg
的id是这样的方式(可能是问题在这里):
int id = context.getResources().getIdentifier(idBoot.getPicture(), "drawable", context.getPackageName());
但仍然id不起作用。它编译和填充,但GridView仍然是空的。
是的,这就是Boots
班的样子:
@XStreamAlias("BOOTS")
public class Boots<E> {
@XStreamImplicit(itemFieldName = "id")
public List<E> ids = new ArrayList<E>();
//region GETTERS-SETTERS
public void setIds(ArrayList<E> temp){
this.ids = temp;
}
public List<E> getIds(){
return ids;
}
// endregion
}
@XStreamAlias("id")
class IdBoots {
@XStreamAlias("boot")
private String boot;
@XStreamAlias("description")
private String description;
@XStreamAlias("price")
private String price;
@XStreamAlias("riding_level")
private String ridingLevel;
@XStreamAlias("lacing_type")
private String lacingType;
@XStreamAlias("picture")
private String picture;
<<GETTERS - SETTERS HERE>>
}
真诚地感谢您的帮助