How to set values for Radio button in android? - java

I am getting the value from DB and setting it to the respective button in the below format. Is there any optimised way to do the same. All these radio buttons are inside a radio group.
if (bundlevalue.get(3).equalsIgnoreCase("Mr.")) {
rg_nametitle.check(R.id.mr);
} else if (bundlevalue.get(3).equalsIgnoreCase("Mrs.")) {
rg_nametitle.check(R.id.mrs);
} else if (bundlevalue.get(3).equalsIgnoreCase("Ms.")) {
rg_nametitle.check(R.id.ms);
} else {
rg_nametitle.check(R.id.messrs);
}

You can try as follows...
String value = bundlevalue.get(3)
Resources res = getResources();
if (value.equalsIgnoreCase("Mr.") || value.equalsIgnoreCase("Mrs.") || value.equalsIgnoreCase("Ms.")) {
String[] splitedValue = value.toLowerCase ().split(".");
int id = res.getIdentifier(splitedValue[0], "id", getContext().getPackageName());
rg_nametitle.check(id);
} else {
rg_nametitle.check(R.id.messrs);
}

In case if you use XML attribute like this :
<RadioGroup
...
...
android:checkedButton="#+id/IdOfTheRadioButtonInsideThatTobeChecked"
... >....</RadioGroup>
or you can use switch-case statement like this :
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}

Use switch statement. Although, there is nothing big difference in using if-else or switch, you can go ahead with whichever is more readable to you.
public enum Title
{
Mr, Mrs, Ms;
}
String title = bundlevalue.get(3).equalsIgnoreCase("Mr.");
switch(Title.valueOf(title)) {
case Mr:
rg_nametitle.check(R.id.mr);
break;
case Ms:
rg_nametitle.check(R.id.ms);
break;
case Mrs:
rg_nametitle.check(R.id.mrs);
break;
default:
break;
}

Related

Why is the text in TextViews changing two times when I am calling it only once at a time?

