伙计们,我正在尝试使以下代码在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;

};

最佳答案

正如@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

07-25 22:21