my problem is this:
I have 2 text view, in the first a text that changes from "your X is" to "you are"("you are" is stored in a string) and in the latter a number that could assume any value or, if another value is "0", just become "perfect"(this took from a string).
All this after a click on a button.
The problem is that the first text changes while the second change from 0 but if the first value ( that i said before ) is 0 ( or minus 0 ) just doesn't change... It not assume the value of the string i want.
I hope you got the problem, this is the code.
if (risultato <= 0) {
risultatoX.setText("0");
X.setText(R.string.youAre);
risultatoOre.setText(R.string.perfect);
risultatoOre.setTextColor(Color.parseColor("#259b24"));
hr.setVisibility(View.GONE);
} else {
X.setText(R.string.First);
risultatoOre.setTextColor(Color.parseColor("#757575"));
hr.setVisibility(View.VISIBLE);
}
If you want to get String from resources you need use getResources().getString() method.
For example:
X.setText(getResources().getString(R.string.youAre));
risultatoOre.setText(getResources().getString(perfect));
Related
There is a table with application numbers and user data. at the beginning of the table, the application number at the end is a green arrow. There can be an infinite number of fields in a table and it always changes.
I need to find the green arrow by the application number and click on it accordingly.
tried to do something like
String myorder = "629/0000/000000021059";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td[5]\n")).getText())){
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[18]/a/img\n")).click();
and so on up to 100+
The first problem is that I really don't like the option of writing if else 100 times. And there is only one digit in the diva where the id of the application and one in the digit in the diva where the green arrow is different(img)
The second problem is that no matter how I insert break; when finding the right application, he clicks on the desired arrow, but after apparently trying to continue searching the page, but the page has already changed and the error falls
(WARNING: The server did not provide any stacktrace information)
if I use the code that was attached above or just an indication of the element (which goes right after the one that turned out to be correct and the page has changed) with a note that I can not find it, if I use else if with break;
tried to do so
for (int i = 1; i < 25; i++) {
String myorder = "629/6300/000000412067";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
But if the item is located, then it clicks on the very first arrow in the table, and not on the one in the same column as the application number
I would really appreciate your help!
XPaths are 1-based not 0, so you are correct in thinking you need to add one, however, these two are not equal as i++ modifies the value of i (even inline like you have it), which you do twice inside your loop. Therefore the 2nd iteration of your loop i will be 3, not 1 as expected. In your loop replace i++ with i + 1 and you should be good to go.
for (int i = 1; i < 25; i++) {
String myorder = "629/3500/000000329976";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
}
Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[31]/table/tbody/tr/td[5]
"}
So it was searching untill 31. And it's strange because i used for i < 25 and there are only 25 rows in the table.
but still dont work for me;(
I have a decimal number in EditText and I'm trying to change it to always show a decimal part but the user doesn't be able to change the decimal part, only the integer part has to be editable. The decimal part is always a default value.
Example: I have the number 2.025,50 at EditText, if I delete all the digits Ill have 0,50. If I write 10 , Ill have 10,50.
Can anyone help me out ??
I created a function you can use for this, so you just input your number with a decimal and it will give you the decimal part of the number. Use editText changed listener. So when u pass the value being typed by a user call this function and pass the numberWithTheFraction to the function getFractionalPart and add the userinput as shown in the code bellow.
private static double getFractionalPart(double num) {
if (num > 0) {
return num - Math.floor(num);
} else {
return ((num - Math.ceil(num)) * -1);
}
}
you can have a look at this example for an example of editText change listener.
So in the above example when you say textView.setText(getFractionalPart(numberWithTheFraction)+userInput)
I'm creating an application which updates users on the score of a football match either in real time or as a final result. At least one score must be inputted in order for the TextView to be updated and the relevant score to be displayed. I'm checking that at least 1 of a pair of EditText fields is not empty using the following code:
if(!(et_current.getText().toString().isEmpty())||(!(et_final.getText().toString().isEmpty()))
&& (!(et_current2.getText().toString().isEmpty())||(!(et_final2.getText().toString().isEmpty()))){
if(!(et_final.getText().toString().isEmpty()))
tv_final.setText(et_final.getText().toString());
else
tv_current.setText(et_current.getText().toString());
if(!(et_final2.getText().toString().isEmpty()))
tv_final2.setText(et_final2.getText().toString());
else
tv_current2.setText(et_current2.getText().toString());
}
I want to be able to set the correct TextView so I have another if statement inside the original if statement to see ensure the correct score is being updated.
When I run the code, I do not seem to be getting past the first if statement. Am I using the correct format or is there an better way to complete these checks?
Thanks!
For readabilities sake, get some variables going
boolean currentEmpty = et_current.getText().toString().isEmpty();
boolean current2Empty = et_current2.getText().toString().isEmpty();
boolean finalEmpty = et_final.getText().toString().isEmpty();
boolean final2Empty = et_final2.getText().toString().isEmpty();
And then your code can be much cleaner. Something like
if( (!currentEmpty || !finalEmpty) || (!current2Empty || !final2Empty)) {
if(finalEmpty) {
tv_current.setText(et_current.getText());
}
else {
tv_final.setText(et_final.getText());
}
if(final2Empty) {
tv_current2.setText(et_current2.getText());
}
else {
tv_final2.setText(et_final2.getText());
}
}
I'm not sure if that is completely correct as the requirement is not entirely clear to me, but it should atleast be a good start to follow what's going on.
I have a global variable that I modify in three different methods. It starts at 0, one method adds by 3, the next one by 2 and the last one by 1. They are all related to a button of their own.
When I click the "plus 1" button, the counter remains at 0 and I need another push to get it to 1. Interestingly, if I start with the other two buttons the counter acts accordingly but if I click my "plus 1" button again, it needs two pushes and acts weird like if it was holding the add...
public void addThreeForTeamB(View view) {
displayForTeamB(scoreTeamB += 3);
}
public void addTwoForTeamB(View view) {
displayForTeamB(scoreTeamB += 2);
}
public void addOneForTeamB(View view) {
displayForTeamB(scoreTeamB ++);
}
So that's the way it is managed, the cool thing is that when I change the last method to a "scoreTeamB += 1" it acts as it should, just adding without conflict.
My question is about the difference between this two operators to better understand the reason behind the slight discrepancy generated when using this 2 operators on the same variable.
scoreTeamB++ returns the previous value of the variable (before it was incremented). += returns the value that was assigned to the variable.
If you replace scoreTeamB++ with ++scoreTeamB or with scoreTeamB +=1 you'll get the new (incremented) value of the variable.
To make this code work as you expect, you should use prefix ++ operator instead of postfix one. Prefix ++ operator(as well as -- operator, apparently) returns incremented value, while postfix operator ++ returns value of variable before increment.
Those may be implemented like this:
public static Integer prefixIncrement(Integer value) {
value = value + 1;
return value;
}
public static Integer postfixIncrement(Integer value) {
Integer returnValue = new Integer(value);
value = value + 1;
return returnValue;
}
In the following if statement from a loop in my code, if the given oldsalary[i] doesn't meet these guidelines, I want to restore the previous numerical value of oldsalary[i] to "Error". However I want it to stay as oldsalary[i] since I will be displaying all the oldsalary[i] later in my code.
So basically when all the oldsalary[i] are displayed in another loop, I want to be able to see "Error" so it's know that something was wrong with that value.
I know the way I have it is completely wrong, I just put it like this to make sense. Sorry if it doesn't make any sense.
if(oldsalary[i] < 25000 || oldsalary[i] > 1000000){
JOptionPane.showMessageDialog(null, userinput[i]+"'s salary is not within
necessary limit.\n Must be between $25,000 and $1,000,000. \n If salary is
correct, empolyee is not eligible for a salary increase.");
double oldsalary[i] = "Error";
}
You can't store both the numerical value and an error indicator in a single double value.
Your best bet is to wrap the salary as an object that contains both the salary value and a boolean that indicates the error condition:
class Salary {
private double value;
private boolean error = false;
... constructor, getters and setters
}
And update your code to use the object instead. I.e.
if(oldsalary[i].getValue() < 25000 || oldsalary[i].getValue() > 1000000) {
oldsalary[i].setError(true);
...
}
So later you can do
if (oldsalary[i].isError()) {
// display error message
}
You can use an extra List that stores the indices that are no pass your requirement test.
List<Integer> invalidIndices = new ArrayList<>();
for (...){
if(oldsalary[i] < 25000 || oldsalary[i] > 1000000){
JOptionPane.showMessageDialog(null, userinput[i]+"'s salary is not within
necessary limit.\n Must be between $25,000 and $1,000,000. \n If salary is
correct, empolyee is not eligible for a salary increase.");
invalidIndices.add(i);
}
}