伙计我试图让下面的代码在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);
更新:
我有一个自定义适配器,它将部分添加到列表视图中,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;
};
答案 0 :(得分:1)
正如@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