Reputation: 2151
I have a popup I want to create that will have 3 choices. (Block, Unblock, Uninstall). Below I have a Confirm|Cancel button. I would like to see which button is pressed, and based on that will display a toast corresponding to the selection. I have written some code to show what I am trying to do. Obviously the if items[item] == Block won't work, but in a simplified sense that is what I am trying to do. Can someone show me how I would write the OnClickListener to capture what button was clicked and how to differenciate it when clicking Confirm?
Code:
package com.popup;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class PopupActivity extends Activity {
String TAG = "PopUpActivity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BlockUnblockUninstallPopUp("");
String TAG = "PopUpActivity";
}
public void BlockUnblockUninstallPopUp(final String PackageName){
//Items you would like to list as options.
final CharSequence[] items = {"Block", "UnBlock", "Uninstall"};
String flag = null;
AlertDialog.Builder builder = new AlertDialog.Builder(PopupActivity.this);
//Title of Popup
builder.setTitle("What would you like to do?");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
//When you click the radio button
public void onClick(DialogInterface dialog, int item) {
}
});
//When you click Confirm
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(items[item] == "Block")
{
Toast.makeText(PopupActivity.this,PackageName + " Blocked.", Toast.LENGTH_SHORT).show();
Log.d(TAG,PackageName + " Blocked.");
}
if(items[item] == "Unblock")
{
Toast.makeText(PopupActivity.this,PackageName + " Unblocked.", Toast.LENGTH_SHORT).show();
Log.d(TAG,PackageName + " Unblocked.");
}
if(items[item] == "Uninstall")
{
Toast.makeText(PopupActivity.this,PackageName + " Uninstalled.", Toast.LENGTH_SHORT).show();
Log.d(TAG,PackageName + " Uninstalled.");
}
}
});
//When you click Cancel, Leaves PopUp.
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
//Creates the AlertDialog
AlertDialog alert = builder.create();
//Starts the Popup.
alert.show();
}
}
Upvotes: 1
Views: 2479
Reputation: 40416
int i;
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
// When you click the radio button
public void onClick(DialogInterface dialog, int item) {
i=item;
}
});
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[i] == "Block") {
Toast.makeText(PopupActivity.this,
PackageName + " Blocked.",
Toast.LENGTH_SHORT).show();
Log.d(TAG, PackageName + " Blocked.");
}
if (items[i] == "UnBlock") {
Toast.makeText(PopupActivity.this,
PackageName + " Unblocked.",
Toast.LENGTH_SHORT).show();
Log.d(TAG, PackageName + " Unblocked.");
}
if (items[i] == "Uninstall") {
Toast.makeText(PopupActivity.this,
PackageName + " Uninstalled.",
Toast.LENGTH_SHORT).show();
Log.d(TAG, PackageName + " Uninstalled.");
}
}
});
Upvotes: 5