Android 将项目从解析的XML传递到BaseAdapter
我在向作为GridView内容的BaseAdapter添加项时遇到问题。如果我手动传递项目,看起来是这样的:Android 将项目从解析的XML传递到BaseAdapter,android,xml,gridview,baseadapter,Android,Xml,Gridview,Baseadapter,我在向作为GridView内容的BaseAdapter添加项时遇到问题。如果我手动传递项目,看起来是这样的: private class MyAdapter extends BaseAdapter { Context context; private List<Item> items = new ArrayList<Item>(); private LayoutInflater inflater; public MyAdapter(Co
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));
}
由于手动添加内容是一种糟糕的方法,所以我尝试从解析的XML文件(使用Xstream)传递它。它解析为Ok。以下是解析后的ArrayList的内容:
@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>>
}
下面是我如何尝试将这些值传递到适配器:
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();
}
}
衷心感谢您的帮助只需在适配器的构造函数中添加一个类似参数的列表,根据您的需要覆盖适配器中的getView和getItem方法,即可覆盖它们。但是如何在代码中应用这些
getView()
和getItem()
?我需要类似于setItem()的东西
@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>>
}