Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Check if a User Id exists in ArrayList?

Posted on November 14, 2022

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

READ  [ANSWERED] android - GestureDetector changing the style properties of the child widget
Powered by Inline Related Posts

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()

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress