2

I have an ArrayList of object in my Fragment.

ArrayList<MenuListItem> menuItems = new ArrayList<MenuListItem>();
menuItems.add(new MenuListItem("Newsfeed", 20));
menuItems.add(new MenuListItem("FriendsRequest", 1));
menuItems.add(new MenuListItem("Messages", 2));

private class MenuListItem {
    public String label;
    public int count;

    public MenuListItem(String label, int count) {
        this.label = label;
        this.count = count;
    }
}

How can I find the index of object that has the label value "Messages" in my ArrayList from my Activity.

0

4 Answers 4

4

have you try to iterate through elements of menuItems?

something of this sort:

for(MenuListItem menuListItem : menuItems){
    if (menuListItem.label.equals(<what you are looking for>){
        <do something>
        break; // in case when 1st occurence is sufficient
    }
}

side note (not related make your members private and add accessors for them)

Edit: just noticed that you are looking for an index, what i have included is to get an MenuListItem what you can do is iterate and return index if you want.

Sign up to request clarification or add additional context in comments.

Comments

3

try

        String search="Messages";
        for(int i=0;i<menuItems.size();i++){
            if(menuItems.get(i).label.equalsIgnoreCase(search)){
                System.out.println("Index " + i);
                break;
            }
        }

Comments

2
public static int indexOf(final List<MenuListItem> menuItems, final String what) {
    final int size = menuItems.size();
    for (int i = 0; i < size; i++) {
        final String label = menuItems.get(i).label;
        if (label != null && label.equals(what)) {
            return i;
        }
    }
    return -1;
}

Comments

0

There's a easier and standard way to do this.

Eclipse

Right-click inside MenuListItem class and select Source -> Generate hashCode() and equals() once you are done generating the implementations of these methods. You can use ArrayList.indexOf(Object) to get the index of the item you are looking for.

IntelliJ or Android Studio

Right-click inside your editor and click on Generate and from the dialog that pops up, select hashCode() and equals().

The Collections framework has made things easier for you already. Why not leverage it instead of writing loops to find objects? :)

PS: This can be hand written, but this is something best left to the IDE. Because having too many attributes makes it tricky to get it right the first time.

4 Comments

I'm using Android Studio. I checked and it is not possible here.
No matter what, you could do that with Android Studio too. hashCode() and equals() are methods inherited from the Object class. You can do it manually, but it's a task best left to the IDE - Eclipse, Netbeans or IntelliJ.
@Rani, this is how you do it with IntelliJ - jetbrains.com/idea/webhelp/…
I have to find the index of the variable and not the object itself. That's why I had to loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.