switch case inside other switch case - android

i have a expandable listview. when selecting an item from this list, childd and parentt variables change. it works fine. but after that, the code below is written, but only last cases work :
switch (groupp)
{
case 1:
{
switch (childd)
{
case 1:
{SelectedGenre.setText("1") ;}
case 2:
{SelectedGenre.setText("2") ;}
case 3:
{SelectedGenre.setText("3") ;}
case 4:
{SelectedGenre.setText("4") ; }
case 5:
{SelectedGenre.setText("5") ;}
}
}
case 2:
{
switch (childd)
{
case 1:
{SelectedMozoo.setText("a") ;}
case 2:
{SelectedMozoo.setText("b") ;}
case 3:
{SelectedMozoo.setText("c") ;}
case 4:
{SelectedMozoo.setText("d") ; }
case 5:
{SelectedMozoo.setText("e") ; }
case 6:
{SelectedMozoo.setText("f") ; }
}
}
}
after running this code, SelectedGenre sets to : 5 and selected mozoo sets to : f. but groupp is not 1 and childd5

use break statement after each case complete.
because switch case says that if any of case is matched rest all will be executed if there is no termination. so use break statement after each case;

You don't have break in your switch case.
Try this:
switch (groupp)
{
case 1:
{
switch (childd)
{
case 1:
{SelectedGenre.setText("1") ;break;}
case 2:
{SelectedGenre.setText("2") ;break;}
case 3:
{SelectedGenre.setText("3") ;break;}
case 4:
{SelectedGenre.setText("4") ; break;}
case 5:
{SelectedGenre.setText("5") ;break;}
}
}
break;
case 2:
{
switch (childd)
{
case 1:
{SelectedMozoo.setText("a") ;break;}
case 2:
{SelectedMozoo.setText("b") ;break;}
case 3:
{SelectedMozoo.setText("c") ;break;}
case 4:
{SelectedMozoo.setText("d") ; break;}
case 5:
{SelectedMozoo.setText("e") ; break;}
case 6:
{SelectedMozoo.setText("f") ; break;}
}break;
}
}

Related

Hide ImageView hides multiple images

I'm trying to hide a specific die after it has been selected and used.
However, it keeps hiding all the ImageView below the one select
I've tried using different containers but can't seem to figure it out
This is XML for the dice for 3 of the dice:
<ImageView
android:id="#+id/img4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="Select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline3"
app:srcCompat="?attr/colorControlHighlight" />
<ImageView
android:id="#+id/img3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="Select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline2"
app:srcCompat="?attr/colorControlHighlight" />
<ImageView
android:id="#+id/img1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="Select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="?attr/colorControlHighlight" />
<androidx.constraintlayout.widget.Guideline
This is the method used to hide the dice:
public void RemoveDice(int pos) {
switch (pos) {
case 0:
img1.setVisibility(View.INVISIBLE);
case 1:
img2.setVisibility(View.GONE);
case 2:
img3.setVisibility(View.GONE);
case 3:
img4.setVisibility(View.INVISIBLE);
case 4:
img5.setVisibility(View.GONE);
case 5:
img6.setVisibility(View.GONE);
case 6:
img7.setVisibility(View.GONE);
case 7:
img8.setVisibility(View.INVISIBLE);
case 8:
img9.setVisibility(View.GONE);
}
}
This shows before selecting Blue 6
https://prnt.sc/peufqr
This after the selected die has been removed
https://prnt.sc/peugn7
I think my problem lies somewhere in the XML file but I am unsure what containers to use to prevent this.
You are missing break between each case. When using a switch, you have to add a break. Otherwise, it will execute any code until it reaches the end of the switch statement.
public void RemoveDice(int pos) {
switch (pos) {
case 0:
img1.setVisibility(View.INVISIBLE);
break; // Add this
case 1:
img2.setVisibility(View.GONE);
break; // Add this
case 2:
img3.setVisibility(View.GONE);
break; // Add this
case 3:
img4.setVisibility(View.INVISIBLE);
break; // Add this
case 4:
img5.setVisibility(View.GONE);
break; // Add this
case 5:
img6.setVisibility(View.GONE);
break; // Add this
case 6:
img7.setVisibility(View.GONE);
break; // Add this
case 7:
img8.setVisibility(View.INVISIBLE);
break; // Add this
case 8:
img9.setVisibility(View.GONE);
break; // Add this
}
}
You are not breaking switch case. use break statement
public void RemoveDice(int pos)
{
switch (pos)
{
case 0:
img1.setVisibility(View.INVISIBLE);
break;
case 1:
img2.setVisibility(View.GONE);
break;
case 2:
img3.setVisibility(View.GONE);
break;
case 3:
img4.setVisibility(View.INVISIBLE);
break;
case 4:
img5.setVisibility(View.GONE);
break;
case 5:
img6.setVisibility(View.GONE);
break;
case 6:
img7.setVisibility(View.GONE);
break;
case 7:
img8.setVisibility(View.INVISIBLE);
break;
case 8:
img9.setVisibility(View.GONE);
break;
}
}
Add breaks after every statement.
public void RemoveDice(int pos) {
switch (pos) {
case 0:
img1.setVisibility(View.INVISIBLE);
break; // Add this
case 1:
img2.setVisibility(View.GONE);
break; // Add this
case 2:
img3.setVisibility(View.GONE);
break; // Add this
case 3:
img4.setVisibility(View.INVISIBLE);
break; // Add this
case 4:
img5.setVisibility(View.GONE);
break; // Add this
case 5:
img6.setVisibility(View.GONE);
break; // Add this
case 6:
img7.setVisibility(View.GONE);
break; // Add this
case 7:
img8.setVisibility(View.INVISIBLE);
break; // Add this
case 8:
img9.setVisibility(View.GONE);
break; // Add this
}
}