I know setText just changes the text only once but I can't seem to find the reason why the text in it changes before I move on to the next question in the quizActivity
What I have made is an app with one activity in it which has a quiz in it, a question is displayed along with 4 options. When the user selects an option, if that option is correct then it becomes green and red otherwise and additionally, I then open a dialog box showing whether the answer was right or wrong and then the question is changed when the user clicks Next on the dialog box.
But what is happening that when the user selects an option, in between the process of clicking the option and then clicking next on the dialog box, the text in the questions and the options changes and I can't seem to figure out why is that happening. In total, the question and options change two times when they should change only once, the unexpected change is when the user clicks on an option and the dialog box opens.
Here's the code:
#Override
public void onClick(View view) {
int selectedOption = 0;
switch (view.getId()) {
case R.id.option_1_tile:
selectedOption = 1;
break;
case R.id.option_2_tile:
selectedOption = 2;
break;
case R.id.option_3_tile:
selectedOption = 3;
break;
case R.id.option_4_tile:
selectedOption = 4;
break;
default:
}
checkAnswer(selectedOption, view);
}
Here's the function which checks the answer:
private void checkAnswer(int selectedOption, View view) {
if (selectedOption == selected_questions.get(quesNum).getAnswer()) {
//Right Answer
(view).setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
quizReference.child(selected_questions.get(quesNum).getId()).child("correct_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getCorrect_attempts()) + 1));
quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));
score++;
correctDialog();
} else {
//Wrong Answer
(view).setBackgroundTintList(ColorStateList.valueOf(Color.RED));
quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));
switch (selected_questions.get(quesNum).getAnswer()) {
case 1:
options[0].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 2:
options[1].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 3:
options[2].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 4:
options[3].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
}
wrongDialog ();
}
}
Here's the function which changes the question:
private void changeQuestion() {
resetColor ();
if (quesNum < selected_questions.size() - 1) {
quesNum++;
playAnim(question, 0, 0);
playAnim(option1_text, 0, 1);
playAnim(option2_text, 0, 2);
playAnim(option3_text, 0, 3);
playAnim(option4_text, 0, 4);
qCount.setText(String.valueOf(quesNum + 1) + "/" + String.valueOf(selected_questions.size()));
} else {
// Go to Score Activity
Intent intent = new Intent(quizActivity.this, scoreActivity.class);
intent.putExtra("SCORE", String.valueOf(score) + "/" + String.valueOf(selected_questions.size()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
Here's the function which sets the text and animation:
private void playAnim(final View view, final int value, final int viewNum) {
view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500)
.setStartDelay(100).setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
if (value == 0) {
switch (viewNum) {
case 0:
((TextView) view).setText(selected_questions.get(quesNum).getQuestion());
break;
case 1:
((TextView) view).setText(selected_questions.get(quesNum).getOption1());
break;
case 2:
((TextView) view).setText(selected_questions.get(quesNum).getOption2());
break;
case 3:
((TextView) view).setText(selected_questions.get(quesNum).getOption3());
break;
case 4:
((TextView) view).setText(selected_questions.get(quesNum).getOption4());
break;
}
if (viewNum != 0)
(view).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#E99C03")));
playAnim(view, 1, viewNum);
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
Here's the code for the dialog boxes:
public void wrongDialog() {
final Dialog dialogWrong = new Dialog(quizActivity.this);
dialogWrong.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogWrong.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogWrong.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogWrong.setContentView(R.layout.dialog_wrong);
dialogWrong.setCancelable(false);
dialogWrong.show();
TextView wrongText = (TextView) dialogWrong.findViewById(R.id.wrongText);
Button buttonNext = (Button) dialogWrong.findViewById(R.id.dialogNext);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogWrong.dismiss();
//reset the color of buttons back to white
resetColor();
//Change question
changeQuestion();
}
});
}
public void correctDialog() {
final Dialog dialogCorrect = new Dialog(quizActivity.this);
dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogCorrect.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogCorrect.setContentView(R.layout.dialog_correct);
dialogCorrect.setCancelable(false);
dialogCorrect.show();
TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
Button buttonNext = (Button) dialogCorrect.findViewById(R.id.dialogNext);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogCorrect.dismiss();
//reset the color of buttons back to white
resetColor();
//it will increment the question number
changeQuestion();
}
});
}
I have tried to explain it to my best ability though I would be glad to answer any additional information/code you may want. Also, this is the link for the project if you have the time to run it and understand the problem better.
I have checked your code. you have placed an addValueEventListener in setUpdates method. When you select an option, you update the firestore database by setting fields like total attempts. As a result, eventListener gets triggered and "selectQuestionSet" function is called.
Hence, every time you select an option, selectQuestionSet function is called. You should make sure that its called only once at the start.

Preventing a second click on RadioButton if it was already checked

