Accessing program data from within onClick() inner class - java

i'm trying to access an EditText from an onClick() method within an onClickListener implementation for a button. here's the code:
transmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//data validation
///////////////////////
boolean valid = true;
if(((EditText)findViewById(R.id.drv_in)).getText().toString() == "") {
TX_FAIL_TEXT = "Missing Driver ID!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
Log.e("smsDRVERR",((EditText)findViewById(R.id.drv_in)).getText().toString());
//begin transmission
///////////////////////
if(valid) {
showDialog(DIALOG_TX_PROGRESS);
Thread t = new Thread(txRunnable);
t.start();
} else {
//do things if needed
}
}
});
the Log.e is never called (does not show up in logcat). and the program executes as if the conditional statement doesn't exist. how do i reference layout items properly in an onClick implementation? i've also tried the following line:
Log.e("smsDRVERR",((EditText)smsActivity.this.findViewById(R.id.drv_in)).getText().toString());
which was alluded to in this question i found on SO:
Inside OnClickListener I cannot access a lot of things - how to approach?
but it does not solve the problem. i'm missing something that i need to reference these items within this inner class, or maybe i have my syntax a bit jarbled. any help is appreciated.

You are not comparing your String correctly.
if(((EditText)findViewById(R.id.drv_in)).getText().toString() == "") {
is checking if the objects are equal, whereas you want to check if the values are equal. You should be using:
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
Personally, I'd assign the value returned by getText to a String variable, rather than calling getText multiple times:
String myEditTextValue = (EditText)findViewById(R.id.drv_in)).getText().toString();
...
if ("".equals(myEditTextValue)) {

First, is your onClick method being called? Second, don't use == to do string comparisons; use .equals(). (Strings that are equal are not necessarily the same object, which is what == tests.)

Related

Trying to delete from the grid under certain condition

private void delete() {
if(customer.getStatus() == status.toString("Not Contacted")) {
service.delete(customer);
myUI.updateList();
setVisible(false);
}else {
Notification.show("you cannot delete this");
}
The error I get says "The method toString() in the type object is not applicable for the arguments (string)"
What I want to do is to let the delete function work only if the status is NotContacted.
What you want is
if(customer.getStatus() == status.toString().equals("Not Contacted")) {...}
Though you should want to give status a proper type to make a type-safe comparison. I suggest looking up on Enums (https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) so that your code becomes something like
if(customer.getStatus() == Status.NOT_CONTACTED)) {...}

Set text color using if and else statement

SO i have this application that uses retrofit client to send a request to the ROS server and now my problem is that I am setting up my Status that if the status is "True" it will set to textcolor as GREEN and else as RED but when i execute it to my application it seems that its only executing the else statement it displays all red even if the status is true. Can anyone help me with this Im just a beginner.
HardwareStatus is a TextView, so it's always not equal to false. Maybe you want to check it text? In that case you should use TextView.getText() method
if (HardwareStatus.getText().equals("false")) {
HardwareStatus.setTextColor(Color.RED);
} else {
HardwareStatus.setTextColor(Color.GREEN);
}
And the first letter of variable should be lower case HardwareStatus => hardwareStatus
Duy Khan Ngyuen answer is right, I just suggest you to use equalsIgnoreCase.
if(HardwareStatus.getText().toString().equalsIgnoreCase("false")){
HardwareStatus.setTextColor(Color.RED);
}else{
HardwareStatus.setTextColor(Color.GREEN);
}
It seems like you haven't get the text of the SoftwareStatus , you need to get the text first convert it to string and then compare it if it is true .
if(SoftwareStatus.getText().toString().equals("true"))
{
SoftwareStatus.setTextColor(Color.GREEN);
}
else
{
SoftwareStatus.setTextColor(Color.RED);
}
It is because .equals() is usually used for comparing strings/characters/text
edited.
after getting your response update your Boolean or string value in app class create setter and getter for that then simply on before add check on your textView `
Class ApplicationClass extends Application(){
String statusValue="false";
Boolean statusBoolean=false;
public void updateStatusValue(String value){
this.statusValue=vale;
}
public String getStatusValue(){
return statusValue;
}
}
after calling API and getting response updateStatusValue
ApplicationClass.updateStatusValue(response.body().getHardwareStatus())
if(ApplicationClass.getStatusBoolean() ||Application.getStatusValue().equalsIgnoreCase("true")) {
SoftwareStatusTv.setTextColor(Color.GREEN);
}else{
HardwareStatusTv.setTextColor(Color.RED);
}
now you can add check for whole application using this simple getter setter

putting check to check some predetermined values and excute the buisness logic

I have an object ar which contains mainy method so one of the method is getrookic() like as shown below..
ar.getrookic()
now its return type is String
now it return values like 23, 34 like these....
now i have to put an condition where in advance i know that i have to do some logic
when this methods will return the value 66,77,64
so what shall i put in if check...
if (!ar.getrookic().equals(66) ||!ar.getrookic().equals(77) || !ar.getrookic().equals(64))
{
//do some logic
}
please advise it is correct approach..!
First, you should not be performing not on your String equality testing. If ar.getrookic() returns a String then you need to test String equality. If your function actually performs work then you should save the first reference -
String str = ar.getrookic();
if (str.equals("66") || str.equals("77") || str.equals("64"))
{
//do some logic
}

If statement with booleans android

I have been having a problem with a Boolean in an if statement all day and it is really starting to irritate me now!! I have looked at other Android threads on here and the solutions just don't seem t work.
My code started off like this:
public class MainActivity extends Activity
{
public static boolean isSignedIn = false;
public final static String USERNAME_MESSAGE = "com.example.libnoise.MESSAGE";
Button btnSignIn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = (Button) findViewById(R.id.btnSignIn);
Intent intent = getIntent();
String message = intent.getStringExtra(PlayZone.USERNAME_MESSAGE);
if(isSignedIn == false))
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
}
Then I had a thought that made it's not like other languages and I only need one "=" sign so I had it as this:
if(isSignedIn = false)
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
That didn't work and that's when I started looking online, after finding a previous thread on here changed it to the following:
if("false".equals(isSignedIn))
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
Now that doesn't look right to me in the first place but hoped it would work and it didn't.
As this is the MainActivity it loads first however since I added all this, the app crashes before it will even load when I take out the if statement it work as expected.
Any ideas?
This
if (isSignedIn == false)
is perfectly correct. (You could also write if (!isSignedIn), but that's just a matter of style.)
Note that, since you never change the value of isSignedIn (at least not in the code you have shown us), it will always be false.
i think you can simply use
if(!isSignedIn)
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
the way you followed is also correct i didn't find any mistake in except you are using extra bracket in condition if(isSignedIn == false))
If statements with boolean are same how you do it in Java, == is the right way to compare
The problem in your code is extra bracket
if (isSignedIn == false))
Just to deviate from the question, but point out what is possibly your problem, your null pointer could be because you are accessing a UI object that may well not be ready to have it's text set yet.
While some API versions cope fine with what you're doing, I've found many device/API combos simply aren't ready to have anything changed from what's in the xml until onStart. The general guidance is to load data in onCreate, but not start doing anything until onStart.

How to disabled a combobox according to the selection in another combobox?

I have this block of code that is giving me results for a combo box, I would like it to ignore the combo box and disable it when the value "SDO/OD" is selected in the one above under the combo box for ROLE aka fcbRole. The following enables the box from the first part, but the second part does not fire off. And it gives me a warning: "This field is required"...Have you seen something like this before?
I have been tinkering with:
fcbRole.addSelectionChangedListener(new SelectionChangedListener<ModelData>()
{
#Override
public void selectionChanged(SelectionChangedEvent<ModelData> se)
{
if ("SDO/OD".equals(this.toString()))
{
fcbOfficeRegion.enable();
} else
{
fcbOfficeRegion.disable();
}
}
});
Don't use == and != to compare Strings, instead use:
if("SDO/OD".equals(this.getStringName()) // or make sure you override toString()
// enable
else
// disable
For String value equality, use equals() method and not operators. Operators does reference equality check.
So, change your code to:
if ("SDO/OD".equals(this.toString()))
{
fcbOfficeRegion.enable();
} else
{
fcbOfficeRegion.disable();
}

Categories