【问题标题】:Filter an ArrayList class into another based on one of the items根据其中一项将 ArrayList 类过滤为另一个类
【发布时间】:2021-05-08 22:16:42
【问题描述】:

我正在尝试对我的commentList 进行排序,结果是itemList,只有最后一个值itemPosition 等于当前位置的项目。我有一个包含 4 个值的列表项的类:

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

我正在尝试使用此代码对其进行排序,但它只是添加正确数量的值而不是我想要的实际值。

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()
        ));
    }
}

这是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));

带有位置的结果 itemList 显示以下项目:

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

我也尝试了所有这些代码:

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)));
    }
}

结果相同。

提前谢谢你!

【问题讨论】:

    标签: java android android-studio arraylist


    【解决方案1】:

    首先,正确声明你的commentList以避免编译错误:

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

    另外,写起来效率更高:

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

    而不是.add() .add() .add()...

    或者,您可以使用 for 循环来创建所有这些新的 CommentItem。由于您正在制作 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));
    }
    

    最后,关于你的问题:

    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);
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 2016-06-21
      • 2017-10-19
      • 2013-12-10
      相关资源
      最近更新 更多