Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can optimize this code that contain some repetitive line?

Tags:

java

android

I have below code in android programming

Button btn1 = ( Button ) findViewById( R.id.btn1 );
Button btn2 = ( Button ) findViewById( R.id.btn2 );
Button btn3 = ( Button ) findViewById( R.id.btn3 );
Button btn4 = ( Button ) findViewById( R.id.btn4 );
Button btn5 = ( Button ) findViewById( R.id.btn5 );
Button btn6 = ( Button ) findViewById( R.id.btn6 );
Button btn7 = ( Button ) findViewById( R.id.btn7 );
Button btn8 = ( Button ) findViewById( R.id.btn8 );
Button btn9 = ( Button ) findViewById( R.id.btn9 );

And it continue untill btn30
In python I optimize it by below easy code

#is a python syntax (for_statement)
#python work by tab
for i in range(1,31):
    #in python not need to declare temp
    temp="""Button btn"""+str(i)+"""=(Button)findViewById(R.id.btn"""+str(i)+""")"""
    exec(temp)#a default function in python

In java programing how i can do it?Or can I do it?do exist a easy code for it?

UPDATE 1

SO there are two way to do it
Code 1:

final int number = 30;
final Button[] buttons = new Button[number];
final Resources resources = getResources();

for (int i = 0; i < number; i++) {
    final String name = "btn" + (i + 1);
    final int id = resources.getIdentifier(name, "id", getPackageName());

    buttons[i] = (Button) findViewById(id);
}

Code 2:

public static int getIdByName(final String name) {
    try {
        final Field field = R.id.class.getDeclaredField(name);

        field.setAccessible(true);
        return field.getInt(null);
    } catch (Exception ignore) {
        return -1;
    }
}

final Button[] buttons = new Button[30];

for (int i = 0; i < buttons.length; i++) {
    buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}

And other way is GidView

like image 386
Mahdi-bagvand Avatar asked Mar 19 '13 15:03

Mahdi-bagvand


2 Answers

You can create an array of Button's and use getIdentifier method that allows you to get an identifier by its name.

final int number = 30;
final Button[] buttons = new Button[number];
final Resources resources = getResources();

for (int i = 0; i < number; i++) {
    final String name = "btn" + (i + 1);
    final int id = resources.getIdentifier(name, "id", getPackageName());

    buttons[i] = (Button) findViewById(id);
}

In case someone is interested how to achive the same result using Java only

The solution above uses Android specific methods (such as getResources, getIdentifier) and can not be used in usual Java, but we can use a reflection and write a method that works like a getIdentifier:

public static int getIdByName(final String name) {
    try {
        final Field field = R.id.class.getDeclaredField(name);

        field.setAccessible(true);
        return field.getInt(null);
    } catch (Exception ignore) {
        return -1;
    }
}

And then:

final Button[] buttons = new Button[30];

for (int i = 0; i < buttons.length; i++) {
    buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
like image 132
Vladimir Mironov Avatar answered Oct 24 '22 13:10

Vladimir Mironov


Instead of optimizing this kind of code you should rethink your layout. If you have 30 buttons on the screen, a ListView is probably the better solution. You can access the items via index and handle onClick events just like with the buttons.

like image 44
Kirill Rakhman Avatar answered Oct 24 '22 12:10

Kirill Rakhman