gpt4 book ai didi

android - 将自定义对象添加到 ArrayAdapter。如何抓取数据?

转载 作者:太空狗 更新时间:2023-10-29 15:39:04 28 4
gpt4 key购买 nike

这是我目前所拥有的:

自定义对象:

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;
}
}

对适配器的调用:

List<String> names, ratings, totals;

ItemObject[] io= new ItemObject[3];
io[0] = new ItemObject(names);
io[1] = new ItemObject(rating);
io[2] = new ItemObject(totals);

adapter = new ItemAdapter(Items.this, io);
setListAdapter(adapter);

假设上面看起来没问题,我的问题是如何设置 ItemAdapter,它的构造函数,并从对象中解包三个列表。然后,在 getView 中,分配这些东西:

每个匹配位置到:

    TextView t1 = (TextView) rowView.findViewById(R.id.itemName);
TextView t2 = (TextView) rowView.findViewById(R.id.itemTotal);
RatingBar r1 = (RatingBar) rowView.findViewById(R.id.ratingBarSmall);

例如,数组“names”中的位置 0 到 t1。数组“totals”中的位置 0 到 t1。数组“ratings”中的位置 0 到 r1。

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

最佳答案

您的代码将无法以其实际形式运行。您真的需要 ItemObject 中的数据列表吗?我的猜测是否定的,您只是想要一个 ItemObject,它包含与行布局中的 3 个 View 相对应的 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);

和适配器类:

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
// ...

}

}

关于android - 将自定义对象添加到 ArrayAdapter。如何抓取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11593574/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com