私はこのデータベースを整数化するincrementLike
メソッドを作成しました。残念ながら、ユーザーが「好きな」画像をクリックするたびにその値を継続的にインクリメントするのを止める方法を理解することはできません。"likeCount"インクリメンタのブール論理
いつもfalse
と思われるので、likeAlready = true
ステートメントに到達できないとします。論理/実装に何が問題なのですか?
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());
}
});
}
更新:
私は、関連するコメントのOBJECTID 2つのポインタの列、すなわちSENDERIDユーザーの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());
}
});
}
私は最後のステップで考えるのが苦労しています。助言がありますか?
私はそれが良いデータベースデザインではないことを知っていますが、私のlikeCount列はすべてのCommentクラスオブジェクトに添付された単なる整数です。代わりに何をお勧めしますか? – santafebound
'comment_id'や' user_id'のようなカラムを使ってデータベースに2番目のテーブルを作成し、そこに「like」というエントリを保存します。 –
"ポインタ"にすることをお勧めしますか? – santafebound