I am creating a small quiz app. As the question suggests, how do I prevent a second click without disabling the button? When I click twice on a radio button it adds 2 points instead of 1. Very much appreciated!
Here's a picture of my app:
public void question1 (View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question1_9:
if (checked) {
scoreForRadioButtons += 1;
}
case R.id.question1_8:
if (checked) {
break;
}
case R.id.question1_7:
if (checked) {
break;
}
}
}
SOLVED
This is what I did: just add 'break;' on the correct answer.
public void question1 (View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question1_9:
if (checked) {
pointForQ1 = 1;
break;
}
case R.id.question1_8:
if (checked) {
pointForQ1 = 0;
}
case R.id.question1_7:
if (checked) {
pointForQ1 = 0;
}
}
}
Short Answer:
You can set the clickable attribute to false for the RadioButton after it is clicked if you really want to using the following:
myRadioButton.setClickable(false);
Long Answer:
Don't use radio buttons for this. Users should be able to click a radio button as many times as they want without anything happening like incrementing the score in your case. It's a standard convention of a radio button that once selected, no new code is executed if it is selected again and again. Note this is only if the state of the radio button hasn't changed during selections (state meaning whether it's selected or not).
The way your app is using (or wants to use) RadioButtons is not correct. I'd recommend using buttons for this, where you could do the following:
myButton.setEnabled(false);
Edit:
If you want to make it visible to the user that they clicked a button, you can do a few things (change it's text, change the background colour). For this example, you can change the background colour with this:
myButton.setBackgroundColor(Color.GREEN); // or whatever colour you choose
If you want to do this add the following to your imports:
import android.graphics.Color
If you just don't want to click that radio button two times, After radio button is clicked,
add view.setEnabled(false);or
view.setClickable(false); to prevent it click again
To prevent the second click on a checked radio button, make that radio disabled
radioButton.setEnabled(false);
Disable a clicked button and enable it when another is checked
public void question1 (View view) {
//will enable all buttons
Integer[] buttons = {R.id.question1_9, R.id.question1_8, R.id.question1_7};
for (int i = 0; i < buttons.length; i++) {
findViewById(buttons[i]).setEnabled(true);
}
RadioButton button = (RadioButton) view;
if (button.isChecked()){
switch (view.getId()) {
case R.id.question1_9:
scoreForRadioButtons ++;
//will disable clicked button
button.setEnabled(false);
break;
case R.id.question1_8:
break;
case R.id.question1_7:
break;
}
}

How do I prevent my submit button in android from giving me multiple scores when clicked multiple times?

Whenever I answer a question and hit the submit button, a score of 1 should show but on hitting submit button again, it keeps adding 1 to the score and more hits on the submit button keeps adding more 1 to the score. I actually don't want 1 to be added to the score every time the submit button is clicked. I do not want the score to be updated based on the number of times I hit the submit button.
package com.example.android.generalknowledge;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int baseScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitResult(View view) {
RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
boolean answerIsLargestRailwayStation = largestRailwayStation.isChecked();
RadioButton insects = (RadioButton) findViewById(R.id.insects);
boolean answerIsInsects = insects.isChecked();
CheckBox physicsChemistry = (CheckBox) findViewById(R.id.physics_chemistry);
boolean physicsChemistryIsAnswer = physicsChemistry.isChecked();
CheckBox physiologyMedicine = (CheckBox) findViewById(R.id.physiology_medicine);
boolean physiologyMedicineIsAnswer = physiologyMedicine.isChecked();
CheckBox literature = (CheckBox) findViewById(R.id.literature);
boolean literatureIsAnswer = literature.isChecked();
CheckBox peaceEconomics = (CheckBox) findViewById(R.id.peace_economics);
boolean peaceEconomicsIsAnswer = peaceEconomics.isChecked();
RadioButton naziParty = (RadioButton) findViewById(R.id.nazi_party);
boolean answerIsNaziParty = naziParty.isChecked();
RadioButton allOfTheAbove = (RadioButton) findViewById(R.id.all_of_the_above);
boolean answerIsAll = allOfTheAbove.isChecked();
RadioButton filmFinance = (RadioButton) findViewById(R.id.film_finance);
boolean answerIsFilmFinance = filmFinance.isChecked();
EditText enterNameHere = (EditText) findViewById(R.id.name_view);
String name = enterNameHere.getText().toString();
EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
String answer = enterAnswerHere.getText().toString();
if (enterAnswerHere.getText().toString().equals("Africa")) {
baseScore += 1 ;
}
int finalScore = calculateTotalScore(answerIsLargestRailwayStation, answerIsInsects, physicsChemistryIsAnswer,
physiologyMedicineIsAnswer, literatureIsAnswer, peaceEconomicsIsAnswer, answerIsNaziParty, answerIsAll, answerIsFilmFinance);
Toast.makeText(MainActivity.this,
name + " " + "\n" + "You have a Total Score of " + " " + finalScore + " " + "/10", Toast.LENGTH_LONG).show();
}
/**
* This method is called when any of the radio buttons in question one is selected
*/
public void onRadioButtonClickedOne(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.largest_railway_station:
if (checked)
// This is the correct answer
break;
case R.id.highest_railway_station:
if (checked)
// Wrong answer
break;
case R.id.longest_railway_station:
if (checked)
// Wrong answer
break;
case R.id.none_of_the_above:
if (checked)
//Wrong answer
break;
}
}
/**
* This method is called when any of the radio buttons in question two is selected
*/
public void onRadioButtonClickedTwo(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.behaviour_of_human_beings:
if (checked)
// Wrong answer
break;
case R.id.insects:
if (checked)
// This is the correct answer
break;
case R.id.origin_history:
if (checked)
// Wrong answer
break;
case R.id.rock_formation:
if (checked)
// Wrong answer
break;
}
}
/**
* This method is called when the checkboxes for question three are clicked
*/
public void onCheckboxThreeClicked(View view) {
//Is the button now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox is clicked
switch (view.getId()) {
case R.id.physics_chemistry:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.physiology_medicine:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.literature:
if (checked)
// One of the Correct answers
break;
}
switch (view.getId()) {
case R.id.peace_economics:
if (checked)
// One of the Correct answers
break;
}
}
/**
* This method is called when any of the radio buttons in question four is selected
*/
public void onRadioButtonClickedFour(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.labour_party:
if (checked)
// Wrong answer
break;
case R.id.nazi_party:
if (checked)
// This is the correct answer
break;
case R.id.leading_party:
if (checked)
// Wrong answer
break;
case R.id.democratic_party:
if (checked)
// Wrong answer
break;
}
}
/**
* This method is called when any of the radio buttons in question six is selected
*/
public void onRadioButtonClickedSix(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.develop_telescope:
if (checked)
// Wrong answer
break;
case R.id.discovered_jupiter:
if (checked)
// Wrong answer
break;
case R.id.movement_of_pendulum:
if (checked)
// Wrong answer
break;
case R.id.all_of_the_above:
if (checked)
// This is the correct answer
break;
}
}
/**
* This method is called when any of the radio buttons in question seven is selected
*/
public void onRadioButtonClickedSeven(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.foreign_finance:
if (checked)
// Wrong answer
break;
case R.id.film_finance:
if (checked)
// This is the correct answer
break;
case R.id.federation_football:
if (checked)
// Wrong answer
break;
case R.id.none:
if (checked)
// Wrong answer
break;
}
}
private int calculateTotalScore(boolean questionOneAnswer, boolean questionTwoAnswer, boolean questionThreeAnswer1,
boolean questionThreeAnswer2, boolean questionThreeAnswer3, boolean questionThreeAnswer4,
boolean questionFourAnswer, boolean questionSixAnswer, boolean questionSevenAnswer) {
if (questionOneAnswer) {
baseScore += 1 ;
}
if (questionTwoAnswer) {
baseScore += 1 ;
}
if (questionThreeAnswer1) {
baseScore += 1 ;
}
if (questionThreeAnswer2) {
baseScore += 1 ;
}
if (questionThreeAnswer3) {
baseScore += 1 ;
}
if (questionThreeAnswer4) {
baseScore += 1 ;
}
if (questionFourAnswer) {
baseScore += 1 ;
}
if (questionSixAnswer) {
baseScore += 1 ;
}
if (questionSevenAnswer) {
baseScore += 1 ;
}
// EditText enterAnswerHere = (EditText) findViewById(R.id.answer_here);
// String answer = enterAnswerHere.getText().toString();
// {
// if (answer == "Africa") {
// baseScore = baseScore + 1;
// }
// }
return baseScore;
}
}
We can achieve by 2 ways,
You can diable the button by button.setEnable(false); once user click for for time
In your case if you don't have button object so you can use one global boolean and make that as false default and once user click for first time do the process and make that boolean as true, from next time check if the boolean is true just return it.
private boolean mIsSubmited = false;
public void submitResult(View view) {
if(mIsSubmited) {
return;
}
mIsSubmited = true;
RadioButton largestRailwayStation = (RadioButton) findViewById(R.id.largest_railway_station);
// Remaining code..
}
You can disable the submit button once it is clicked. For eg:
onclick() {
submitbutton.setEnabled(false);
}
Prevent user to click on submit again and again.
You can enable it back using:
submitbutton.setEnabled(true);
You can declare a Boolean globally and set it to false at first.
then on button click you can turn its value to true.
on the button click you can check if its false then only to increase the score.
You can use handler for this may be there is better solution then this.
Handler mHandler=new Handler();
//now in your click listener
mHandler.removeCallbacks(mRunnable);//
mHandler.postDelayed(mRunnable, 500);
Runnable mRunnable =new Runnable() {
#Override
public void run() {
//calculate it
}
};
//Wait for half sec before calculating the result if user clicked before then stop runnable from calcualting and again go for calculation.
Hope it helps.

