Android java使两个类似的循环进入一个循环

时间:2015-02-17 22:21:22

标签: java android for-loop view android-view

我试图将这两个for循环合并在一起,以便我可以将它用于一个fillview。

我一直试图尝试几天,但我不确定如何做到这一点,所以会感激一些帮助!

感谢任何帮助

    int maxDataSetSize = Math.max(data1.size(), Math.max(data2.size(), data3.size())) ;

    for (int i = 0; i < maxDataSetSize; i++) {
        String dataset1Value = data1.size() > i ? data1.get(i) : null;
        String dataset2Value = data2.size() > i ? data2.get(i) : null;
        String dataset3Value = data3.size() > i ? data3.get(i) : null;

        View statsRowview = getLayoutInflater().inflate(R.layout.stats_row, null);

        fillView(statsRowview, dataset1Value, dataset2Value, dataset3Value);

        tableStats.addView(statsRowview);
    }


    int maxDataSetSize2 = Math.max(data4.size(), Math.max(data5.size(), data6.size()));
    for (int i = 0; i < maxDataSetSize2; i++) {
        String dataset4Value = data4.size() > i ? data4.get(i) : DEFAULT_COLOR;
        String dataset5Value = data5.size() > i ? data5.get(i) : DEFAULT_COLOR;
        String dataset6Value = data6.size() > i ? data6.get(i) : DEFAULT_COLOR;

        View statsRowview = getLayoutInflater().inflate(R.layout.stats_row, null);

        fillView2(statsRowview, dataset4Value, dataset5Value, dataset6Value);

        tableStats.addView(statsRowview);
    }
}

1 个答案:

答案 0 :(得分:2)

试试这个:

int maxDataSetSize = Math.max(data1.size(), Math.max(data2.size(), data3.size())) ;
int maxDataSetSize2 = Math.max(data4.size(), Math.max(data5.size(), data6.size()));

for (int i = 0, j = 0; i < maxDataSetSize || j < maxDataSetSize2; i++, j++) {

    View statsRowview = getLayoutInflater().inflate(R.layout.stats_row, null);

    if(i < maxDataSetSize){
        String dataset1Value = data1.size() > i ? data1.get(i) : null;
        String dataset2Value = data2.size() > i ? data2.get(i) : null;
        String dataset3Value = data3.size() > i ? data3.get(i) : null;
        fillView(statsRowview, dataset1Value, dataset2Value, dataset3Value);
    }


    if(j < maxDataSetSize2){
        String dataset4Value = data4.size() > j ? data4.get(j) : DEFAULT_COLOR;
        String dataset5Value = data5.size() > j ? data5.get(j) : DEFAULT_COLOR;
        String dataset6Value = data6.size() > j ? data6.get(j) : DEFAULT_COLOR;
        fillView2(statsRowview, dataset4Value, dataset5Value, dataset6Value);
    }

    tableStats.addView(statsRowview);
}

这应该有用。