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!
question from:
https://stackoverflow.com/questions/66048587/filter-an-arraylist-class-into-another-based-on-one-of-the-items