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;
...
}
Related
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.
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;
}
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;
}
}
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;
}
}
Is it possible to use an OR statement in a switch/case structure? Suppose I want to do the same thing when clicking on two different items.
I tried
(case R.id.bOne || case R.id.tvOne):
and
case (R.id.bOne || R.id.tvOne):
But none of them seem to work..
This will work:
case R.id.bOne:
case R.id.tvOne:
// do your stuff
Try this:
switch(id){
case R.id.bOne:
case R.id.tvOne:
// do your stuff here
break;
case R.id.x:
// do your stuff here
break;
default:
// do your stuff here
}