Java 如何在Android中使用列表的forech条件
在我的应用程序中,我有一个Java 如何在Android中使用列表的forech条件,java,android,list,kotlin,Java,Android,List,Kotlin,在我的应用程序中,我有一个列表,在这个列表中,我有一个整数值。 我想用列表的整数检查postid,如果此id相等,我应该更改UI。 我写了下面的代码,但在我的代码中只更改最后一项UI。 我的代码: for (int i = 0; i <= data.getBasket().size() - 1; i++) { Log.e("BooksIdLog", bookId + " - " + data.getBasket().get(i).getBookId()); if (book
列表
,在这个列表
中,我有一个整数
值。我想用
列表的整数检查postid
,如果此id
相等,我应该更改UI。
我写了下面的代码,但在我的代码中只更改最后一项UI。
我的代码:
for (int i = 0; i <= data.getBasket().size() - 1; i++) {
Log.e("BooksIdLog", bookId + " - " + data.getBasket().get(i).getBookId());
if (bookId == data.getBasket().get(i).getBookId()) {
detailPage_bottomSheetPrintVersion.setBackground(context.getResources()
.getDrawable(R.drawable.bg_rounded_stroke_red));
detailPage_bottomSheetPrintVersion.setText(getString(R.string.removeFromBasket));
detailPage_bottomSheetPrintVersion.setTextColor(context.getResources()
.getColor(R.color.red));
detailPage_bottomSheetPrintVersion.setOnClickListener(view ->
presenter.callRemoveBookBasketApi(apiHash, userToken, bookId)
);
} else {
detailPage_bottomSheetPrintVersion.setBgColor(context.getResources()
.getColor(R.color.platinumGray));
detailPage_bottomSheetPrintVersion.setText(getString(R.string.printVersion));
detailPage_bottomSheetPrintVersion.setTextColor(context.getResources()
.getColor(android.R.color.black));
detailPage_bottomSheetPrintVersion.setOnClickListener(view -> {
presenter.callAddBookBasketApi(apiHash, userToken, bookId);
});
}
}
例如:
我在这个列表中有3项,这3项id是=155060-155070-154030,当转到详细信息页面活动时,只需更新这个id中的UI154030不是所有id
如何修复它?如果您只想使用forEach
而不是的经典for
-循环来处理可变列表
(例如),那么您可以在Kotlin中这样做:
data.getBasket().forEach(basket -> {
Log.e("BooksIdLog", bookId + " - " + basket.getBookId());
if (bookId == basket.getBookId()) {
...
} else {
...
}
}
但我认为你的循环工作正常
我猜您没有正确更新用于比较的bookId
的值。检查它在哪里声明、定义和使用…数据中的数据类型是什么?类似于Book
?@deHaar,请查看我的更新帖子OK,因此您正在使用Kotlin,请相应地更新您的问题标签…getBasket()返回什么?List?@deHaar,好的,但是你能帮我吗?@DrKingo在Java中,你可以为
-每个循环:为(Basket-Basket:data.getBasket()){…}
data.getBasket().forEach(basket -> {
Log.e("BooksIdLog", bookId + " - " + basket.getBookId());
if (bookId == basket.getBookId()) {
...
} else {
...
}
}