Making Quiz App in Android with Radio Buttons

I'm making a Quizz App for Android with 10 Questions, all of them with 4 Radio Buttons, and one button at the end to show the score. The problem is when I choose the correct answer it gives 5 points, but if I check another radio button the points will stay 5 and if I press again it sums 5. What is the best way to code this?
Here is the code:
package com.example.android.quizproject;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int points = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void firstRadioButtons (View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.questionOneA:
if (checked)
points += 0;
break;
case R.id.questionOneB:
if (checked)
points += 0;
break;
case R.id.questionOneC:
if (checked)
points += 5;
break;
case R.id.questionOneD:
if (checked)
points += 0;
break;
}
}
public void showScore (View view) {
TextView scoreTextView = (TextView) findViewById(R.id.score);
scoreTextView.setText(" " + points);
}
}
You can make use of a counter vvariable which checks if the question has been previousy answered or not. Modify part of your code to this
public void firstRadioButtons (View view){
boolean checked = ((RadioButton) view).isChecked();
int count=0;
switch (view.getId()) {
case R.id.questionOneA:
if (checked)
{
if(count!=0){
points-=5;
count=0;
}
}
break;
case R.id.questionOneB:
if (checked)
{
if(count!=0){
points-=5;
count=0;
}
}
break;
case R.id.questionOneC:
if (checked){
points += 5;
count+=1;}
break;
case R.id.questionOneD:
if (checked)
{
if(count!=0){
points-=5;
count=0;
}
}
break;
}
}
Actually, the way you described it, it's common sense. If you click the right answer once, it will set it to 5, but if you press any other it will add 0 to it.
In general, it will print out 5 since you got the answer correct once, and the other questions are set to 0. There's really nothing to fix here, it's kind of common sense that your variable wouldn't read other than 5. Just like Abhriya said, you could add a counter increment value as done in ( her / his ) example.

