gpt4 book ai didi

java - Android Firestore 将限制为每个用户 1 票

转载 作者:行者123 更新时间:2023-11-29 04:07:45 39 4
gpt4 key购买 nike

我正在从头开始创建我的第一个应用程序,它是 Android 上的一个论坛应用程序,我已经进入了它的赞成票/反对票部分。

我已将其设置为我的用户可以投反对票(类似于 StackOverflow),但我不知道如何将其限制为每个用户一票。

我正在使用 Firebase-Firestore 作为数据库。

我将赞成票和反对票切换按钮设置为在按钮单击监听器中更新 firestore 中的分数,每个按钮都有各自的选择器 item.xml

任何人都可以阐明一种类似于堆栈溢出的方法来限制用户对每个用户的每个答案投一票吗?

感谢任何帮助。

edit

尝试提供的答案之前的当前代码。

应答适配器

public class AnswerAdapter extends FirestoreRecyclerAdapter<Answer, AnswerAdapter.AnswerHolder> {

private QuestionAdapter.OnItemClickListener listener;
private Context mContext;
private static final String TAG = "AnswerAdapter";

/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {@link
* FirestoreRecyclerOptions} for configuration options.
*
* @param options
*/

public AnswerAdapter(@NonNull FirestoreRecyclerOptions<Answer> options) {

super(options);
}

@NonNull
@Override
public AnswerHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.answer_item, viewGroup, false);
return new AnswerHolder(v);
}

@Override
protected void onBindViewHolder(@NonNull final AnswerAdapter.AnswerHolder answerHolder, final int position, @NonNull final Answer model) {

answerHolder.answerItemTextView.setText(model.getAnswer());
answerHolder.answerAuthorTextView.setText(model.getAuthor());
answerHolder.answerTimeStampTextView.setText(model.getTimestamp());
answerHolder.answerScoreTextView.setText(getSnapshots().getSnapshot(position).get("answerScore").toString());

answerHolder.mAnswerUpVoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 2019-07-26 check order of operations. seems to go through upvote and then also through downvote unchecking state
if (answerHolder.mAnswerUpVoteButton.isChecked()){

answerUpVote(answerHolder, position, model);
} else {
// answerDownVote(answerHolder, position, model);
}
}
});

public class AnswerHolder extends RecyclerView.ViewHolder{

TextView answerItemTextView;
TextView answerScoreTextView;
ToggleButton answerCheckMark;
TextView answerAuthorTextView;
TextView answerTimeStampTextView;
ToggleButton mAnswerUpVoteButton;
ToggleButton mAnswerDownVoteButton;


public AnswerHolder(@NonNull final View itemView) {
super(itemView);
answerItemTextView = itemView.findViewById(R.id.answerItemTextViewId);
answerScoreTextView = itemView.findViewById(R.id.questionScoreId);
answerCheckMark = itemView.findViewById(R.id.answerCheckMarkId);
answerAuthorTextView = itemView.findViewById(R.id.answerAuthorTextViewId);
answerTimeStampTextView = itemView.findViewById(R.id.answerTimeStampTextViewId);
mAnswerUpVoteButton = itemView.findViewById(R.id.answerUpVoteId);
mAnswerDownVoteButton = itemView.findViewById(R.id.answerDownVoteId);

mContext = itemView.getContext();

}

}

private void answerUpVote(@NonNull final AnswerAdapter.AnswerHolder answerHolder, final int position, @NonNull final Answer model) {
final String answerScoreFBValue = getSnapshots().getSnapshot(position).get("answerScore").toString();
final String answerFirebaseIdString = getSnapshots().getSnapshot(position).get("answerFirebaseId").toString();
// Toast.makeText(mContext, "upvote button clicked " + answerScoreFBValue, Toast.LENGTH_SHORT).show();
final CollectionReference answerCollectionRef = FirebaseFirestore.getInstance().collection("Answers");
final DocumentReference answerDocRef = answerCollectionRef.document(answerFirebaseIdString);


answerDocRef.update("answerScore", FieldValue.increment(1)).addOnSuccessListener(new OnSuccessListener<Void>() {
// answerRef.document().update("answerscore", answerScoreTestInt).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: answerScore incremented");
// answerHolder.answerScoreTextView.setText("testing");

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "onFailure: answerScore was not incremented", e);
}
});
}

Answer.java

package com.example.stairmaster.models;

public class Answer {

private String answer;
private String comment;
private String timestamp;
private int answerScore;
private String author;
private String answerFirebaseId;
private String parentQuestionId;


public Answer(String answer, String timeStamp, String author, String parentQuestionId, int answerScore) {

this.answer = answer;
this.timestamp = timeStamp;
this.answerScore = answerScore;
this.author = author;
this.parentQuestionId = parentQuestionId;
}

public Answer () {
// public no-arg constructor needed
}

public String getAnswerFirebaseId() {
return answerFirebaseId;
}

public void setAnswerFirebaseId(String answerFirebaseId) {
this.answerFirebaseId = answerFirebaseId;
}

public String getAnswer() {
return answer;
}

public void setAnswer(String answer) {
this.answer = answer;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}

public int getAnswerScore() {
return answerScore;
}

public void setAnswerScore(int answerScore) {
answerScore = answerScore;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getParentQuestionId() {
return parentQuestionId;
}

public void setParentQuestionId(String parentQuestionId) {
this.parentQuestionId = parentQuestionId;
}
}

最佳答案

您可能会考虑将您的 firebase 数据库中的数据库表修改为如下所示。

user_votes:
- userid: 1234232 // A sample user id for your user
- forumid: 8769765 // The id of the forum or blog
- upvote: 1 // Number of upvotes in that specific forum/blog
- downvote: 0 // Number indicates the downvote to that forum/blog
- forumid: 1233432
- upvote: 1
- downvote: 0
- forumid: 8712169767
- upvote: 0
- downvote: 1

另一个记录特定论坛/博客的赞成票/反对票的表格可能如下所示。

all_votes:
- forumid: 8769765 // The id of the forum or blog
- upvote: 9876 // Number of upvotes in that specific forum/blog
- downvote: 123 // Number indicates the downvote to that forum/blog
- forumid: 1233432
- upvote: 87
- downvote: 12
- forumid: 8712169767
- upvote: 112
- downvote: 10

现在,根据这两个表,您可以轻松决定是否要在 all_votes 表中为论坛 ID 发出 setValue 命令。如果发现用户已经发出了赞成票,则使应用程序逻辑不再在 all_votes 表中为该特定用户发出另一个赞成票。

希望对您有所帮助!

关于java - Android Firestore 将限制为每个用户 1 票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57225467/

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