Problem with setting data in two TextView using switch case

this is my piece of code:
declaration:
int posizionePagina = 1;
switch:
posizionePagina = posizionePagina + 1;
if(posizionePagina == 3) {
posizionePagina = 1;
}
switch (posizionePagina) {
case 1:
Title.setText("Giocatori:");
Title.setText("a");
case 2:
Title.setText("Dati:");
Title.setText("b");
default:
}
It is strange because sometimes I get the data of case 1 with the title of case 2, I can't see the problem so I'll appreciate your help.
You forget to break the switch case
case 1:
..
break;
Kindly use break; for Breaking the case.

Adding touch pointers in an android app

I'd like to add more than two pointers.
Here is the current code that works:
if (id == 0)
textView1.setText(touchStatus);
else
textView2.setText(touchStatus);
How would I add a third, forth, fifth...?
switch(id) {
case 0:
textView1.setText(touchStatus);
break;
case 1:
textView2.setText(touchStatus);
break;
case 2:
textView3.setText(touchStatus);
break;
case 3:
textView4.setText(touchStatus);
break;
...
}

Android - Unexpected layout

i haven't many experience in android development. this is my code:
public void onClick(View v) {
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
}
myAvatar is an ImageView.
when i click in one of the four imageButton , the result is always the same: Case R.id.categoriaA4.
Why??
You need to add break to each case. like
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
break;
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
break;
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
break;
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
break;
}

How to move to next case in switch condition

I am in need of a code, using which in my Android App, I want to go through all the cases of switch condition , (Not randomly!!!).I am able to move it randomly as follows.
public void switchingLogic(){
Random rand = new random();
int i = rand.nextInt(4)+1;
switch (i) {
case 1:
setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
break;
case 2:
setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
break;
case 3:
setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
break;
case 4:
setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
break;
}
But now I want to just go in order from 1 through 4...
If the condition(Selected image) is correct in first case, I am showing Image in fullscreen and calling back the method again.
I just came across CURSOR .
Can I use it(if yes , How?) or is there any way I can solve this.
Important: I Want to use this "Switching Logic method" in onCreate()
If you dont set the break command in a switch-case, switch wont terminate after executing one case and move to the next one. so this code:
int i = 2;
switch(i) {
case 1:
// do something
case 2:
// do something
case 3:
// do something
}
will execute case 2 and case 3. Maybe you can implement an ifclause to define if the cases should break or not.
You can try creating 2 methods to do this:
a method to loop through all cases by using a for loop
a method that contains your cases
Then just call
LoopThroughAllCases();
The code would look something like this.
void LoopThroughAllCases()
{
int minCaseValue = 0; // modify to suit your case
int maxCaseValue = 4; // modify to suit your case
for (int i = minCaseValue ; i <= maxCaseValue ; i++)
{
Cases(i);
}
}
void Cases(int i)
{
switch (i)
{
case 0:
// do something
break;
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
case 4:
// do something
break;
default:
//handle unknown value
break;
}
}

Categories

Resources