switch-case does not work properly

i the below posted code, when I leave the field ip blank/empty and give values to the other fields, the toast always gives message the KATimer is invalid or missing.
i expected to see a toast showing with a message indicating the empty field,but the below code, if any field is empty, it always says KATimer is invalid or empty.
why that is happeneing, i am missing something
Code:
btnStubView_Connect:
btnStubView_Connect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isValidMQTTConfigs(etStubView_ip) &&
isValidMQTTConfigs(etStubView_port) &&
isValidMQTTConfigs(etStubView_ClientID) &&
isValidMQTTConfigs(etStubView_KATimer)) {
Log.d(TAG, "#btnStubView_ConnectListener(): all entries are valid");
setCSession(cbStubView_CS.isChecked()); // set the current state of the cleanSession checkBox.
addToContentValues();
Log.d(TAG, "#btnStubView_ConnectListener(): all entries added toContentValues");
} else {
Log.w(TAG, "#btnStubView_ConnectListener(): one or more entry(s) is invalid or left blank.");
}
}
});
isValidMQTTConfigs:
protected boolean isValidMQTTConfigs(View view) {
// TODO Auto-generated method stub
boolean valid = false;
String viewName = "";
switch(view.getId()) {
case R.id.etSubView_ip:
viewName = "IP";
if (isDuly( ((EditText) view).getText().toString())) {
this.setIP(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_port:
viewName = "Port";
if (isDuly( ((EditText) view).getText().toString())) {
this.setPort(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_clientID:
viewName = "clientID";
if (isDuly( ((EditText) view).getText().toString())) {
this.setClienID(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_KATimer:
viewName = "KAtimer";
if (isDuly( ((EditText) view).getText().toString())) {
this.setKATimer(((EditText) view).getText().toString());
return valid = true;
}
}
Log.w(TAG, "#checkMQTTConfigs(): " + viewName + " is invalid or missing");
Toast.makeText(getActivity(), viewName + " is invalid or missing", Toast.LENGTH_SHORT).show();
return valid;
}
isDuly:
private boolean isDuly(String text) {
// TODO Auto-generated method stub
if (text.trim().equals("")) {
return false;
} else {
return true;
}
}
You are not using break; after every case which causes the cases below to execute even if you do not want them to. For eg, this is correct :-
switch(int){
case 1:
break;
case 2:
break;
}
and this will cause unexpected output though it is not wrong:-
switch(int){
case 1:
case 2:
}
Omitting break will cause execution of case 2 after executing case 1.
You seem to be missing several break statements.
Switch(X){
case 1: doOne();
case 2: doTwo();
case 3: doThree();
}
the waterfall flow makes sure that if X is 2, both doTwo and doThree will be executed. If X is 1, all three methods will be executed.
If you want only the linked method to be called, change the code into:
Switch(X){
case 1: doOne(); break;
case 2: doTwo(); break;
case 3: doThree(); break;
}

Categories

Resources