public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(CategoriesTab.this);
adb.setTitle("Selected Category");
adb.setMessage("Selected Item is = "+lv1.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
This at the moment displays an alertbox when an item from listview is clicked. I want to convert the alertbox to load a specific xml for each choices clicked. How can i do this?
thanks for your help.
switch(position) {
case 0:
setContentView(R.layout.xml0);
break;
case 1:
setContentView(R.layout.xml1);
break;
default:
setContentView(R.layout.default);
break;
}
i hope this will do the job!
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.:
break;
case R.id.:
break;
default:
break;
}
}
switch(position) {
case 0:
...
break;
case 1:
...
break;
default:
...
}
Did you mean that?
You can do this:
#Override
protected Dialog onCreateDialog(int id) {
String messageDialog;
String valueOK;
String valueCancel;
String titleDialog;
switch (id) {
case id:
titleDialog = itemTitle;
messageDialog = itemDescription
valueOK = "OK";
return new AlertDialog.Builder(HomeView.this).setTitle(titleDialog).setPositiveButton(valueOK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.d(this.getClass().getName(), "AlertItem");
}
}).setMessage(messageDialog).create();
and then call to
showDialog(numbreOfItem);
Related
error: an enum switch case label must be the unqualified name of an enumeration constant
error: duplicate case label
no compiling, help me!
public class CardViewStyleSetting extends ThemedSetting {
public CardViewStyleSetting(ThemedActivity activity) {
super(activity);
}
public void show() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), getActivity().getDialogStyle());
final View dialogLayout = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_album_card_style, null);
TextView dialogTitle = dialogLayout.findViewById(R.id.dialog_card_view_style_title);
((CardView) dialogLayout.findViewById(R.id.dialog_card_view_style)).setCardBackgroundColor(getActivity().getCardBackgroundColor());
dialogTitle.setBackgroundColor(getActivity().getPrimaryColor());
final RadioGroup rGroup = dialogLayout.findViewById(R.id.radio_group_card_view_style);
final CheckBox chkShowMediaCount = dialogLayout.findViewById(R.id.show_media_count);
final CheckBox chkShowAlbumPath = dialogLayout.findViewById(R.id.show_album_path);
RadioButton rCompact = dialogLayout.findViewById(R.id.radio_card_compact);
RadioButton rFlat = dialogLayout.findViewById(R.id.radio_card_flat);
RadioButton rMaterial = dialogLayout.findViewById(R.id.radio_card_material);
chkShowMediaCount.setChecked(Prefs.showMediaCount());
chkShowAlbumPath.setChecked(Prefs.showAlbumPath());
getActivity().themeRadioButton(rCompact);
getActivity().themeRadioButton(rFlat);
getActivity().themeRadioButton(rMaterial);
getActivity().themeCheckBox(chkShowMediaCount);
getActivity().themeCheckBox(chkShowAlbumPath);
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
final View v;
switch (i) {
case R.id.radio_card_compact:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.COMPACT.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(ColorPalette.getTransparentColor(getActivity().getBackgroundColor(), 150));
break;
case R.id.radio_card_flat:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.FLAT.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(ColorPalette.getTransparentColor(getActivity().getBackgroundColor(), 150));
break;
case R.id.radio_card_material: default:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.MATERIAL.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(getActivity().getCardBackgroundColor());
break;
}
ImageView img = v.findViewById(R.id.album_preview);
img.setBackgroundColor(getActivity().getPrimaryColor());
Glide.with(getActivity())
.load(R.drawable.donald_header)
.into(img);
String hexPrimaryColor = ColorPalette.getHexColor(getActivity().getPrimaryColor());
String hexAccentColor = ColorPalette.getHexColor(getActivity().getAccentColor());
if (hexAccentColor.equals(hexPrimaryColor))
hexAccentColor = ColorPalette.getHexColor(ColorPalette.getDarkerColor(getActivity().getAccentColor()));
String textColor = getActivity().getBaseTheme().equals(Theme.LIGHT) ? "#2B2B2B" : "#FAFAFA";
String albumPhotoCountHtml = "<b><font color='" + hexAccentColor + "'>420</font></b>";
((TextView) v.findViewById(R.id.album_media_count)).setText(StringUtils.html(albumPhotoCountHtml));
((TextView) v.findViewById(R.id.album_media_label)).setTextColor(getActivity().getTextColor());
((TextView) v.findViewById(R.id.album_path)).setTextColor(getActivity().getSubTextColor());
v.findViewById(R.id.ll_media_count).setVisibility( chkShowMediaCount.isChecked() ? View.VISIBLE : View.GONE);
chkShowMediaCount.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
v.findViewById(R.id.ll_media_count).setVisibility(b ? View.VISIBLE : View.GONE);
}
});
v.findViewById(R.id.album_path).setVisibility( chkShowAlbumPath.isChecked() ? View.VISIBLE : View.GONE);
chkShowAlbumPath.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
v.findViewById(R.id.album_path).setVisibility(b ? View.VISIBLE : View.GONE);
}
});
((TextView) v.findViewById(R.id.album_name)).setText(StringUtils.html("<i><font color='" + textColor + "'>PraiseDuarte</font></i>"));
((TextView) v.findViewById(R.id.album_path)).setText("~/home/PraiseDuarte");
((CardView) v).setUseCompatPadding(true);
((CardView) v).setRadius(2);
((LinearLayout) dialogLayout.findViewById(R.id.ll_preview_album_card)).removeAllViews();
((LinearLayout) dialogLayout.findViewById(R.id.ll_preview_album_card)).addView(v);
}
});
switch (Prefs.getCardStyle()) {
case CardViewStyle.COMPACT: rCompact.setChecked(true); break;
case CardViewStyle.FLAT: rFlat.setChecked(true); break;
case CardViewStyle.MATERIAL: default: rMaterial.setChecked(true); break;
}
builder.setNegativeButton(getActivity().getString(R.string.cancel).toUpperCase(), null);
builder.setPositiveButton(getActivity().getString(R.string.ok_action).toUpperCase(), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
CardViewStyle cardViewStyle;
switch (rGroup.getCheckedRadioButtonId()) {
case R.id.radio_card_material:
default:
cardViewStyle = CardViewStyle.MATERIAL;
break;
case R.id.radio_card_flat:
cardViewStyle = CardViewStyle.FLAT;
break;
case R.id.radio_card_compact:
cardViewStyle = CardViewStyle.COMPACT;
break;
}
Prefs.setCardStyle(cardViewStyle);
Prefs.setShowMediaCount(chkShowMediaCount.isChecked());
Prefs.setShowAlbumPath(chkShowAlbumPath.isChecked());
Toast.makeText(getActivity(), getActivity().getString(R.string.restart_app), Toast.LENGTH_SHORT).show();
}
});
builder.setView(dialogLayout);
builder.show();
}
}
As per Java docs
The Identifier in a EnumConstant may be used in a name to refer to the enum constant.
so we need to use the name only in case of an enum.
Change to this
switch (Prefs.getCardStyle()) {
case COMPACT: rCompact.setChecked(true); break;
case FLAT: rFlat.setChecked(true); break;
case MATERIAL: default: rMaterial.setChecked(true); break;
}
error: an enum switch case label must be the unqualified name of an enumeration constant
error: duplicate case label
no compiling, help me!
public class CardViewStyleSetting extends ThemedSetting {
public CardViewStyleSetting(ThemedActivity activity) {
super(activity);
}
public void show() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), getActivity().getDialogStyle());
final View dialogLayout = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_album_card_style, null);
TextView dialogTitle = dialogLayout.findViewById(R.id.dialog_card_view_style_title);
((CardView) dialogLayout.findViewById(R.id.dialog_card_view_style)).setCardBackgroundColor(getActivity().getCardBackgroundColor());
dialogTitle.setBackgroundColor(getActivity().getPrimaryColor());
final RadioGroup rGroup = dialogLayout.findViewById(R.id.radio_group_card_view_style);
final CheckBox chkShowMediaCount = dialogLayout.findViewById(R.id.show_media_count);
final CheckBox chkShowAlbumPath = dialogLayout.findViewById(R.id.show_album_path);
RadioButton rCompact = dialogLayout.findViewById(R.id.radio_card_compact);
RadioButton rFlat = dialogLayout.findViewById(R.id.radio_card_flat);
RadioButton rMaterial = dialogLayout.findViewById(R.id.radio_card_material);
chkShowMediaCount.setChecked(Prefs.showMediaCount());
chkShowAlbumPath.setChecked(Prefs.showAlbumPath());
getActivity().themeRadioButton(rCompact);
getActivity().themeRadioButton(rFlat);
getActivity().themeRadioButton(rMaterial);
getActivity().themeCheckBox(chkShowMediaCount);
getActivity().themeCheckBox(chkShowAlbumPath);
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
final View v;
switch (i) {
case R.id.radio_card_compact:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.COMPACT.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(ColorPalette.getTransparentColor(getActivity().getBackgroundColor(), 150));
break;
case R.id.radio_card_flat:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.FLAT.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(ColorPalette.getTransparentColor(getActivity().getBackgroundColor(), 150));
break;
case R.id.radio_card_material: default:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.MATERIAL.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(getActivity().getCardBackgroundColor());
break;
}
ImageView img = v.findViewById(R.id.album_preview);
img.setBackgroundColor(getActivity().getPrimaryColor());
Glide.with(getActivity())
.load(R.drawable.donald_header)
.into(img);
String hexPrimaryColor = ColorPalette.getHexColor(getActivity().getPrimaryColor());
String hexAccentColor = ColorPalette.getHexColor(getActivity().getAccentColor());
if (hexAccentColor.equals(hexPrimaryColor))
hexAccentColor = ColorPalette.getHexColor(ColorPalette.getDarkerColor(getActivity().getAccentColor()));
String textColor = getActivity().getBaseTheme().equals(Theme.LIGHT) ? "#2B2B2B" : "#FAFAFA";
String albumPhotoCountHtml = "<b><font color='" + hexAccentColor + "'>420</font></b>";
((TextView) v.findViewById(R.id.album_media_count)).setText(StringUtils.html(albumPhotoCountHtml));
((TextView) v.findViewById(R.id.album_media_label)).setTextColor(getActivity().getTextColor());
((TextView) v.findViewById(R.id.album_path)).setTextColor(getActivity().getSubTextColor());
v.findViewById(R.id.ll_media_count).setVisibility( chkShowMediaCount.isChecked() ? View.VISIBLE : View.GONE);
chkShowMediaCount.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
v.findViewById(R.id.ll_media_count).setVisibility(b ? View.VISIBLE : View.GONE);
}
});
v.findViewById(R.id.album_path).setVisibility( chkShowAlbumPath.isChecked() ? View.VISIBLE : View.GONE);
chkShowAlbumPath.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
v.findViewById(R.id.album_path).setVisibility(b ? View.VISIBLE : View.GONE);
}
});
((TextView) v.findViewById(R.id.album_name)).setText(StringUtils.html("<i><font color='" + textColor + "'>PraiseDuarte</font></i>"));
((TextView) v.findViewById(R.id.album_path)).setText("~/home/PraiseDuarte");
((CardView) v).setUseCompatPadding(true);
((CardView) v).setRadius(2);
((LinearLayout) dialogLayout.findViewById(R.id.ll_preview_album_card)).removeAllViews();
((LinearLayout) dialogLayout.findViewById(R.id.ll_preview_album_card)).addView(v);
}
});
switch (Prefs.getCardStyle()) {
case CardViewStyle.COMPACT: rCompact.setChecked(true); break;
case CardViewStyle.FLAT: rFlat.setChecked(true); break;
case CardViewStyle.MATERIAL: default: rMaterial.setChecked(true); break;
}
builder.setNegativeButton(getActivity().getString(R.string.cancel).toUpperCase(), null);
builder.setPositiveButton(getActivity().getString(R.string.ok_action).toUpperCase(), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
CardViewStyle cardViewStyle;
switch (rGroup.getCheckedRadioButtonId()) {
case R.id.radio_card_material:
default:
cardViewStyle = CardViewStyle.MATERIAL;
break;
case R.id.radio_card_flat:
cardViewStyle = CardViewStyle.FLAT;
break;
case R.id.radio_card_compact:
cardViewStyle = CardViewStyle.COMPACT;
break;
}
Prefs.setCardStyle(cardViewStyle);
Prefs.setShowMediaCount(chkShowMediaCount.isChecked());
Prefs.setShowAlbumPath(chkShowAlbumPath.isChecked());
Toast.makeText(getActivity(), getActivity().getString(R.string.restart_app), Toast.LENGTH_SHORT).show();
}
});
builder.setView(dialogLayout);
builder.show();
}
}
As per Java docs
The Identifier in a EnumConstant may be used in a name to refer to the enum constant.
so we need to use the name only in case of an enum.
Change to this
switch (Prefs.getCardStyle()) {
case COMPACT: rCompact.setChecked(true); break;
case FLAT: rFlat.setChecked(true); break;
case MATERIAL: default: rMaterial.setChecked(true); break;
}
error: an enum switch case label must be the unqualified name of an enumeration constant
error: duplicate case label
no compiling, help me!
public class CardViewStyleSetting extends ThemedSetting {
public CardViewStyleSetting(ThemedActivity activity) {
super(activity);
}
public void show() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), getActivity().getDialogStyle());
final View dialogLayout = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_album_card_style, null);
TextView dialogTitle = dialogLayout.findViewById(R.id.dialog_card_view_style_title);
((CardView) dialogLayout.findViewById(R.id.dialog_card_view_style)).setCardBackgroundColor(getActivity().getCardBackgroundColor());
dialogTitle.setBackgroundColor(getActivity().getPrimaryColor());
final RadioGroup rGroup = dialogLayout.findViewById(R.id.radio_group_card_view_style);
final CheckBox chkShowMediaCount = dialogLayout.findViewById(R.id.show_media_count);
final CheckBox chkShowAlbumPath = dialogLayout.findViewById(R.id.show_album_path);
RadioButton rCompact = dialogLayout.findViewById(R.id.radio_card_compact);
RadioButton rFlat = dialogLayout.findViewById(R.id.radio_card_flat);
RadioButton rMaterial = dialogLayout.findViewById(R.id.radio_card_material);
chkShowMediaCount.setChecked(Prefs.showMediaCount());
chkShowAlbumPath.setChecked(Prefs.showAlbumPath());
getActivity().themeRadioButton(rCompact);
getActivity().themeRadioButton(rFlat);
getActivity().themeRadioButton(rMaterial);
getActivity().themeCheckBox(chkShowMediaCount);
getActivity().themeCheckBox(chkShowAlbumPath);
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
final View v;
switch (i) {
case R.id.radio_card_compact:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.COMPACT.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(ColorPalette.getTransparentColor(getActivity().getBackgroundColor(), 150));
break;
case R.id.radio_card_flat:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.FLAT.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(ColorPalette.getTransparentColor(getActivity().getBackgroundColor(), 150));
break;
case R.id.radio_card_material: default:
v = LayoutInflater.from(getActivity()).inflate(CardViewStyle.MATERIAL.getLayout(), null);
v.findViewById(R.id.ll_album_info).setBackgroundColor(getActivity().getCardBackgroundColor());
break;
}
ImageView img = v.findViewById(R.id.album_preview);
img.setBackgroundColor(getActivity().getPrimaryColor());
Glide.with(getActivity())
.load(R.drawable.donald_header)
.into(img);
String hexPrimaryColor = ColorPalette.getHexColor(getActivity().getPrimaryColor());
String hexAccentColor = ColorPalette.getHexColor(getActivity().getAccentColor());
if (hexAccentColor.equals(hexPrimaryColor))
hexAccentColor = ColorPalette.getHexColor(ColorPalette.getDarkerColor(getActivity().getAccentColor()));
String textColor = getActivity().getBaseTheme().equals(Theme.LIGHT) ? "#2B2B2B" : "#FAFAFA";
String albumPhotoCountHtml = "<b><font color='" + hexAccentColor + "'>420</font></b>";
((TextView) v.findViewById(R.id.album_media_count)).setText(StringUtils.html(albumPhotoCountHtml));
((TextView) v.findViewById(R.id.album_media_label)).setTextColor(getActivity().getTextColor());
((TextView) v.findViewById(R.id.album_path)).setTextColor(getActivity().getSubTextColor());
v.findViewById(R.id.ll_media_count).setVisibility( chkShowMediaCount.isChecked() ? View.VISIBLE : View.GONE);
chkShowMediaCount.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
v.findViewById(R.id.ll_media_count).setVisibility(b ? View.VISIBLE : View.GONE);
}
});
v.findViewById(R.id.album_path).setVisibility( chkShowAlbumPath.isChecked() ? View.VISIBLE : View.GONE);
chkShowAlbumPath.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
v.findViewById(R.id.album_path).setVisibility(b ? View.VISIBLE : View.GONE);
}
});
((TextView) v.findViewById(R.id.album_name)).setText(StringUtils.html("<i><font color='" + textColor + "'>PraiseDuarte</font></i>"));
((TextView) v.findViewById(R.id.album_path)).setText("~/home/PraiseDuarte");
((CardView) v).setUseCompatPadding(true);
((CardView) v).setRadius(2);
((LinearLayout) dialogLayout.findViewById(R.id.ll_preview_album_card)).removeAllViews();
((LinearLayout) dialogLayout.findViewById(R.id.ll_preview_album_card)).addView(v);
}
});
switch (Prefs.getCardStyle()) {
case CardViewStyle.COMPACT: rCompact.setChecked(true); break;
case CardViewStyle.FLAT: rFlat.setChecked(true); break;
case CardViewStyle.MATERIAL: default: rMaterial.setChecked(true); break;
}
builder.setNegativeButton(getActivity().getString(R.string.cancel).toUpperCase(), null);
builder.setPositiveButton(getActivity().getString(R.string.ok_action).toUpperCase(), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
CardViewStyle cardViewStyle;
switch (rGroup.getCheckedRadioButtonId()) {
case R.id.radio_card_material:
default:
cardViewStyle = CardViewStyle.MATERIAL;
break;
case R.id.radio_card_flat:
cardViewStyle = CardViewStyle.FLAT;
break;
case R.id.radio_card_compact:
cardViewStyle = CardViewStyle.COMPACT;
break;
}
Prefs.setCardStyle(cardViewStyle);
Prefs.setShowMediaCount(chkShowMediaCount.isChecked());
Prefs.setShowAlbumPath(chkShowAlbumPath.isChecked());
Toast.makeText(getActivity(), getActivity().getString(R.string.restart_app), Toast.LENGTH_SHORT).show();
}
});
builder.setView(dialogLayout);
builder.show();
}
}
As per Java docs
The Identifier in a EnumConstant may be used in a name to refer to the enum constant.
so we need to use the name only in case of an enum.
Change to this
switch (Prefs.getCardStyle()) {
case COMPACT: rCompact.setChecked(true); break;
case FLAT: rFlat.setChecked(true); break;
case MATERIAL: default: rMaterial.setChecked(true); break;
}
Switch statment fix:
The switch statement is only returning the last case i.e case 4, "#0R0dfdf0FF". how can i fix this so the text view shows the the one clicked in the dialogue box?
I'm a total newbie so yes help would really be appreciated.
public class NoteEdit extends Activity {
public EditText mTitleText;
public EditText mBodyText;
public EditText mColor;
private NotesDbAdapter mDbHelper;
private static final int DIALOG_ALERT = 10;
Long mRowId;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.note_edit);
setTitle(R.string.done);
mTitleText = (EditText) findViewById(R.id.editTitle);
mBodyText = (EditText) findViewById(R.id.editNote);
mColor = (EditText) findViewById(R.id.editColor);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
}
populateFields();
setupActionBar();
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
setResult(RESULT_OK);
finish();
}
return super.onOptionsItemSelected(item);
}
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
mBodyText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
mColor.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_COLOR)));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}
#Override
protected void onPause() {
super.onPause();
saveState();
}
#Override
protected void onResume() {
super.onResume();
populateFields();
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
String color = mColor.getText().toString();
if (mRowId == null) {
long id = mDbHelper.createNote(title, body, color);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title, body, color);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.add:
showDialog(DIALOG_ALERT);
return true;
}
return super.onMenuItemSelected(featureId, item);
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
// Create out AlterDialog
android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] colors = {"Blue", "Green", "Yellow", "Red", "Purple"};
builder.setTitle(R.string.body);
builder.setItems(colors, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which){
case 0:
mColor.setText("#000000");
case 1:
mColor.setText("#0000FF");
case 2:
mColor.setText("#0R00FF");
case 3:
mColor.setText("#0R00dsdFF");
case 4:
mColor.setText("#0R0dfdf0FF");
default:
break;
}
} });
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
}
You are missing break; at the end of the switch branches.
Fall Through.
You have to add the break.
case 0:
mColor.setText("#000000");
break;
You can find that in docs
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
You need a break when you don't have a return otherwise it causes fall through
You need the break; after all cases except for the last one or else it'll fall through case by case
switch (which){
case 0:
mColor.setText("#000000");
break;
case 1:
mColor.setText("#0000FF");
break;
case 2:
mColor.setText("#0R00FF");
break;
case 3:
mColor.setText("#0R00dsdFF");
break;
case 4:
mColor.setText("#0R0dfdf0FF");
default:
break;
}
I've created a menu with a intents to access different activities, but I have a strange behavior, it always goes through all the cases of the switch statement after the statement selected , I've reviewed the value of the variable item and is correct, any ideas what could be wrong?
the snippet of code that represents the menu is:
public static final int wiifidi = 0;
public static final int cuentaint = 1;
public static final int cajerosint = 2;
public static final int indicadoresint = 3;
public static final int promoint = 5;
public static final int contactoint = 4;
....
....
....
#Override
//add the items to the menu on the class
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0,wiifidi, 0, R.string.menu_wifi);
menu.add(0,cuentaint, 0, R.string.menu_cuenta);
menu.add(0,cajerosint,0,R.string.menu_cajeros);
menu.add(0,indicadoresint,0,R.string.menu_indicadores);
menu.add(0,contactoint,0,R.string.menu_contacto);
menu.add(0,promoint,0,R.string.menu_promo);
return result;
}
#Override
//handle everything that happens when an item of menu is selected
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), "el item es " +item.getItemId(), Toast.LENGTH_LONG).show();
switch (item.getItemId()) {
case wiifidi:
wifistatus();
case cuentaint:{
consulta();
}
case cajerosint:{
cajero();
}
case indicadoresint:{
indicador();
}
case contactoint:{
contacto();
}
case promoint:{
promocion();
}
}
return super.onOptionsItemSelected(item);
}
Remember to break out of your switches.
switch (item.getItemId())
{
case wiifidi:
wifistatus();
break;
case cuentaint:
consulta();
break;
case cajerosint:
cajero();
break;
case indicadoresint:
indicador();
break;
case contactoint:
contacto();
break;
case promoint:
promocion();
break;
}
Specify break
case wiifidi:
wifistatus();
break;