1
\$\begingroup\$

I am wanting to implement an AlertDialog with a list into my app and I created a sample to see if I can get the result that I am looking for. It seems I do have the result I wanted but before I implement it into my actual app, is there a way to make my code better?:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Press here" />

</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                showAlertDialogWithList();
        }
    }

    public void showAlertDialogWithList() {
        List<String> mAnimals = new ArrayList<>();
        mAnimals.add("Cat");
        mAnimals.add("Dog");
        mAnimals.add("Horse");
        mAnimals.add("Elephant");
        mAnimals.add("Rat");
        mAnimals.add("Lion");
        mAnimals.add("Tiger");
        mAnimals.add("Bird");
        mAnimals.add("Zebra");
        mAnimals.add("Cheetah");
        mAnimals.add("Hyena");

        final CharSequence[] animals = mAnimals.toArray(new String[mAnimals.size()]);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("Animals");
        dialogBuilder.setItems(animals, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String selectedText = animals[i].toString();
                Toast.makeText(getApplicationContext(), selectedText, Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialogObject = dialogBuilder.create();
        alertDialogObject.show();
    }

}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I don't know how many times you wish to use this on your project so I tested and I suggest you to first extract the list from the method context and receive it as argument.

public void showAlertDialogWithList(List<String> mAnimals) {

    final CharSequence[] animals = mAnimals.toArray(new String[mAnimals.size()]);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Animals");
    dialogBuilder.setItems(animals, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            String selectedText = animals[i].toString();
            Toast.makeText(getApplicationContext(), selectedText, Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog alertDialogObject = dialogBuilder.create();
    alertDialogObject.show();
}

Then you can extract a class and put it in some util package making it available to entire project like this one:

public class AlertDialogList {
    public static void showAlertDialogWithList(String title, List<String> mAnimals, final Activity activity) {

        final CharSequence[] animals = mAnimals.toArray(new String[mAnimals.size()]);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        dialogBuilder.setTitle(title);
        dialogBuilder.setItems(animals, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String selectedText = animals[i].toString();
                Toast.makeText(activity.getApplicationContext(), selectedText, Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialogObject = dialogBuilder.create();
        alertDialogObject.show();
    }
}

and use it like this

 AlertDialogList.showAlertDialogWithList("Animals Title", this.animals, this);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.