I am preparing a password activity and I want to get the proper input for it.
I am using the code
while(counter<5){
switch(counter){
case 1:pwdField();
counter++;
break;
case 2:pwdField2();
counter++;
break;
case 3:pwdField3();
counter++;
break;
case 4:pwdField4();
counter++;
break;
}
}
pwdField are the methods that contain listeners for buttons. But for some reason only the third textView receives input. It doesnt break off after the input is received. What am i doing wrong?
Moved from comments:
Why do you have the while loop? If the while loop condition is met and counter is not negative then the loop will go into a case and break out of the loop, thus only going through it once. If the condition is not met then the loop will be iterated 0 times. Why not replace the while loop with an if statement?
You may not want to have the breaks in there but I don't know the intent of this snippet.
Related
I am busy working on an app with multiple switch statement that each have a different number of cases.
switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something
I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.
How do I know when the count is at the last case? What is the code for that?
I appreciate your time.
You can use default case for your problem.
switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}
You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.
I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch
.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.
[EDIT]
You can restart the variable in the last case of your switch.
I have several objects in an activity and I would like to check their id before I perform any actions on them. However I'm not sure how to go about it, I tried :
if (v.getId().contains("empty")){
but that gives an error and then I tried
if (v.getId() == "[attr^=empty]"){
and that is also wrong. To be honest I am aware that both of these don't use proper assignments and so on but I'm really lost on how to check whether an id contains a certain substring.... I would appreciate it if someone could help
PS: v is a View object
v.getId() returns an integer. In order to check id before doing something, I'd recommend something like:
switch (v.getId()){
case R.id.ViewElement1:
//do something here
break;
case R.id.ViewElement2:
//do something here
break;
case R.id.ViewElement3:
//do something here
break;
}
What this code does is that it gets the Id of your View then compares it through a switch-case block. If the id taken from v.getId(), the code in the case block executes. The break; is important, otherwise, the code in the cases below the one executed will also run. You can also do this for an if-else if that fits your style more.
If you want to fetch the id as a string, you can try:
String ResourceIdAsString = v.getResources().getResourceName(v.getId());
and then you can do your if contains operations on the String object.
I am not entirely sure what you are trying to achieve. So there's some overview of how id works.
First you have to id all of your views in xml like:
<TextView
android:height="wrap_content"
android:weight="match_parent"
android:id="#+/myTextView" />
Then in your JAVA code use a switch case like:
switch(view.getId()){
case R.id.myTextView1:
break;
}
If you have some views that do same thing you can use them like:
switch(view.getId()){
case R.id.myTextView1:
case R.id.myTextView2:
case R.id.myTextView3:
case R.id.myTextView4:
perfomeSomeAction();
break;
}
I have some classes on my code and based on the click of the listview i want to run the selected class. What I mean is that if a user clicks on position 0 I want to run method GoToTown().
I have more than 40 methods so if I do it with if / elseif it would be really bad. I have a custom adapter for the listview so probably I could use it somehow?
Is there some way I could pass an array to do it?
Instead of using 40 different methods, use only 1 (the one which should get the listview result and call one of the 40 methods). And then you should have a switch inside it:
// "number" is the number if the item clicked in the list
switch (number) {
case 0: // First item was clicked, counting starts from 0
// Put some code here
break;
case 1: // Second item...
// Other code
break;
// Etc.
default: // You did not define a "case" for a number, default gets executed
// Code here
}
I want to make some kinda dictionary,But not exactly a dictionary,
I have two text views, One for the word, The other one for the meaning.
I have two Buttons which they change an integer's number
And finally I have lots of if, For Example:
(if i==1){
txt1.setText("x");
txt2.setText("y");
}
(if i==2){
txt1.setText("n");
txt2.setText("m");
}
Q1: Its better with switch right?
Q2: Where should I put those Switch? In each Button's ClickListener ?
Q3: Can you suggest a better way to do that?
Switch is definitely a better option.
You can have a common API that will handle this logic like:
toggleText(int i){
switch(i){
case 1:
txt1.setText("x");
txt2.setText("y");
break;
case 2:
txt1.setText("n");
txt2.setText("m");
break;
}
}
Then u can call this API from ur button cick:
button.setOnClickListener(new View.OnClickListener{
#Override
onClick(){
toggleText(pass your value here)
}
});
Let me know if u need any help.
Switch is good option than if else
Make a function and put all switch conditions in it and call that function on button click
I have a seemingly simple question that I need help with. I have a button. I want it so when I click on the button quickly it adds one to a total. I also want it to be so that when I hold down this same button for about 2 seconds, it removes one from the total. The only part I am having trouble with is the motion event part. I have been experimenting with ACTION_UP and ACTION_DOWN with no luck. Is there an easy way to do this?
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
total ++;
return true;
}
case MotionEvent.ACTION_UP:
{
if(total >0){
total--;
}
return true;
}
}
Thanks for the help!
Why not use the Button's setOnClickListener and setOnLongClickListener?
The only limitation with this approach is that you cannot set the the timeout value, which is somewhat over 1,5 seconds, if I remember correctly.
If you're persistent about the two seconds (or some other value), then I suppose you could use an onTouchListener and keep track of the pressed time yourself. In that case, the MotionEvent's getDownTime might be of interest to you.