Lot Bajrami
Lot Bajrami

Reputation: 19

Android Studio alertDialog in onItemClick

I have a little problem with a simple app in Android Studio, the app is simple, you click a color and the background changes to that color, but I wanted to add an alertDialog so it asks before it changes to that color.
When I click a color, the dialog appears, but the operation that changes the color doesn't, I want it to stop so if I choose yes , it will continue, if I choose No, it will not change the color. I don't know how to cancel the operation after No is clicked.

Here is the code in MainActivity:

 @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to choose this color?")
            .setTitle("Change color")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
    builder.create().show();

    String msg=((TextView) view).getText().toString();
    ShowMessage(msg);

    int[]colorsCodes = new int[]{
            getResources().getColor(R.color.White),
            getResources().getColor(R.color.Black),
            getResources().getColor(R.color.Red),
            getResources().getColor(R.color.Blue),
            getResources().getColor(R.color.Green),
            getResources().getColor(R.color.Yellow),
            getResources().getColor(R.color.Orange),
            getResources().getColor(R.color.Pink),
            getResources().getColor(R.color.Violet),
            getResources().getColor(R.color.Brown)};



    myListView.setBackgroundColor(colorsCodes[i]);

Edit:
I added myListView.setBackgroundColor(colorsCodes[i]); function in the "Yes" option of the alert Dialog, and also declared ColorCodes outside the onItemClick(), everything is working fine except when I click Yes, the app crashes...

 AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to choose this color?")
            .setTitle("Change color")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    myListView.setBackgroundColor(colorsCodes[i]);

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });

edit: thank you, I solved it now, the problem was that the function

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

was taking int i, along with the "yes" option, so I just renamed it to j

public void onClick(DialogInterface dialogInterface, int j) {
                    myListView.setBackgroundColor(colorsCodes[i]);

                }

Upvotes: 0

Views: 959

Answers (1)

Sethuraman Srinivasan
Sethuraman Srinivasan

Reputation: 1606

You have to place the color changing function

myListView.setBackgroundColor(colorsCodes[i]);

inside the onClick function of setPositiveButton function. You can leave it blank inside the setNegativeButton function.

P.S.- don't forget to define the colorsCodes before the onItemClick function.

Upvotes: 1

Related Questions