开发者

Java/Android: How do I simplify this complicated, nested if else and for loop codeblock?

I have two sections of code that are largely identical except for a few different constants that are used depending on the condition of the if statement. I would like to ask if anyone here knows how to simplify the code so that I don't have so much duplication of code.

Here's the code:

public static final int numLandscapeRows = 5;
public static final int numPortraitRows = 8;
public static final int numLandscapeImagesPerRow = 8;
public static final int numPortraitImagesPerRow = 5;
public static boolean imageViewLandscapeTracker[][] = new boolean[numLandscapeRows][numLandscapeImagesPerRow];
public static boolean imageViewPortraitTracker[][] = new boolean[numPortraitRows][numPortraitImagesPerRow];

if (screenWidth > screenHeight) {
    TableRow tableRow[] = new TableRow[numLandscapeRows];
    ImageView imageView[] = new ImageView[开发者_Go百科numLandscapeImagesPerRow];

    for (i = 0; i < numLandscapeRows; i++) {
        for (j = 0; j < numLandscapeImagesPerRow; j++) {
            imageView[j].setOnClickListener(new OnClickListener(){
                public void onClick(View v) {
                    if (imageViewLandscapeTracker[i-1][j-1] == false) {
                        imageViewLandscapeTracker[i-1][j-1] = true;
                        v.setBackgroundResource(R.drawable.launcher);
                    } else {
                        imageViewLandscapeTracker[i-1][j-1] = false;
                        v.setBackgroundResource(R.drawable.icon);
                    }
        }
    }

} else {
    TableRow tableRow[] = new TableRow[numPortraitRows];
    ImageView imageView[] = new ImageView[numPortraitImagesPerRow];

    for (i = 0; i < numPortraitRows; i++) {
        for (j = 0; j < numPortraitImagesPerRow; j++) {
            imageView[j].setOnClickListener(new OnClickListener(){
                    public void onClick(View v) {
                        if (imageViewPortraitTracker[i-1][j-1] == false) {
                            imageViewPortraitTracker[i-1][j-1] = true;
                            v.setBackgroundResource(R.drawable.launcher);
                        } else {
                            imageViewPortraitTracker[i-1][j-1] = false;
                            v.setBackgroundResource(R.drawable.icon);
                        }
        }
    }
}

I hope that's easy to understand. My Java is very basic so I don't think I can write very complicated code. But if there's anything you need clarification on, please let me know.

Thank you for your time.


Seems like you can just make a simple function to do the work (this syntax isn't 100% right, but you should get the idea):

reducedFunction(int numRows, int numImages, bool[][] tracker) {
    for (i = 0; i < numRows; i++) {
        for (j = 0; j < numImages; j++) {
            imageView[j].setOnClickListener(new OnClickListener(){
                public void onClick(View v) {
                    if (tracker[i-1][j-1] == false) {
                        tracker[i-1][j-1] = true;
                        v.setBackgroundResource(R.drawable.launcher);
                    } else {
                        tracker[i-1][j-1] = false;
                        v.setBackgroundResource(R.drawable.icon);
                    }
        }
    }
}

if(screenWidth > screenHeight) {
    reducedFunction(numLandscapeRows, numLandscapeImagesPerRow, imageViewLandscapeTracker)
}
else {
    reducedFunction(numPortraitRows, numPortraitImagesPerRow, imageViewPortraitTracker)
}


They appear to be nearly idententical.

int maxRows = screenWidth > screenHeight ? numLandscapeRows : numPortraitRows;
int maxCols = screenWidth > screenHeight ? numPortraitImagesPerRow :  numPortraitImagesPerRow;

TableRow tableRow[] = new TableRow[numPortraitRows];
ImageView imageView[] = new ImageView[numPortraitImagesPerRow];

for (i = 0; i < maxRows ; i++) {
    for (j = 0; j < maxCols ; j++) {
        imageView[j].setOnClickListener(new OnClickListener(){
                public void onClick(View v) {
                    if (imageViewPortraitTracker[i-1][j-1]) {
                        v.setBackgroundResource(R.drawable.icon);
                    } else {
                        v.setBackgroundResource(R.drawable.launcher);
                    }
                    imageViewPortraitTracker[i-1][j-1] = imageViewPortraitTracker[i-1][j-1]
                 }
         });
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