【问题标题】:Boolean logic for "likeCount" incrementer“likeCount”增量器的布尔逻辑
【发布时间】: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


    【解决方案1】:

    不幸的是,我不知道如何阻止用户在每次点击“喜欢”图片时不断增加该值。

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

    【讨论】:

    • 我知道这不是好的数据库设计,但我的 likeCount 列只是一个整数,它附加到每个 Comment 类对象。你会推荐什么?
    • 在数据库中创建第二个表,其中包含 comment_iduser_id 等列,并在此处保存“like”条目。
    • 当然,这就足够了(假设 comment_id 和 user_id 是唯一的)。您只需要知道给定的 user_id 是否没有为给定的评论投票。
    • 也许从 Likes 列表中创建一个 User objectId 的 ArrayList,看看当前 ParseUser 的 objectId 是否不在其中?
    • 好吧,你可以,但你仍然需要将它存储在某个地方,以便在重启/应用重启时保持持久性。如果将其放入共享首选项中,则检查是否有相关条目将需要在返回数组时读取。不是很有效。使用数据库
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      你应该存储谁“喜欢”它(就像谁点击了图标)。您可以使用循环来检查 Dictionary 之类的内容,您已将用户名保存为键值对。另一个可以提供帮助的参考是 Shared Preferences

      【讨论】:

        猜你喜欢
        • 2016-01-12
        • 1970-01-01
        • 2011-12-22
        • 2020-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-18
        相关资源
        最近更新 更多