Java “的布尔逻辑”;“likeCount”;递增器

Java “的布尔逻辑”;“likeCount”;递增器,java,android,parse-platform,boolean,increment,Java,Android,Parse Platform,Boolean,Increment,我创建了这个incrementLike方法,在我的数据库中递增一个整数。不幸的是,我不知道如何阻止用户每次单击“like”图像时不断增加该值 我假设likeAlready=true语句不可访问,因为它似乎总是false。我的逻辑/实现有什么问题 private void incrementLike(int position) { ParseQuery<ParseObject> query = new ParseQuery<>("Comment");

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

我假设
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());
            }
        });
    }
private void incrementLike(int位置){
ParseQuery=新的ParseQuery(“注释”);
query.whereEqualTo(ParseConstants.KEY_OBJECT_ID,getItem(position.getObjectId());
query.findInBackground((注释,e)->{
如果(e==null){
//迭代所有消息并删除它们
for(ParseObject注释对象:注释)
{
布尔值likedAlready=false;
if(likedAlready==false){
commentObject.increment(“likeCount”);
commentObject.saveInBackground();
Toast.makeText(getContext(),“Post liked”,Toast.LENGTH_SHORT.show();
likedAlready=真;
}否则{
Toast.makeText(getContext(),“你已经喜欢这篇文章了”,Toast.LENGTH_SHORT.show();
}
}
}否则{
Log.e(“Error”,e.getMessage());
}
});
}
更新:

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

当按下“like”图像时,我在“like”表中创建了一个新对象,带有senderId和commentObjectId。最后一步是确定是否已为该特定注释对象发送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());
            }
        });
    }
private void incrementLike(int位置){
ParseQuery查询=新的ParseQuery(ParseConstants.CLASS\u注释);
query.whereEqualTo(ParseConstants.KEY_OBJECT_ID,getItem(position.getObjectId());
query.findInBackground((注释,e)->{
如果(e==null){
//迭代所有消息
for(ParseObject注释对象:注释)
{
ParseObject newLike=新的ParseObject(ParseConstants.CLASS_LIKE);
newLike.put(ParseConstants.KEY_SENDER_ID,ParseUser.getCurrentUser());
put(ParseConstants.KEY\u COMMENT\u OBJECT\u ID,commentObject);
newLike.saveInBackground();
Toast.makeText(getContext(),“喜欢Yeet”,Toast.LENGTH_SHORT.show();
ParseQuery query2=新的ParseQuery(ParseConstants.CLASS_-LIKE);
query2.whereEqualTo(ParseConstants.KEY_COMMENT_OBJECT_ID,commentObject.getObjectId());
查询2.findInBackground((评论2,e2)->{
if(e2==null){
//现在,我有了一个类似对象的列表,其中commentObjectId与此注释关联
//仅当当前ParseUser的用户对象ID在此列表中不存在时才递增
commentObject.increment(“likeCount”);
commentObject.saveInBackground();
/*this.adapter.addAll(mYeets)*/
notifyDataSetChanged();
}否则{
Log.e(“Error”,e2.getMessage());
}
});
}
}否则{
Log.e(“Error”,e.getMessage());
}
});
}
我很难把最后一步想清楚。有什么建议吗

不幸的是,我不知道如何阻止用户每次单击“like”图像时不断增加该值

当前代码中的简单布尔逻辑实际上是不可行的,因为除了增加db存储计数器外,还必须存储增加它的信息(即用户id)(也在db中)。您可以事先检查,然后显示“喜欢”按钮,只有这样的用户还没有这样做

不幸的是,我不知道如何阻止用户每次单击“like”图像时不断增加该值


当前代码中的简单布尔逻辑实际上是不可行的,因为除了增加db存储计数器外,还必须存储增加它的信息(即用户id)(也在db中)。您可以事先检查,然后仅当该用户尚未这样做时才显示“like”按钮。

问题是每次运行incrementLike()方法时,它都会首先将布尔值初始化为false。因此,它的解决方案之一就是将布尔值初始化为全局变量。但这种解决方案的问题是,每次用户运行应用程序时,布尔值都会丢失。所以,如果您想永久存储它,您必须将它存储在数据库中,关于谁喜欢,然后您可以从数据库中检索已经喜欢或不喜欢的人

问题是每次运行incrementLike()方法时,它都会首先将布尔值初始化为false。因此,它的解决方案之一就是将布尔值初始化为全局变量。但这种解决方案的问题是,每次用户运行应用程序时,布尔值都会丢失。所以,如果您想永久存储它,您必须将它存储在数据库中,关于谁喜欢,然后您可以从数据库中检索已经喜欢或不喜欢的人

你应该存储谁“喜欢”它(比如谁点击了图标)。您可以使用一个循环来检查一些内容,例如,您在其中将用户名保存为键值对。另一个有帮助的参考是这一点。

您应该存储“喜欢”它的人(如who)