Read data from xlsx file with java [duplicate] - java

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;
}

Related

Switch case is not working with Enum in Java [duplicate]

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;
}

Check if a property of a custom object ArrayList is an empty string or not?

I want to hide a WebView object (txtCode) if the code property of a custom object Arraylist (arrQues) contains nothing.
if (arrQues.get(count).code.isEmpty())
txtCode.setVisibility(View.GONE);
Its an ArrayList of custom objects fetched from a database table which is shown below
And if the code property does contains code then I have dynamically added rules to layout as shown below:
if (!(arrQues.get(count).code.isEmpty())) {
submit_params.removeRule(RelativeLayout.BELOW);
submit_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
submit_params.bottomMargin = (int) convertPxToDp(getContext(), convertDpToPx(getContext(), 15));
main_params.addRule(RelativeLayout.ABOVE, submitContainer.getId());
mainContainer.setLayoutParams(main_params);
submitContainer.setLayoutParams(submit_params);
}
The issue is when I load the second question and so on... the layout gets messed up and the current question number does not shows as 2 even if its 2 as shown in below:
Both of these issues only arises whenever I use...
arrQues.get(count).code.isEmpty() in the code
I have also tried using "" instead of isEmpty() and even null, but the result was same.
Also what I have noticed is only those questions are loaded from database which have something in the code column.
Below is the complete code for Java file
public class QuestionsFragment extends Fragment implements View.OnClickListener {
TextView txtTimer, txtStatus;
LinearLayout boxA, boxB, boxC, boxD, mainContainer;
RelativeLayout submitContainer;
RelativeLayout.LayoutParams submit_params;
RelativeLayout.LayoutParams main_params;
ScrollView scrollView;
Button btnSubmit;
DBHelper dbHelper;
SharedPreferences sharedPreferences;
TextView txtQues;
WebView txtCode;
TextView txtOptA, txtOptB, txtOptC, txtOptD;
String ans;
ArrayList<QuestionModal> arrQues = new ArrayList<>();
ArrayList<String> arrAnswers = new ArrayList<>();
CountDownTimer countDownTimer;
boolean timerSwitch;
int selectedVal, id;
int curr_quesNo = 0;
int count = 0;
int right = 0;
int non_attempted = 0;
public QuestionsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_questions, container, false);
txtQues = view.findViewById(R.id.txtQues);
txtOptA = view.findViewById(R.id.txtOptionA);
txtOptB = view.findViewById(R.id.txtOptionB);
txtOptC = view.findViewById(R.id.txtOptionC);
txtOptD = view.findViewById(R.id.txtOptionD);
txtCode = view.findViewById(R.id.txtCode);
txtStatus = view.findViewById(R.id.txtStatus);
boxA = view.findViewById(R.id.boxA);
boxB = view.findViewById(R.id.boxB);
boxC = view.findViewById(R.id.boxC);
boxD = view.findViewById(R.id.boxD);
scrollView = view.findViewById(R.id.scrollView);
btnSubmit = view.findViewById(R.id.btnSubmit);
submitContainer = view.findViewById(R.id.submitContainer);
mainContainer = view.findViewById(R.id.mainContainer);
submit_params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
main_params = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
sharedPreferences = getActivity().getSharedPreferences("PrefFile", MODE_PRIVATE);
timerSwitch = sharedPreferences.getBoolean("timer_switch", true);
selectedVal = sharedPreferences.getInt("selectedVal", 10);
dbHelper = DBHelper.getDB(getActivity(), sharedPreferences.getString("db_name", null));
if (!dbHelper.checkDB()) {
dbHelper.createDB(getActivity());
}
dbHelper.openDB();
String levelKey = sharedPreferences.getString("level_key", null);
arrQues = dbHelper.getQues(levelKey, selectedVal);
loadQues(timerSwitch);
txtTimer = view.findViewById(R.id.txtTimer);
switch (sharedPreferences.getString("db_name", null)) {
case "Android":
((MainActivity) getActivity()).setFragTitle("Android Quiz");
// topicLogo.setImageResource(R.drawable.ic_nature_people_black_24dp);
break;
case "Java":
((MainActivity) getActivity()).setFragTitle("Java Quiz");
// topicLogo.setImageResource(R.drawable.ic_nature_people_black_24dp);
break;
case "C":
((MainActivity) getActivity()).setFragTitle("C Quiz");
((MainActivity) getActivity()).setFragLogo(R.drawable.ic_home_black_24dp);
break;
case "C++":
((MainActivity) getActivity()).setFragTitle("C++ Quiz");
break;
case "Python":
((MainActivity) getActivity()).setFragTitle("Python Quiz");
break;
case "Kotlin":
((MainActivity) getActivity()).setFragTitle("Kotlin Quiz");
break;
}
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (timerSwitch)
countDownTimer.cancel();
if (id == 0) {
non_attempted++;
arrAnswers.add("NotAttempted");
Toast.makeText(getActivity(), "Not Attempted!", Toast.LENGTH_SHORT).show();
}
switch (id) {
case R.id.boxA:
arrAnswers.add("A");
break;
case R.id.boxB:
arrAnswers.add("B");
break;
case R.id.boxC:
arrAnswers.add("C");
break;
case R.id.boxD:
arrAnswers.add("D");
break;
}
if ((id == R.id.boxA && ans.equals("A"))
|| (id == R.id.boxB && ans.equals("B"))
|| (id == R.id.boxC && ans.equals("C"))
|| (id == R.id.boxD && ans.equals("D"))) {
right++;
count++;
Toast.makeText(getActivity(), "RIGHT!", Toast.LENGTH_SHORT).show();
if (count < arrQues.size()) {
loadQues(timerSwitch);
} else {
sendResult();
}
} else {
count++;
if (count < arrQues.size()) {
loadQues(timerSwitch);
} else {
sendResult();
}
}
}
});
return view;
}
public void setBtnDefault() {
boxA.setBackgroundColor(getResources().getColor(android.R.color.transparent));
boxB.setBackgroundColor(getResources().getColor(android.R.color.transparent));
boxC.setBackgroundColor(getResources().getColor(android.R.color.transparent));
boxD.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
public void sendResult() {
int attempted = selectedVal - non_attempted;
Gson gson = new Gson();
String jsonAnswers = gson.toJson(arrAnswers);
String jsonQues = gson.toJson(arrQues);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("right_key", right);
editor.putInt("wrong_key", attempted - right);
editor.putInt("total_key", selectedVal);
editor.putInt("attempted_key", attempted);
editor.putString("arr_answers", jsonAnswers);
editor.putString("arr_ques", jsonQues);
editor.commit();
((MainActivity) getActivity()).AddFrag(new ResultFragment(), 1);
}
public void LoadTimer() {
countDownTimer = new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
txtTimer.setText("0:" + millisUntilFinished / 1000);
}
#SuppressLint("SetTextI18n")
#Override
public void onFinish() {
txtTimer.setText("Time Over");
}
};
}
#SuppressLint("NewApi")
public void loadQues(boolean timer_switch) {
try {
id = 0;
setBtnDefault();
if (timer_switch) {
LoadTimer();
countDownTimer.start();
}
curr_quesNo++;
txtStatus.setText(curr_quesNo + "/" + selectedVal);
txtOptC.setVisibility(View.VISIBLE);
txtOptD.setVisibility(View.VISIBLE);
txtCode.setVisibility(View.VISIBLE);
main_params.removeRule(RelativeLayout.ABOVE);
submit_params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
submit_params.addRule(RelativeLayout.BELOW, mainContainer.getId());
submit_params.topMargin = (int) convertPxToDp(getContext(), convertDpToPx(getContext(), 70));
mainContainer.setLayoutParams(main_params);
submitContainer.setLayoutParams(submit_params);
txtQues.setText(arrQues.get(count).ques);
txtOptA.setText(arrQues.get(count).optionA);
txtOptB.setText(arrQues.get(count).optionB);
txtOptC.setText(arrQues.get(count).optionC);
txtOptD.setText(arrQues.get(count).optionD);
txtCode.loadDataWithBaseURL(null, arrQues.get(count).code, "text/html", null, null);
if (txtOptC.getText().toString().isEmpty())
txtOptC.setVisibility(View.GONE);
if (txtOptD.getText().toString().isEmpty())
txtOptD.setVisibility(View.GONE);
if (arrQues.get(count).code.isEmpty())
txtCode.setVisibility(View.GONE);
if (!(arrQues.get(count).code.isEmpty())) {
submit_params.removeRule(RelativeLayout.BELOW);
submit_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
submit_params.bottomMargin = (int) convertPxToDp(getContext(), convertDpToPx(getContext(), 15));
main_params.addRule(RelativeLayout.ABOVE, submitContainer.getId());
mainContainer.setLayoutParams(main_params);
submitContainer.setLayoutParams(submit_params);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
scrollView.arrowScroll(View.FOCUS_DOWN);
}
}, 1000);
}
ans = arrQues.get(count).answer;
boxA.setOnClickListener(this);
boxB.setOnClickListener(this);
boxC.setOnClickListener(this);
boxD.setOnClickListener(this);
} catch (Exception e) {
((MainActivity) getActivity()).AddFrag(new QuestionsFragment(), 1);
}
}
#Override
public void onClick(View v) {
setBtnDefault();
id = v.getId();
v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
public float convertDpToPx(Context context, float dp) {
return dp * context.getResources().getDisplayMetrics().density;
}
public float convertPxToDp(Context context, float px) {
return px / context.getResources().getDisplayMetrics().density;
}
}
I solved it, all issues were happening because arrQues.get(count).code was fetching null values from the database(The column "Code" had null values). As soon as I replaced null values with empty strings "" isEmpty() worked perfectly. I guess isEmpty() doesn't work with null values and is only intended for empty strings.

error: an enum switch case label must be the unqualified name of an enumeration constant

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 statement just returning the last case

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;
}

Android: how to create Switch case from this?

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);

Categories