gpt4 book ai didi

java - 使用 Java 8 forEach 将 View 添加到 tablerow

转载 作者:行者123 更新时间:2023-12-01 19:59:53 25 4
gpt4 key购买 nike

TableRow tableRow = new TableRow(getContext());
List<File> threeFiles = listOfFiles.subList(i, i+2);
threeFiles.forEach(action -> tableRow.addView(new ImageView(getActivity().getApplicationContext()).setBackground(Drawable.createFromPath(action.getAbsolutePath()))));

在最后一行,它说 ViewGroup 中的 addView(android.view.View) 不能应用于 (void)。如何使用 Java 8 ForEach 将带有文件源的 View 添加到表行?我正在尝试更多地使用 Java 8 Streams 来使我的代码更加简洁

最佳答案

您的问题是在末尾添加 setBackground 会使该行返回 void,因为 setBackground 返回 void,您可能需要像这样吐出该行:

threeFiles.forEach(action -> {
ImageView imageView = new ImageView(getActivity().getApplicationContext());
imageView.setBackground(Drawable.createFromPath(action.getAbsolutePath());
tableRow.addView(imageView);
});

关于java - 使用 Java 8 forEach 将 View 添加到 tablerow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59010592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com