gpt4 book ai didi

java - "likeCount"增量器的 boolean 逻辑

转载 作者:行者123 更新时间:2023-11-29 08:42:08 26 4
gpt4 key购买 nike

我创建了这个 incrementLike 方法来递增我的数据库中的一个整数。不幸的是,我无法弄清楚如何阻止用户在每次单击“喜欢”图像时不断增加该值。

我想 likeAlready = true 语句无法访问,因为它似乎总是 false。我的逻辑/实现有什么问题?

private void incrementLike(int position) {
ParseQuery<ParseObject> query = new ParseQuery<>("Comment");
query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, getItem(position).getObjectId());
query.findInBackground((comment, e) -> {
if (e == null) {
// Iterate over all messages and delete them
for (ParseObject commentObject : comment)
{
boolean likedAlready = false;
if (likedAlready == false) {
commentObject.increment("likeCount");
commentObject.saveInBackground();
Toast.makeText(getContext(), "Post liked", Toast.LENGTH_SHORT).show();
likedAlready = true;
} else {
Toast.makeText(getContext(), "You already liked this post", Toast.LENGTH_SHORT).show();
}

}
} else {
Log.e("Error", e.getMessage());
}
});
}

更新:

我创建了一个名为“Like”的新类表,其中包含两个指针列,即 senderId,即用户的 objectId 和 commentObjectId,即相关评论的 objectId。

当按下“赞”图像时,我在“赞”表中创建一个新对象,其中包含 senderId 和 commentObjectId。最后一步是确定是否已经为该特定 Comment 对象发送了 senderId。这是我目前所拥有的:

private void incrementLike(int position) {

ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_COMMENT);
query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, getItem(position).getObjectId());
query.findInBackground((comment, e) -> {
if (e == null) {
// Iterate over all messages
for (ParseObject commentObject : comment)
{

ParseObject newLike = new ParseObject(ParseConstants.CLASS_LIKE);
newLike.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser());
newLike.put(ParseConstants.KEY_COMMENT_OBJECT_ID, commentObject);
newLike.saveInBackground();
Toast.makeText(getContext(), "Yeet liked", Toast.LENGTH_SHORT).show();

ParseQuery<ParseObject> query2 = new ParseQuery<>(ParseConstants.CLASS_LIKE);
query2.whereEqualTo(ParseConstants.KEY_COMMENT_OBJECT_ID, commentObject.getObjectId());
query2.findInBackground((comment2, e2) -> {
if (e2 == null) {

// I now have a list of Like objects with the commentObjectId associated with this Comment
// Only increment if the User objectId of the current ParseUser does not exist in this list
commentObject.increment("likeCount");
commentObject.saveInBackground();
/*this.adapter.addAll(mYeets);*/
notifyDataSetChanged();


} else {
Log.e("Error", e2.getMessage());
}
});

}
} else {
Log.e("Error", e.getMessage());
}
});
}

我在思考最后一步时遇到了麻烦。有什么建议吗?

最佳答案

Unfortunately I can't figure out how to stop the user from continually incrementing that value every time they click the "like" image.

您当前代码中的简单 boolean 逻辑实际上是不可行的,因为除了递增数据库存储计数器之外,您还必须存储递增它的信息(即用户 ID)(也在数据库中)。您可以提前检查并仅在该用户尚未这样做时才显示“喜欢”按钮。

关于java - "likeCount"增量器的 boolean 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191093/

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