Toast didnt work - android

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.

Related

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.

Many switch cases but 1 activity

First of all there are few buttons.
I am set a switch case for all buttons in onClick method.
I want to do different task for all buttons like e.g
For add button I want to display questions of Addition , same for subtraction etc.
But I want to start same activity on the click of the buttons, I mean I have a counter class which should start before the actual questions starts getting displayed.
So on the click on any of the buttons I want to start that counter activity an after that display the questions.
#Override
public void onClick(View v) {
Intent Counter = new Intent(this, TYS_Counter.class);
startActivity(Counter);
switch (v.getId()) {
case R.id.BAddition:
break;
case R.id.BSubtraction:
break;
case R.id.BMultiplication:
break;
case R.id.BDivision:
break;
case R.id.BAll:
break;
}
}
I have done this , is this correct or not, I dont think so, so please guide me.
Edit:
I want when the user clicks on any button the the timer of 3 2 1 starts and when it finishes then the activity with questions starts.
Thanks
From what I think you are saying, you want to display the question after the timer expires. You have several options. One is that you can use
Intent Counter = new Intent(this, TYS_Counter.class);
startActivityForResult(Counter); // notice the change here
Then when your timer Activity finishes, it can use setResult() and return a value to onActivityResult() you can then start the Activity with the questions you want depending on what is sent back from the timer Activity.
If you want to start a new Activity when the counter expires then you could change your code like this
#Override
public void onClick(View v) {
Intent Counter = new Intent(this, TYS_Counter.class);
switch (v.getId()) {
case R.id.BAddition:
counter.putExtra("key", "addition");
break;
case R.id.BSubtraction:
counter.putExtra("key", "subtraction");
break;
case R.id.BMultiplication:
counter.putExtra("key", "multiplication");
break;
case R.id.BDivision:
counter.putExtra("key", "division");
break;
case R.id.BAll:
counter.putExtra("key", "whateverThisIs");
break;
}
startActivity(Counter);
}
Then when your counter Activity finishes you can use the extra sent to tell it which Activity to start

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

Or statement in switch/case ?

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
}

So, why is my switch case affecting all the following cases?

So, I'm trying to set up a series of checkboxes that, when clicked, will add a number, depending on the checkbox to either the int variable attackTotal or damageTotal, which will in turn be shown in some textviews.
Currently, however, clicking the top checkbox acts as if I had clicked both it, and every checkbox after it in the switch statement. The second clickbox seems to activate itself and all the following ones as well, etcetera etcetera...
So here's the code I've gotten together.
public void onCheckboxClicked(View v) {
// Is the view now checked?
boolean checked = ((CheckBox) v).isChecked();
// Check which checkbox was clicked
switch(v.getId()) {
case R.id.checkBox1:
if (checked)
{
flankAttack=2;
}
else
{
flankAttack=0;
}
case R.id.checkBox2:
if (checked)
{
pbs=1;
}
else
{
pbs=0;
}
all the way down to..
case R.id.checkBox10:
if (checked)
{
attackTotal=attack+flankAttack+pbs;
damageTotal=damage+pbs;
TextView textView = (TextView) findViewById(R.id.textView2);
TextView textView2 = (TextView) findViewById(R.id.textView4);
textView.setText(Integer.toString(attackTotal));
textView2.setText(Integer.toString(damageTotal));
}
else
{
attackTotal=attack+flankAttack+pbs;
damageTotal=damage+pbs;
TextView textView = (TextView) findViewById(R.id.textView2);
TextView textView2 = (TextView) findViewById(R.id.textView4);
textView.setText(Integer.toString(attackTotal));
textView2.setText(Integer.toString(damageTotal));
}
I only started trying to figure out this programming stuff on Friday, so be gentle.
just before case R.id.checkBox2: you need to have a break; to tell the program to break from the switch statement. Otherwise anything meeting R.id.checkBox1 just continue through and executes all the logic you have for R.id.checkBox2 as well. (You need break; before all your other cases as well).
You forgot adding
break;
in the end of every case.
You forgot the break statement after each case. It is falling through to the next case each time.
Also, I strongly recommend not to put more than a few lines of code in a case statement, else it gets ugly very fast and hard to work with. Instead, pull out each case into its own method. Usually it will be easy to come up with a good name for each that is self-documenting:
switch (foo) {
case 0:
do();
lots();
of();
things();
break;
case 1:
do();
other();
things();
break;
case 2:
if (ugly)
{
this_gets();
messy();
quickly();
}
else
{
we_could();
do_better();
}
break;
}
Becomes:
void do_case_0() {
do();
lots();
of();
things();
}
void do_case_1() {
do();
other();
things();
}
void do_case_2() {
if (ugly)
{
this_gets();
messy();
quickly();
}
else
{
we_could();
do_better();
}
}
// ...
switch (foo) {
case 0: do_case_0(); break;
case 1: do_case_1(); break;
case 2: do_case_2(); break;
}

Categories

Resources