0

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");
}
}
2
  • Why not use an array? Commented Sep 25, 2014 at 20:56
  • Add all of your TextViews in an ArrayList and iterate the arraylist to change the text of each TextView. Commented Sep 25, 2014 at 20:57

2 Answers 2

1

Yes, creating the views programmatically is one solution (and of course putting them in an array or ArrayList after doing so). You can, however, just add those items to an array afterwards.

EditText[] textBoxes = new EditText[numberOfViews];

textBoxes[0] = findViewById(R....);

textBoxes[1] = findVi...

The way to create them manually is to just make a new EditText object and setting all the parameters you would otherwise set in your xml file. For example you would

for(int i = 0; i < amount; i++) {
    textBoxes[0] = new EditText(this);
    textBoxes[0].setWidth(...);
}

Of course you'll have to add them to your parent view (the activity) afterwards.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

I have updated my answer, it have cut out a lot of lines thanks to your suggestions
any further comments?
I think this is about as tight as it will get!
0

It seems pretty straight forward (if that's what you need).

Let's say you need to create 5 TextViews programatically:

int N = 5;
List<TextView> list = new ArrayList<TextView>(N);
for (int i = 0; i < N; i++) {
    TextView t = new TextView(this);
    t.setText("Textview number: " + i); //customize as you wish            
    list.add(t);
}

By the way, I saw you are doing a lot of String concatenations there. It would be much faster to use a StringBuilder to add all the characters, and then produce the final string just once with toString().

2 Comments

I've updated my answer now with my new code. Any more suggestions? I wasn't clear on the StringBuilder comment
Let's see if I can clarify. Concatenating Strings like this "hello " + "world". Will make Java create a new String("hello world") for every concatenation. If you use a StringBuilder object, and just append the Characters that you need, it will only allocate a new String with the needed memory when you call the toString() method on it. This will be MUCH faster than the former method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.