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;
}
}
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'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;
...
}
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;
}
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
}
I have android project v.2.1.
and I have a code, in the code I have Toast, but it doesn't work. Why?
public void click(){
TextView color = (TextView) findViewById(R.id.text);
switch(i){
case 1: table.setBackgroundColor(Color.RED); color.setText("Красный");
break;
case 2: table.setBackgroundColor(Color.rgb (255, 127, 0) );
color.setText("Оранжевый");
break;
case 3: table.setBackgroundColor(Color.YELLOW);
color.setText("Желтый");
break;
case 4: table.setBackgroundColor(Color.GREEN) ;
color.setText("Зеленый");
break;
case 5: table.setBackgroundColor(Color.rgb (0,191,255) );
color.setText("Голубой");
break;
case 6: table.setBackgroundColor(Color.BLUE );
color.setText("Синий");
break;
case 7: table.setBackgroundColor(Color.rgb (160,32,240) );
color.setText("Фиолетовый");
break;
case 8: // i=1;
Toast.makeText(getApplicationContext(), "Вы прошли все цвета",Toast.LENGTH_SHORT).show(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< MY TOAST!
break;
}
Toast not in an OnCreate.
Your statement to create and show the Toast is correct. It is likely you are having a logic error are you absolutely positive that your switch statement is using case 8? It would be a good idea to put some log statements inside each case that print out which case it is so you can debug what is happening easier.
Im not certain what the rest of your activity looks like but this appears to be some kind of click listener callback method. If that is the case you should not have
TextView color = (TextView) findViewById(R.id.text);
Inside a click callback. Doing so means that findViewById() is going to get called every time your view gets clicked. Since this is a relatively expensive method to call its more appropriate to call it once in your onCreate and just keep the reference that you get back for all other times you'll need it.