Solution 1 :
Updated:
As per ur request, modified already provided function.
private Userlist getExistingUser(){
Userlist existingUser = null;
//#1. Iterate through list
for(Userlist userlist: UserListArray){
//#2. Check if user id is matching
if(userlist.mId == Users.getId()){
//#3. Matches, user id exists, get the record and out from loop
existingUser = userlist;
break;
}
}
return existingUser;
}
Where getting this function, pls check with null or not.
Userlist user = getExistingUser();
If (user!=null){ //means user is present in the list array
//so, Get the quantity here..
}
It might solve your problem.
Old:
Try with following function, it will return boolean as user id exists or not
private boolean checkUserIdExist(){
boolean isUserIdExist = false;
//#1. Iterate through list
for(Userlist userlist: UserListArray){
//#2. Check if user id is matching
if(userlist.mId == Users.getId()){
//#3. Matches, user id exists, out from loop
isUserIdExist = true;
break;
}
}
return isUserIdExist;
}
Solution 2 :
public boolean checkUserExist(User user){
for(i=0; i<UserListArray.size(); i++){
if(UserListArray.get(i).getmId == user.getmId){
return true;
}
}
return false
}
Solution 3 :
You can use anyMatch
of Stream API
if(UserListArray.stream().anyMatch(u -> u.getmId().equals(Users.getId()))){
}
And to get user
UserListArray.stream().filter(u -> u.getmId().equals(Users.getId())).findFirst().get()
Problem :
This Is My Data Handler Class
public class Userlist {
private String mId;
private String mQuantity;
public String getmId() {
return mId;
}
public void setmId(String mId) {
this.mId = mId;
}
public String getmQuantity() {
return mQuantity;
}
public void setmQuantity(String mQuantity) {
this.mQuantity = mQuantity;
}
public Cartlist(String mId, String mQuantity) {
this.mId = mId;
this.mQuantity = mQuantity;
}
}
And This Is My ArrayList Code
public static ArrayList<Userlist> UserListArray = new ArrayList<>();
And Here Is I Add Users
Home.UserListArray.add(new Userlist ( "1" , "595" ));
Home.UserListArray.add(new Userlist ( "2" , "5558" ));
Home.UserListArray.add(new Userlist ( "55" , "5154" ));
Home.UserListArray.add(new Userlist ( "97" , "513" ));
Home.UserListArray.add(new Userlist ( "16" , "565" ));
Home.UserListArray.add(new Userlist ( "21" , "598" ));
Now I Want To Check If There Is Userid Exits In Arraylist Or Not
if (UserListArray.contains( Users.getId() ) == true){
Toast.makeText(context,"List Conatin User",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"List Not Conatin User",Toast.LENGTH_SHORT).show();
}
ANYONE CAN HELP
REGARDS
AHSAN JAVED
Comments
Comment posted by Robert
Instead of an
Comment posted by King Developers
please Provide some Example I’m new or give any video example how to Check If There Is Userid Exits In Arraylist Or Not
Comment posted by Robert
You learn better if you don’t follow video tutorials. Therefore just use
Comment posted by King Developers
That’s Work But Now i Want mQuantity of that User After There is Id Exist From Users.getId()
Comment posted by King Developers
That’s Work But Now i Want mQuantity of that User After There is Id Exist From Users.getId()