【发布时间】:2017-01-04 14:09:42
【问题描述】:
我创建了这个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”的新类表,其中包含两个指针列,即用户的 objectId 的 senderId 和相关 Comment 的 objectId 的 commentObjectId。
当按下“赞”图像时,我在“赞”表中创建了一个新对象,其中包含 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());
}
});
}
我在考虑最后一步时遇到了麻烦。有什么建议吗?
【问题讨论】:
标签: java android parse-platform boolean increment