I want to determine which value has been sent by different buttons that I have in my code. I have tried the code below.
Thanks in advance!
if (intent.getExtras().getString("") == button1value){
}
You can't compare two strings with ==.
if (intent.getExtras().getString("").equals(button1value)) {
}
Related
I have a dynamic arraylist that contains String values. I want to match one of its values with AutocompleteText whatever the user has typed/entered. In case the value is not matching with any of the Arraylist values I would like to clear Autocomplete Text.
Couple of Ways I have tried.
Match the typed or user selected value from autocomplete text with list directly without any loop using "contains" method. Didn't achieve the expected result.
Store the values in String[] array from list and loop through it and match it with user input/selection from Autocompletetext. Didn't achieve the result this way too.
Please provide any ideas on how to clear text from autocompletetextview if value is not found?
You did not specify any language that's why i will answer in kotlin
Override onKeyDown method and let's say you will check when enter pressed check if given keycode value equals to KeyEvent.KEYCODE_ENTER
In if block get your text and use an algorithm like below
val arr = intArrayOf(1,2,3) if (textFromTextView !in arr) it means your typed text not in your array and if this condition is true
use autoCompleteTextView.replaceText("") and i hope it will work for you.I never tried but i searched for you.
I am trying to filter an object type list based on a string value which is working fine, but now I need to filter based on an integer attribute of the objects on the list.
Say the object.name is "name", so when I start typing just "na" the filters works properly, but for the integer filter I cannot figure it out. It just filters when the user inputs the entire number.
return objectList.filter { it.name.contains(filter) || it.pCode == filter.toInt() }
I know that the way is comparing "==" only returns when is equal, but I do not know something like contains but for numbers.
Try this
it.pCode.toString().contains(filter)
In my onCreate() I have:
if(sharedPref.getString("name",null)==""){EditText.setText("Something");}
else{EditText.setText(sharedPref.getString("name",null));}
And then in my onStop() I have:
sharedPrefEditor.putString("name",EditText.getText().toString());
The EditText shows only the hint when I first install and run it. It does seems to display the correct text when it's started later, however.
Don't use == to compare content of Strings. Use equals() instead :
if(sharedPref.getString("name",null).equals("")){
EditText.setText("Something");
}
Why should I use equals instead of ==
First of all you are not using correctly the shared preferences. You are typing null as second parameter which is the value you will get if the key you typed as first parameter is not defined. This is not a problem itself but then in your if sentence you are comparing it to an empty string.
So first of all use equals instead of ==and then if you want to receive an empty string if the key is not defined type an empty string as second parameter of SharedPreferences.gerString method.
Hope it helps ;)
If "Something" is to be used if preference not set, then:
EditText.setText(sharedPref.getString("name","Something"));
getString() will automatically return "Something" if preference is not set. No need of extra code.
String strName = sharedPref.getString("name","");
if(!strName.isEmpty()){
EditText.setText(strName);
}else{
EditText.setText("Something");
}
The operator == can not be used to compare strings in Java.
if(string1.equals(string2){
}
Also, you set a value to SharedPreferences, but you never commit the changes.
sharedPrefEditor.commit();
OnCreate
String prefsVal = sharedPref.getString("name", null);
if(prefsVal.equals(null)){ //If default value was returned
EditText.setText("Something");
}else{
EditText.setText(prefsVal);
}
OR
The following will turn your posted code into a single line solution. The second parameter passed to SharedPref is the default value to be returned if no vale was found for the given key.
EditText.setText(sharedPref.getString("name", "Something");
OnStop
sharedPrefEditor.putString("name", EditText.getText().toString());
sharedPrefEditoy.commit();
I have two spinners in my app. I want that if i select option "first" from spinner 1 and option "second" from spinner 2, then the action gets performed. But it show "NUMBERFORMATEXCEPTION".
Here's the code
if (((spinner.getItemAtPosition(pos).toString()=="first" &&
(s2.getItemAtPosition(id).toString()=="second"))))
{
tv.setText(String.valueOf(gmtomilli(x)));
}
This code has the error, if i omit this code, then the app works fine,without action
Do String comparison using equals.
spinner.getItemAtPosition(pos).toString()=="first"
instead use:
spinner.getItemAtPosition(pos).toString().equals("first")
Similarly for:
s2.getItemAtPosition(id).toString()=="second"
instead use:
s2.getItemAtPosition(id).toString().equals("second")
Read this for more information.
== compares references,not the values. In your case, you want to check for the value equality, not the reference equality.
EDIT:
Since you have mentioned that your code is generating NumberFormatException, I probably believe that either pos or id are of String type generating the NumberFormatException.
EDIT 2:
As per the your comment:
float x=Float.parseFloat(String.valueOf(et.getText()));
Your getText() is returning a String that can't be actually parsed into a float. Try checking if the content is actually a float in String format.
Besides, use String.trim() before parsing to ensure your String doesn't contain any leading or trailing whitespaces that's generating the NumberFormatException.
I want to compare 2 strings.
My first value is in 'list[0][0]' variable and the second value is in item[0].
But when I am comparing the 2 strings using 'if' statement, I don't get the answer.
if(selected_list[0][0]==items[0])
{
// some code
}
it is not working.
But when I am hard-coded these values, it is working fine.
if("banana"=="banana")
{
// some code
}
Please give me the solution?
Thank you..
Here is an explanation of how strings should be compared and the different options for doing so. They aren't as simple as comparing int's.
if (string1.equals(string2))
You have to compare it as [list[0][0] isEqualToString:items[0]] otherwise you are comparing their addresses not the values.
Use the compareTo() or equals() method of one of your strings, passing the other string as argument.
string1.equals(string2)
// returns true if both strings are equal
string1.compareTo(string2)
// returns 0 if both strings are equal