开发者

Java code optimalization - Two functions into one

开发者 https://www.devze.com 2023-03-27 13:07 出处:网络
Hello I have this code: this.firstBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) {

Hello I have this code:

this.firstBtn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        final CharSequence[] items = {"1", "2", "3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
        builder.setTitle("test");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                langFrom.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
});

this.secondBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            final CharSequence[] items = {"1", "2", "3"};
            AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
            builder.set开发者_如何学编程Title("test");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    langFrom.setText(items[item]);
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

There are just two different parts: this.firstBtn and this.secondBtn Is there a way how to merge it? For example when I click on firstBtn it calls a function foo(firstBtn) and secondBtn calls foo(secondBtn) and the rest would work the same? I'm not sure about syntax etc. because I'm new to Java and Android development today.

Thanks


What you want can be done relatively easily. You can define a new class that implements OnClickListener, and use a new instance of this class in both locations.

class MyOnClickListener implements OnClickListener {
    public void onClick(View v) {
        final CharSequence[] items = {"1", "2", "3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
        builder.setTitle("test");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                langFrom.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

Your code can then look like this:

this.firstBtn.setOnClickListener(new MyOnClickListener());
this.secondBtn.setOnClickListener(new MyOnClickListener());

Obviously this solution can be taken further, if your two MyOnClickListener's need to be just slightly different. This can be done by creating a constructor that takes in the parameters that you want to change and store them as member variables, which are then used on the call to onClick(...):

class MyOnClickListener implements OnClickListener {
    private CharSequence[] items;

    public MyOnClickListener(CharSequence[] _items) {
        items = _items;
    }

    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
        builder.setTitle("test");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                langFrom.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

And this can then be used like so:

final CharSequence[] items1 = {"1", "2", "3"};
final CharSequence[] items2 = {"2", "3", "4"};
this.firstBtn.setOnClickListener(new MyOnClickListener(items1));
this.secondBtn.setOnClickListener(new MyOnClickListener(items2));

UPDATE

public class SlovnikoidActivity extends Activity {
    //slovnikoidActivity definition

    //inner class definition for MyOnClickListener
    class MyOnClickListener implements OnClickListener {
        private CharSequence[] items;

        public MyOnClickListener(CharSequence[] _items) {
            items = _items;
        }

        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
            builder.setTitle("test");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    langFrom.setText(items[item]);
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
}


As an alternative to nicholas.hauschild's answer, if you don't want to make a separate class, just make your activity implement View.OnClickListener.

Then, move your onClick(View v) method to the same class your button code is already in. If you need to distinguish between different buttons ever, then just do switch (v.getId()).

0

精彩评论

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