1

This is my current code, is their anyway that I can use some sort of loop to replace the copy paste? I am using Android Studio if that makes any difference.

    if (otterPurchased >= 1) {
        TableRow layoutOtter2 = (TableRow) findViewById(R.id.layoutOtter2);
        layoutOtter2.setVisibility(View.VISIBLE);
        if (otterPurchased >= 2) {
            TableRow layoutOtter3 = (TableRow) findViewById(R.id.layoutOtter3);
            layoutOtter3.setVisibility(View.VISIBLE);
            if (otterPurchased >= 3) {
                TableRow layoutOtter4 = (TableRow) findViewById(R.id.layoutOtter4);
                layoutOtter4.setVisibility(View.VISIBLE);
                if (otterPurchased >= 4) {
                    TableRow layoutOtter5 = (TableRow) findViewById(R.id.layoutOtter5);
                    layoutOtter5.setVisibility(View.VISIBLE);
                    if (otterPurchased >= 5) {
                        TableRow layoutOtter6 = (TableRow) findViewById(R.id.layoutOtter6);
                        layoutOtter6.setVisibility(View.VISIBLE);
                        if (otterPurchased >= 6) {
                            TableRow layoutOtter7 = (TableRow) findViewById(R.id.layoutOtter7);
                            layoutOtter7.setVisibility(View.VISIBLE);
                            if (otterPurchased >= 7) {
                                TableRow layoutOtter8 = (TableRow) findViewById(R.id.layoutOtter8);
                                layoutOtter8.setVisibility(View.VISIBLE);
                                if (otterPurchased >= 8) {
                                    TableRow layoutOtter9 = (TableRow) findViewById(R.id.layoutOtter9);
                                    layoutOtter9.setVisibility(View.VISIBLE);
                                    if (otterPurchased >= 9) {
                                        TableRow layoutOtter10 = (TableRow)
findViewById(R.id.layoutOtter10);
                                        layoutOtter10.setVisibility(View.VISIBLE);
                                        if (otterPurchased >= 10) {
                                            TableRow layoutOtter11 = (TableRow) findViewById(R.id.layoutOtter11);
                                            layoutOtter11.setVisibility(View.VISIBLE);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

This just checks variables and runs the underneath code if true, also in a forever loop.

Rose
  • 29
  • 5
  • you probably shouldn't use manual table rows but one of the list view layouts that allow you to build dynamic lists. https://developer.android.com/develop/ui/views/layout/recyclerview - there's also https://stackoverflow.com/a/6831562/995891 which would work in a loop – zapl Nov 04 '22 at 18:23
  • 1
    Believe it or not, your code is the most efficient and effective way of doing things :-)! To reduce this code, you need a different approach to the problem, which can be messy, confusing, and convoluted. (To include more XML code, for loops, and strange ways of obtaining reference to the views in your XML layout). I believe zapl's second link is more the way I'd approach the problem. If not, leave it alone! It looks good, it's symmetric, and syntatically correct! Good luck! – BigBeef Nov 05 '22 at 01:37

0 Answers0