Android 将自定义对象添加到ArrayAdapter。如何获取数据?

Android 将自定义对象添加到ArrayAdapter。如何获取数据?,android,list,object,android-arrayadapter,Android,List,Object,Android Arrayadapter,这就是我到目前为止所做的: 自定义对象: class ItemObject { List<String> name; List<String> total; List<String> rating; public ItemObject(List<ItemObject> io) { this.total = total; this.name = name; this.rating = rating;

这就是我到目前为止所做的:

自定义对象:

class ItemObject {
    List<String> name;
    List<String> total;
    List<String> rating;

public ItemObject(List<ItemObject> io) {
    this.total = total;
    this.name = name;
    this.rating = rating;
 }
}
例如,数组“names”中的位置0到t1。 数组“总计”中的位置0到t1。 数组“额定值”中的位置0到r1


编辑:我不希望有人编写整个适配器。我只需要知道如何从自定义对象中展开列表,以便使用数据。(甚至没有在另一个问题中提出或询问的内容)

您的代码将无法以其实际形式工作。您真的需要
ItemObject
中的数据列表吗?我的猜测是否定的,您只需要一个
ItemObject
,它包含与行布局中的3个视图相对应的3个字符串。如果是这种情况:

class ItemObject {
    String name;
    String total;
    String rating;// are you sure this isn't a float

public ItemObject(String total, String name, String rating) {
    this.total = total;
    this.name = name;
    this.rating = rating;
 }
}
然后,您的列表将合并到
ItemObject
的列表中:

List<String> names, ratings, totals;
ItemObject[] io= new ItemObject[3];
// use a for loop
io[0] = new ItemObject(totals.get(0), names.get(0), ratings(0));
io[1] = new ItemObject(totals.get(1), names.get(1), ratings(1));
io[2] = new ItemObject(totals.get(2), names.get(2), ratings(2));
adapter = new ItemAdapter(Items.this, io);
setListAdapter(adapter);
列出名称、评级、总数;
ItemObject[]io=新的ItemObject[3];
//使用for循环
io[0]=新的ItemObject(totals.get(0)、names.get(0)、ratings(0));
io[1]=新的ItemObject(totals.get(1)、names.get(1)、ratings(1));
io[2]=newitemObject(totals.get(2)、names.get(2)、ratings(2));
适配器=新项目适配器(Items.this,io);
setListAdapter(适配器);
和适配器类:

public class ItemAdapter extends ArrayAdapter<ItemObject> {

        public ItemAdapter(Context context,
                ItemObject[] objects) {
            super(context, 0, objects);         
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // do the normal stuff
            ItemObject obj = getItem(position);
            // set the text obtained from obj
                    String name = obj.name; //etc       
                    // ...

        }       

}
公共类ItemAdapter扩展了ArrayAdapter{
公共项适配器(上下文,
ItemObject[]对象){
超级(上下文,0,对象);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//做正常的事情
ItemObject obj=getItem(位置);
//设置从obj获取的文本
String name=obj.name;//等
// ...
}       
}

这与你昨天的问题非常相似:另一个问题只是问如何(或使用什么方法)做某事。事实上,我还在研究如何做到这一点。这个问题是指这样做的一种具体方式。还有,在这个问题上根本没有提出过一种方法。我仍在决定如何处理这件事。我仍然感谢大家在提供答案时给予的帮助。我很可能会回到这些想法中的一个。感谢人们的一种常见方式是投票选出有意义的答案,并将最好的答案标记为正确。我花了一些时间,但一旦我知道了什么,我总是回到它。虽然我应该早点投票。。。(直到我有了一个我能够使用和实现的答案,答案才被选中)太棒了。Luksprog为您指出了正确的方向,我不太确定是否有人能在这方面有显著的改进。由于投票结果是关闭的(出于某种原因),我将在为时已晚之前投票认为这是正确的。我想确保你得到信用,而这正是我想要的!
List<String> names, ratings, totals;
ItemObject[] io= new ItemObject[3];
// use a for loop
io[0] = new ItemObject(totals.get(0), names.get(0), ratings(0));
io[1] = new ItemObject(totals.get(1), names.get(1), ratings(1));
io[2] = new ItemObject(totals.get(2), names.get(2), ratings(2));
adapter = new ItemAdapter(Items.this, io);
setListAdapter(adapter);
public class ItemAdapter extends ArrayAdapter<ItemObject> {

        public ItemAdapter(Context context,
                ItemObject[] objects) {
            super(context, 0, objects);         
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // do the normal stuff
            ItemObject obj = getItem(position);
            // set the text obtained from obj
                    String name = obj.name; //etc       
                    // ...

        }       

}