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
}
Related
This question already has answers here:
How to return a DocumentSnapShot as a result of a method?
(2 answers)
How to return a list from Firestore database as a result of a function in Kotlin?
(3 answers)
Closed 4 months ago.
I ran into a problem which I don't really understand. Maybe it is trivial, maybe I am just too tired and therefore I can't understand how it works at the moment.
I have a Firestore query which returns a list of object. In the onResult I try to store this result in a local variable so I can change its values later. My problem is that when I update this local variable somehow the original result changes too. I don't really understand how it is possible.
getParts(
carId = carId,
onResult = { vehicleParts ->
//store callback result in local variable
var updatedVehicleParts: List<VehiclePart> = mutableListOf()
if (vehicleParts != null) {
updatedVehicleParts = vehicleParts
}
//update local variable -> (newValues is a parameter of the function)
newValues.map { newPart ->
updatedVehicleParts.find { it.name == newPart.name }?.let {
it.currentHealth = newPart.maxLifeSpan
it.replacementTime = newPart.replacementTime
}
}
//here not only the local variable is updated BUT also the result of the callback
},
onError = {
}
)
I feel so stupid not recognizing something here...
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())
Can anyone tell me why these is not working? If these work for me it will answer all my questions, please help me:
for (int i = 0; i < records.size(); i++) {
JSONObject jsonObject = new JSONObject();
if (Scanner.listOfBarcodes.contains(records.get(i).getsid())) {
jsonObject.put("attendance","Present");
} else {
jsonObject.put("attendance","Absent");
}
listOfBarcodes is an ArrayList which contains a bunch of scanned barcode values which are student id numbers like 3924,3922...
But it is always putting Absent in the JSONObject. Why is that happening? Please help me.
This is my serializable java class:
public class Student {
private String sid;
public void setsid(String sid){
this.sid=sid;
}
public String getsid(){
return sid;
}
}
I might be able to help better with a bit more of your code to be sure of the answer, but I believe the problem is the function contains() expects to gets as a parameter an object of the same type of the ArrayList it is called from.
You need to debug it and check the that return value of records.get(i).getsid() has the same type as the ArrayList listOfBarcodes.
Maybe try to assign it to a temp value as follow and check what it returns and compare to this temp item, the problem could be in saving the values not in retrieving it (Your call could be returning null) as follows:
String tempSid = records.get(i).getsid();
if (Scanner.listOfBarcodes.contains(tempSid)) {
jsonObject.put("attendance","Present");
}
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
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I can't seem to get the code within the second if statement to execute. I have logged both values being compared and also ran the debugger and checked them. They are both "a". It always shows the incorrectPasswordDialog. This question seems difficult because it seems as though it should just work but any help is appreciated.
private void logUserIn(AppUser user) {
if (user != null){
Log.d("mPassword: ", mPassword);
Log.d("user.getPassword(): ", user.getPassword());
String userPassword = user.getPassword().toString();
String formPassword = mPassword.toString();
if ( userPassword == formPassword ){
Intent welcomePage = new Intent(this, StartScreenActivity.class);
welcomePage.putExtra("name", mName);
startActivity(welcomePage);
}
else {
showIncorrectPasswordDialog();
}
}else {
showIncorrectUserNameDialog();
}
}
You are comparing the object identity. Use string.equals() to check for equivalence.
if(userPassword.equals(formPassword)){
}
Change
if ( userPassword == formPassword ){
to
if ( userPassword.equals(formPassword) ){
== compares object references, whereas .equals compared String values.
You can not compare string using ==
try like this
Compares the specified object to this string and returns true if they are equal. The object must be an instance of string with the same characters in the same order.
if ( userPassword.equals(formPassword)){
// do some stuff
}
or
Compares the specified string to this string ignoring the case of the characters and returns true if they are equal.
if(userPassword.equalsIgnoreCase(formPassword))
{
//do some stuff
}
In JAVA to compare between strings, you should use:
if(userPassword.equals(formPassword)) {
// they are equal
}
Change
if ( userPassword == formPassword ){
to
if ( userPassword.equals(formPassword)){
In Java, String comparison is done with .equals(). Using == compares the object reference and not their values.
Java String comparison
In JAVA you should use equals to judge equal of value of different String.
== is used to judge object pointer for string, so it would only return false in your case.