简体   繁体   English

根据其中一项将 ArrayList class 过滤到另一个

[英]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.我正在尝试使用itemList对我的commentList和结果进行排序,其中仅包含最后一个值itemPosition等于当前 position 的项目。 I have a class for the List items with 4 values:对于具有 4 个值的列表项,我有一个 class:

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

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:生成的带有itemList的 itemList 显示了以下项目:

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!先感谢您!

First, declare your commentList correctly to avoid compile errors:首先,正确声明您的 commentList 以避免编译错误:

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()...而不是.add().add().add()...

Alternatively, you could use a for loop to create all those new CommentItems.或者,您可以使用 for 循环来创建所有这些新的 CommentItem。 Since you are making user1, user2, etc..., just write:由于您正在制作 user1、user2 等...,只需编写:

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.其中最重要的部分是commentList.get(commentList.size()-1)) ,这是获取列表中最后一个元素的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM