Q:

Java code optimalization - Two functions into one

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.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();
        }
    });

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

A:

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();
        }
    }
}

