Text Retrieval from EditText [duplicate] - android

This question already has answers here:
Get Value of a Edit Text field
(13 answers)
Closed 4 years ago.
Why does not
String.valueOf(edittext var name)
retrieve the text from EditText?

That's what Android has provided.
You can just use editText.getText().toString() instead.
Info about String.valueOf(obj)
String class is a Java class, it does not know Android classes like TextView or EditText.
String.valueOf accept EditText or any object because it will be considered to call String.valueOf(Object) method.
From String Documentation internal implementation of String.valueOf(Object) is
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
If you call String.valueOf(editText) It will return you class name like EditText#2a139a55.

Use editTextName.getText().toString() which returns String value

String value = String.valueOf(editText.getText())

Related

Cannot resolve substring() or indexOf() methods

I am new to android and i am trying to search for a certain character in an edit text and then extract all the characters before it in a new variable using the substring() and indexOf() methods , but android studio saying that it cannot solve either method . So please tell me what i am doing wrong with the code. Here is the declaration for the edit text :
EditText text;
text = (EditText) findViewById(R.id.text);
And the code for calling these methods :
if(text.getText().toString().contains("+")) {
String before = text.substring(text.indexOf("+") - 1);
}
You're calling substring() and indexOf() on your text variable, which is an EditText object, and they're string methods. Convert to string first and then use them, like so:
String textString = text.getText().toString();
if(textString.contains("+")) {
String before = textString.substring(textString.indexOf("+") - 1);
}
You need to call them in the same way you call contains(): text.getText().toString() because the two methods are part of the String class, not EditText.
You can try String before = text.split("+")[0]; This will return you everything before the first '+'

How to check whether String already exist in Arraylist? [duplicate]

This question already has answers here:
How does a ArrayList's contains() method evaluate objects?
(10 answers)
Closed 5 years ago.
I have an app in which contain ArrayList of some String let suppose a,b,c,d and i have a String called pack. What i have to do is that i have to check whether pack is already exist in arraylist or not if not exist then i have to add it in arraylist. How do i do that
code:-
ArrayList<CBlackList> appsListDataSet;
public void setAppsDetails(String pack) {
if (appsListDataSet != null && appsListDataSet.size() > 0) {
for (int i = 0; i < appsListDataSet.size(); i++) {
CAppsModel appsModel = appsListDataSet.get(i);
if (pack.equalsIgnoreCase(appsModel.getPackageName())){
//Not to know what to do here.
}
}
}
}
Use the contains method in an arrayList. If your arrayList contains only String and you want to check for String value itself, then just do
boolean isPackPresent = appsListDataSet.contains(pack)
this will work if your arraylist has the same datatype as pack and is not a custom object.
Try Some thing like this
if(appsListDataSet.contains("your String"))
{
///Action to be occur
}

Comparing strings that use the substring method [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I hope somebody could give me an explaination why the below code wont work:
//Why doesnt this work
String l = myString.substring(cut, lengthLastBtn-1);
String c = myString.substring(cut, lengthLastBtn-1);
if(l==c){
Log.i(TAG, "Correct");
}
//End
//This work!
String l = "hi";
String c = "hi";
if(l==c){
Log.i(TAG, "Correct");
}
// End
// Or if i want the Vars as in the first code i have to use the if statement like this
if(l.contains(c)){
Log.i(TAG, "Correct");
}
//End
So, why cant a compare a string when i have used the substring method on it. I even see in the log for the strings that they are the same, or have the same text at least.
When you use the “==“ operator with String`s, it means a comparison between objects, not the value that objects hold.
In order to compare Strings values , you should use the built-in method equals. The result is true if the String object represents the same sequence of characters.
if(string1.equals(string2)) {
//Match
}

calling methods details like line number, method name and class name in android [duplicate]

This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 9 years ago.
I need to display the calling methods details like line number, method name and class name.
How to get all those information in android,whenever a method is called in an application the calling method info has to be dispalyed,can anyone help me in solving this...
You can get using the following code- [copied from How to get method name for debug output in Android/Java? ]
Thread current = Thread.currentThread();
StackTraceElement[] stack = current.getStackTrace();
for(StackTraceElement element : stack)
{
if (!element.isNativeMethod()) {
String className = element.getClassName();
String fileName = element.getFileName();
int lineNumber = element.getLineNumber();
String methodName = element.getMethodName();
}
}
Once you got the line number, method name, class name you can use it as you wish.

Android if statement does not fire? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Why does my "if" statement not fire?
String something = "";
String category = json_data.getString("den");
Log.e("JSON", "category="+category);
if (category == "1"){
something = "Random something";
}
Even if in my logcat I can see JSON:"category=1", the "something" String does not take "Random something" value.
This must be some convention in java?
Please help.
use:
if (category.equals("1")){
something = "Random something";
}
or better way to avoid null pointer exception:
if ("1".equals(category)){
something = "Random something";
}
Also have a look at this link for detail : How do I compare strings in Java?
You need to do string comparison in Java using the .equals method on the String object. The comparison you are doing is only comparing the references not the actual string values.
Example:
if(category.equals("1")){
//do amazing stuff
}

Categories

Resources