Olivia
Olivia

Reputation: 1

Filter an ArrayList class into another based on one of the items

I'm trying to sort my commentList and result with an itemList with only the items where the last value, itemPosition equals the current position. I have a class for the List items with 4 values:

class CommentItem {
    int profile; 
    String user;
    String message; 
    int itemPosition;
}

I am trying to use this code to sort it, but it is only adding the correct number of values instead of the actual values that I want.

for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(new CommentItem(
            commentList.get(position).getProfile(),
            commentList.get(position).getUser(),
            commentList.get(position).getMessage(),
            commentList.get(position).getItemPosition()
        ));
    }
}

This is the commentList:

commentList = new ArrayList<>();
commentList.add(new CommentItem(R.drawable.circleicon, "User0", "Message0", 1));
commentList.add(new CommentItem(R.drawable.circleicon, "User1", "Message1", 2));
commentList.add(new CommentItem(R.drawable.circleicon, "User2", "Message2", 3));
commentList.add(new CommentItem(R.drawable.circleicon, "User3", "Message3", 4));
commentList.add(new CommentItem(R.drawable.circleicon, "User4", "Message4", 1));
commentList.add(new CommentItem(R.drawable.circleicon, "User5", "Message5", 2));
commentList.add(new CommentItem(R.drawable.circleicon, "User6", "Message6", 3));
commentList.add(new CommentItem(R.drawable.circleicon, "User7", "Message7", 4));
commentList.add(new CommentItem(R.drawable.circleicon, "User8", "Message8", 0));

The resulting itemList with the position has these items displayed:

commentList.add(new CommentItem(R.drawable.circleicon, "User0", "Message0", 1));
commentList.add(new CommentItem(R.drawable.circleicon, "User1", "Message1", 2));

I have also tried all of these codes:

for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(new CommentItem(
            item.getProfile(), item.getUser(), 
            item.getMessage(), item.getItemPosition()
    ));
    }
}
for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(item);
    }
}
for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(commentList.get(commentList.indexOf(item)));
    }
}

With the same result.

Thank you in advance!

Upvotes: 0

Views: 39

Answers (1)

Cole Henrich
Cole Henrich

Reputation: 165

First, declare your commentList correctly to avoid compile errors:

ArrayList<CommentItem>commentList = new ArrayList<CommentItem>();

Also, it is more efficient to write:

...new ArrayList<CommentItem>(Arrays.asList(/*insert contents here*/));

Instead of .add() .add() .add()...

Alternatively, you could use a for loop to create all those new CommentItems. Since you are making user1, user2, etc..., just write:

for (int i = 0, j = 1; i < 9 && j < 5; i++, j++){
    commentList.add(
    new CommentItem(
    R.drawable.circleicon, ("User" + i), ("Message" + i), j));
}

Finally, to your question:

ArrayList<CommentItem> resultItems = new ArrayList<CommentItem>();
for (int index = 0; index < commentList.size(); index++){
    CommentItem item = commentList.get(index);
    if (item == commentList.get(commentList.size()-1))//last index{
    resultItems.add(item);
    }
}

The most important part of that is commentList.get(commentList.size()-1)), which is an easy way to get the last element in a list.

Upvotes: 1

Related Questions