There has to be a better way to do this but everything I've tried fails. Maybe I have to programtically create the textViews, I'm not sure.
I have this:
if (totalCount[0] > 0) {
b1Pct = df.format((totalMs[0] / (float) totalTestMs) * 100);
b1 = df.format((totalBits[0] / totalCount[0]) / 1000000);
}
if (totalCount[1] > 0) {
b2Pct = df.format((totalMs[1] / (float) totalTestMs) * 100);
b2 = df.format((totalBits[1] / totalCount[1]) / 1000000);
}
if (totalCount[2] > 0) {
b3Pct = df.format((totalMs[2] / (float) totalTestMs) * 100);
b3 = df.format((totalBits[2] / totalCount[2]) / 1000000);
}
As you can see I have need of a loop to cut this code down, it actually goes for another 6 entries. I'd like to do something like this but it doesn't work how I'd hoped:
for (var i=0; i < totalCount.length; i++) {
var count = i+1;
if (totalCount[i] > 0) {
"b"+count+"Pct" = df.format((totalMs[i] / (float) totalTestMs) * 100);
"b"+count = df.format((totalBits[i] / totalCount[i]) / 1000000);
"Bucket"+count.setText(b+count); "Bucket"+count+"pct".setText("b"+count+"Pct");
}
}
Obviously that doesn't work.
Edit
I've cut a whole ton of lines down, so thank you. I'm sure it can still be improved?
DecimalFormat df = new DecimalFormat("0.00");
String[] percentages = { "b1Pct", "b2Pct", "b3Pct", "b4Pct", "b5Pct", "b6Pct", "b7Pct", "b8Pct", "b9Pct", "b10Pct" };
String[] mbpsResults = { "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10" };
TextView[] buckets = { Bucket1, Bucket2, Bucket3, Bucket4, Bucket5, Bucket6, Bucket7, Bucket8, Bucket9, Bucket10 };
TextView[] bucketsPct = { Bucket1pct, Bucket2pct, Bucket3pct, Bucket4pct, Bucket5pct, Bucket6pct, Bucket7pct, Bucket8pct, Bucket9pct, Bucket10pct };
for (int j = 0; j < totalCount.length; j++) {
if (totalCount[j] > 0) {
percentages[j] = df.format((totalMs[j] / (float) totalTestMs) * 100);
mbpsResults[j] = df.format((totalBits[j] / totalCount[j]) / 1000000);
buckets[j].setText(mbpsResults[j]);
bucketsPct[j].setText(percentages[j]);
}
else {
buckets[j].setText("0");
bucketsPct[j].setText("0");
}
}