I'm trying to create a simple Android app in Java with the following code:
public class MainActivity extends Activity {
//Declare variables
boolean first = true;
boolean secondorbefore = true;
Button ClickMe = (Button) findViewById(R.id.clicker);
ClickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Check to see if this is the first click
if (first = true) {
first = false;
// DO STUFF FOR FIRST CLICK
} else if ((secondorbefore = true) {
//so this is the second click?
secondorbefore = false;
// DO STUFF FOR SECOND CLICK
} else {
//OK it's the third click or later
// DO STUFF FOR THIRD OR LATER CLICKS
}
}
});
}
However, it only seems to run through the if condition once. It carries out the code in the first instance, including setting the secondorbefore variable to false, but subsequent clicks seem to do nothing. The code OnClickListener is being executed in subsequent clicks but it's just not running through the conditional statement.
New to Java, so I'm probably making a very obvious mistake.
Many thanks in advance.
Hint:
= is an assignment operator.
== is an equality operator.
What happens when you use assignment operator in an if statement ??
What happens when you use equalityoperator in an if statement ??
in ifconditions you always have to use the == operator for comparison and not the single =. So it would be:
if (first == true) { // this is comparison
first = false; // this is assignment
// DO STUFF FOR FIRST CLICK
} else if ((secondorbefore == true) {
//so this is the second click?
secondorbefore = false;
// DO STUFF FOR SECOND CLICK
} else {
//OK it's the third click or later
// DO STUFF FOR THIRD OR LATER CLICKS
}
if you use = instead of == you are assigning a value instead of comparing. And the return of the assingment is the value being assigned. So in that case it will be true So:
if(bool = true){...}
and
if(true){...}
are equivalent in the comparison. The difference is that bool will carry the new value from this statement on.
change this if (first = true) to { if (first == true) {
Here = is an assignment operator .But == is equality operator.
When checking if a variable is equal to something else always use ==
ie
public void onClick(View v) {
//Check to see if this is the first click
if (first == true) {
first = false;
// DO STUFF FOR FIRST CLICK
} else if ((secondorbefore == true) {
//so this is the second click?
secondorbefore = false;
// DO STUFF FOR SECOND CLICK
} else {
//OK it's the third click or later
// DO STUFF FOR THIRD OR LATER CLICKS
}
}
Related
I've a native function called get, I'm checking the returned value in while loop by this function. If this returned value is 1, I continue to get different returned value. However, as you predict it cause infinite loop in this way. So that, I also want to check the stop button is pressed to break infinite while loop. How can I do that?
My Native Function
int rValue = get(); // I get returned value here from native get() function
while(rValue == 1)
{
rValue = get(); // I'm trying to get different returned value here
if(stopButton.getModel().isPressed() == true) // I tried something like this but didn't work
break;
}
if(rValue == -1)
{
// do something
}
You have to use Action Listener to achieve this
Something like this,
stopButton.addActionListener( new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Stop Button Clicked");
}
});
I have overridden the canFinish() method of IWizard. The API can be found here
Here is my code:
#Override
public boolean canFinish(){
if(templatePage.isPageComplete()
&& jenkinsPage.isPageComplete()
&& containerPage.isPageComplete()
&& dataSourcePage.isPageComplete()
&& queuePage.isPageComplete()
&& deploymentPage.isPageComplete()){
return true;
}
else if(model.isDeployOnly()){
return true;
}
return false;
}
There are other methods like addPages() that are also executed.
Really, I want to know when canFinish() is executed? And is there a list of the order in which they are executed?
canFinish is called by the WizardDialog whenever it needs to update the buttons on the button bar (the Back, Next and Finish button). There will be calls when the wizard is first shown and when you move between pages. Individual wizards can also call IWizardContainer.updateButtons whenever they want the button status updated.
Note: the default code for canFinish is:
public boolean canFinish() {
// Default implementation is to check if all pages are complete.
for (int i = 0; i < pages.size(); i++) {
if (!pages.get(i).isPageComplete()) {
return false;
}
}
return true;
}
so if you just want to call isPageComplete on all your pages just call super.canFinish
As I've tried to structure below, I want an if statement that performs in a for loop to do one thing the first time it is processed, and something else every other time.
if (true) {
for (value i : valueList) {
if (i = true) {
First time do this!
Second time onwards do this!
}
}
}
Is there a function in java that allows me to do this?
I've always used a flag, which gets unset in the first block to turn it off:
boolean first = true;
for ( value i : valueList ) {
if ( i ) {
if (first) {
// First time do this!
first = false;
} else {
// Second time onwards do this!
}
}
}
Note I removed the buggy assignment if (i = true) which is always true, and assigns true to i (clobbering your loop variable's value).
Also, the outer if (true) is unnecessary.
I have a spinner where the user can select something. When they do, it calls switchCat() which then calls chooseView() and based on the int passed will do something different. In this case it loads a different set of array strings.
However, whenever I run it, I will ONLY see the strings from R.array.array0, so the first if in chooseView(). csArray is set nowhere else. I am calling chooseView() onCreate.
I am a novice. Thank you!
Spinner item selected
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (synthetic) {
synthetic = false;
return true;
}
switchCat(itemPosition);
return true;
}
switchCat();
public void switchCat(int i) {
if(i == 0) {
chooseView(0);
}
else if(i == 1) {
chooseView(1);
}
}
chooseView()
public void chooseView(int i) {
if(i == 0) {
CharSequence[] csArray = getResources().getTextArray(R.array.array0);
for (CharSequence s : csArray) {
mArrayList.add(s);
}
}
if(i == 1) {
CharSequence[] csArray = getResources().getTextArray(R.array.array1);
for (CharSequence s : csArray) {
mArrayList.add(s);
}
}
}
It looks like you are adding items to your ArrayList. Are you sure you don't mean to get rid of old ones as well prior to adding? If you don't, they will remain in the list when chooseView() is called. In chooseView(), simply call mArrayList.clear() before your if statements. This, of course, assumes that your ListView is dependent on mArrayList.
Whenever you run first time it will give correct output,another time you arraylist will add the values with previous one,so every time you want's to clear the arraylist for get a correct output.
public void switchCat(int i) {
myArrayList.clear();
if(i == 0) {
chooseView(0);
}
else if(i == 1) {
chooseView(1);
}
}
I have a function when has an if-else statement. It essentially looks like this:
if(boolean == true)
{
// do something
boolean = false;
}
else if(boolean == false)
{
// do the other thing
boolean = true;
}
Now, my understanding is that the if statement will exit and return control to the function and then continue according to the changed boolean value. But I'm clearly missing something because my code is not exiting the original 'if'/'else if' statement (whichever the original case). Can anyone tell me what I've missed?
Well as requested, additional data about the code is that it is a part of my android project and each condition in the if-else block has a nested function and the boolean(global) value is being set/unset withing these functions. So the code now looks like this:
dummyFunction(){
boolean = checkIfTrueOrFalse();
if (boolean) {
onClick( public void onClick(){
// do something
boolean = false;}
} else if(boolean == false){
onClick( public void onClick(){
// do something
boolean = true;}
}
}
Any ideas?
if(boolean == true)
{
// do something
boolean = false;
}
if (boolean == false)
{
// do the other thing
boolean = true;
}
When you do this, then the program will flow to the second condition. In an if/else if statement, if the if statement has been satisfied, then the program will ignore the else if block.
Your current code simply flows through the first if block and then skips the else if statement to end the block.
void someMethod()
{
boolean aBoolean = true;
if(aBoolean == true)
{
// do something
aBoolean = false;
}
else if(aBoolean == false)
{
// do the other thing
aBoolean = true;
}
}
When someMethod will execute, since aBoolean is assigned with true, control will come to if block cause the condition becomes true. if it was false, then the control will come to else part.
We have many good answers/comments already but just wanted to add something here -
1.
if (condition) {
} else {
}
is a single code construct. The condition will be evaluated at the beginning at run time and java will decide which block to execute i.e. the if block or the else block. Only 1 of the 2 can be executed.
Java allows us to nest if/else. That means we can have something like below -
if(condition1){
} else if (condition2) {
} else if (condition3) {
} else {
}
It is effectively same as below -
if (condition1) {
} else {
if (condition2) {
} else {
if (condition 3) {
} else {
}
}
}
Here, it should be noted that only the block which satisfies the condition will be executed. If none of the conditions is met, then the inner most else will be executed (i.e. the else block of condition3 )
Finally, I feel that your confusion is between the below blocks
boolean aBoolean = true;
if(aBoolean == true)
{
// do something
aBoolean = false;
} else if(aBoolean == false)
{
// do the other thing
aBoolean = true;
}
VS
boolean aBoolean = true;
if(aBoolean == true)
{
// do something
aBoolean = false;
}
if(aBoolean == false)
{
// do the other thing
aBoolean = true;
}
In the latter of the 2 examples, there are 2 independent if blocks and both will get executed (off course, this is not logically correct but it is a legal java code.)
Could you provide more info regarding your code not exiting either of the 2 blocks? Doing System.out.println() of variables within your blocks might be able to help you determine why your code is not exiting.
You could use an if/else pair instead of an if/else-if as the parameter that your code depends on is would be either true/false. If the if-block is not satisfied, automatically the else-block would be traversed.
Your code is actually a shortcut for
if (boolean) {
// do something
boolean = false;
} else {
if (!boolean) {
// do the other thing
boolean = true;
}
}
Written this way, it maybe becomes clearer that the inner if nested in the else case will not be processed if the first if condition was already met.
Well I've solved it (taking inputs from here of course). I just added a call to the function within the nested functions and it worked. Now the code looks like this:
public static void dummyFunction(){
boolean = checkIfTrueOrFalse();
if (boolean) {
onClick( public void onClick(){
// do something
dummyFunction();
boolean = false;}
} else if(boolean == false){
onClick( public void onClick(){
// do something
dummyFunction();
boolean = true;}
}
}