samir samir - 6 months ago 16
Java Question

How to find the index of array in custom array list ...?

I want tofind the index of the custom array list.
This is my custom array list:

private ArrayList<UserData> ListItems = new ArrayList<>();

UserData list = new UserData("list", "5", R.drawable.email_black, false);

UserData list1 = new UserData("list1", "3",R.drawable.text_msg, false);

UserData list2 = new UserData("list2", "2",R.drawable.phone_call, false);


I am doing like below but not getting the index .

int m = ListItems.indexOf("list1");

UserData obj = ListItems.get(m);
String name = obj.getName();

I need list1 in name string.


The indexOf method is based on the argument and the item in the list being equal. Since they are not (one is a UserData instance and the other is a String), you can't use indexOf. Instead, you'll have to implement this logic yourself:

private UserData getUserDataByName(String name) {
    for (UserData item : listItems) {
        if (item.getName().equals(name)) {
            return item;
    // Not found, return null;
    return null;