Save values selected in an AlertDialog
How do I save the selected value?
private final CharSequence[] mColors = {"1", "2", "3", "4", "5" , "6"};
switch (id) {
case w_COLOR:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Тест");
builder.setSingleChoiceItems(mColors, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
mResult = item;
}
});
builder.setPositiveButton("Ок", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "ITEM: " + mColors[mResult], Toast.LENGTH_LONG).show();
if (mResult == 0)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 1)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 2)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
开发者_开发百科 }
if (mResult == 3)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
}
if (mResult == 4)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 5)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
}
Set a variable. For example I flip a boolean value in this call:
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// Save the record, then flip editMode and change updating
// editable
Log.i(TAG, "Save requested. Saving...");
updateRecord();
editMode = !editMode;
titlebar.setText(editMode ? EDIT_TITLE : VIEW_TITLE);
updateEditable(editMode);
recordUpdated = false;
String ems = editMode ? "ON" : "OFF";
Log.i(TAG, "Switching edit mode "+ems);
}
});
Also, you should probably either switch the if
structure to an if...else if... else
structure or switch (case)
like you do above. As it is now if that setPositiveButton is clicked you're checking each if statement and they're all mutually exclusive. You can also probably pull the Parameters params = camera.getParameters();
outside of the if structure since it seems to be called each time.
精彩评论