gpt4 book ai didi

java - 列表项 ArrayList ListView

转载 作者:行者123 更新时间:2023-11-30 11:00:39 24 4
gpt4 key购买 nike

伙计们,我试图让下面的代码在 exampleArray 中存储多个项目,但它只获取第一个 SectionOutageListItem。我是否需要创建另一个 listItem 数组来再次循环它?

SectionOutageListItem[] exampleArray = new SectionOutageListItem[outnums.size()];

for(int i = 0; i < outnums.size(); i++) {
exampleArray[i] =
new SectionOutageListItem("Impact", impacted.get(i), "Outage No. " + outnums.get(i)),
new SectionOutageListItem("status", status.get(i), "Outage No. " + outnums.get(i));


}
CustomOutageDetailListAdapter adapter = new CustomOutageDetailListAdapter(this, exampleArray);
sectionAdapter = new SectionOutageListAdapter(getLayoutInflater(),
adapter);

更新:

我有一个自定义适配器,它向 ListView 添加部分,SectionOutageListItem 确定该部分中有多少行。 outnums.get(i) 创建多个部分,应将影响和状态添加为每个部分的行。它只是将第一个新的 SectionOutageListItem 添加为一行,而不是第二个。

自定义列表适配器代码

public class CustomOutageDetailListAdapter extends ArrayAdapter<SectionOutageListItem> {

private Activity context;
private SectionOutageListItem[] items;

//private final ArrayList<String> itemname;

public CustomOutageDetailListAdapter(Activity context, SectionOutageListItem[] items) {
super(context, R.layout.mylistoutagedetails, items);
this.items= items;
this.context = context;
}

@Override
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylistoutagedetails, null,true);
final SectionOutageListItem currentItem = items[position];
if (currentItem != null) {
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
TextView txtName = (TextView) rowView.findViewById(R.id.name);
if (txtTitle != null) {
txtTitle.setText(currentItem.item.toString());

}
if (txtName != null) {

txtName.setText(currentItem.name.toString());

}
}
return rowView;

};

最佳答案

正如@Trobbins 指出的那样,您可能需要更改代码如下,

SectionOutageListItem[][] exampleArray = new SectionOutageListItem[outnums.size()][2];

for(int i = 0; i < outnums.size(); i++) {
exampleArray[i][0] =
new SectionOutageListItem("Impact", impacted.get(i), "Outage No. " + outnums.get(i));
exampleArray[i][1] = new SectionOutageListItem("status", status.get(i), "Outage No. " + outnums.get(i));


}
CustomOutageDetailListAdapter adapter = new CustomOutageDetailListAdapter(this, exampleArray);
sectionAdapter = new SectionOutageListAdapter(getLayoutInflater(),
adapter);

如果你想维护插入顺序,你也可以使用 Map 特别是 LinkedHashMap 或者 HashMap

关于java - 列表项 ArrayList ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31438635/

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