以下是我的Android应用程序中每个加息路径的评级系统。用户可以为每条路径评分,并实时显示平均评分。但现在问题是每个用户可以为每个路径多次评级。我想让用户只对每条路径评分一次。我是否需要更改Firebase数据结构,或者我可以按如下方式重写代码以实现此目的?谢谢:))
public class XXXAcitvity extends AppCompatActivity implements RatingDialogListener {
public String trailId="";
public FloatingActionButton rateButton;
public RatingBar ratingBar;
FirebaseDatabase mDatabase;
DatabaseReference trails;
DatabaseReference ratings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.XXX);
Intent intent = this.getIntent();
rateButton=(FloatingActionButton)findViewById(R.id.ratingbtn) ;
ratingBar=(RatingBar)findViewById(R.id.ratingbar);
//Firebase
mDatabase= FirebaseDatabase.getInstance();
trails = mDatabase.getReference("Trails");
ratings = mDatabase.getReference("Ratings");
//get intent
if(getIntent()!=null)
{
trailId=getIntent().getStringExtra("TrailID");
}
if(!trailId.isEmpty())
{
getRatingTrail(trailId);
}
单击评级按钮时,firebase将自动为每个费率生成随机密钥,并根据评级类型设置值,而trailId是指向Firebase中的Ratings子级和Trails子级的关键
@Override
public void onPositiveButtonClicked(int value, String comment) {
//
final Rating rating = new Rating(FirebaseAuth.getInstance().getCurrentUser().getUid(),
trailId,
String.valueOf(value),
comment);
ratings.push().setValue(rating)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
@Override
public void onNegativeButtonClicked() {
}
}
这是函数计算firebase中的平均评级
private void getRatingTrail(String trailId)
{
Query trailRating =
ratings.orderByChild("trailId").equalTo(trailId);
trailRating.addValueEventListener(new ValueEventListener() {
int count=0;
int sum=0;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
Rating item = postSnapshot.getValue(Rating.class);
sum+=Integer.parseInt(item.getRateValue());
count++;
}
if(count!=0)
{
int average= sum/count;
ratingBar.setRating(average);
}
}
评级等级
public class Rating {
private String UserID;
private String trailId;
private String rateValue;
private String comment;
public Rating(){
}
public Rating(String UserID, String trailId, String rateValue, String comment) {
this.UserID = UserID;
this.trailId = trailId;
this.rateValue = rateValue;
this.comment = comment;
}
public void setUserID(String UserID) {
UserID = UserID;
}
public void setTrailId(String trailId) {
trailId = trailId;
}
public void rateValue(String rateVaule) {
this.rateValue = rateVaule;
}
public void setCommennht(String comment) {
this.comment = comment;
}
public String getUserID() {
return UserID;
}
public String getTrailId() {
return trailId;
}
public String getRateValue() {
return rateValue;
}
public String getComment() {
return comment;
}
}
这是我的数据库结构
"Ratings" : {
"-L8qMG4cttR7VIYMrhNS" : {
"comment" : "",
"rateValue" : "1",
"trailId" : "Trail1",
"userID" : "bNgsbQKk7rcb755ISW6oDo7EUaJ3"
},
"-L8qMLRyC5TV2kULIMm2" : {
"comment" : "",
"rateValue" : "1",
"trailId" : "Trail2",
"userID" : "bNgsbQKk7rcb755ISW6oDo7EUaJ3"
},
"-L8qMOdE1QylSX8smLb7" : {
"comment" : "",
"rateValue" : "5",
"trailId" : "Trail2",
"userID" : "bNgsbQKk7rcb755ISW6oDo7EUaJ3"
},
"-L8qMQjjTCqqCBJ5dbFe" : {
"comment" : "",
"rateValue" : "5",
"trailId" : "Trail2",
"userID" : "bNgsbQKk7rcb755ISW6oDo7EUaJ3"
},
"-L8qN3RZTPWXy3cWSf2h" : {
"comment" : "",
"rateValue" : "1",
"trailId" : "Trail2",
"userID" : "oWrUNCA7P8QJlzG1sZ6sHwEcBfu1"
},
"-L8qN8LNzIZNMwu0EGwE" : {
"comment" : "",
"rateValue" : "5",
"trailId" : "Trail2",
"userID" : "oWrUNCA7P8QJlzG1sZ6sHwEcBfu1"
},
"-L8qNEWQ7GSFPpxJMA9A" : {
"comment" : "",
"rateValue" : "5",
"trailId" : "Trail3",
"userID" : "oWrUNCA7P8QJlzG1sZ6sHwEcBfu1"
},
"-L8qNP_e45GaJj4EnmKV" : {
"comment" : "",
"rateValue" : "1",
"trailId" : "Trail3",
"userID" : "kK2ERTyxUgRo4cvrG8gOsN8tRge2"
},
"-L8qNUtkWJjqHlVkFOZy" : {
"comment" : "",
"rateValue" : "1",
"trailId" : "Trail3",
"userID" : "kK2ERTyxUgRo4cvrG8gOsN8tRge2"
}
},
"Trails" : {
"Trail1" : {
"TimeDuration" : "60mins",
"dec" : "the most pretty trails",
"distance" : "20km",
"name" : "Dragon Backs",
"trailId" : "Trail1"
},
"Trail2" : {
"name" : "Hike 2",
"trailId" : "Trail2"
},
"Trail3" : {
"name" : "Hike 3",
"trailId" : "Trail3"
}
},
答案 0 :(得分:1)
要实现这一点,您必须检查用户是否已经使用您的数据库结构对Trail进行评级,您必须检查所有评级表,并查看userId和他投票的跟踪的名称是否在列表。然而,这是非常低效的。
我建议创建一个新表users-trails,存储用户评分的所有路径。
例如对于用户:bNgsbQKk7rcb755ISW6oDo7EUaJ3
"users-trails" : {
"-bNgsbQKk7rcb755ISW6oDo7EUaJ3" : {
Trail1: 1
Trail2: 1
},
然后,当您想要查看用户是否已对Trail1评分时,您只需要快速检查此表。
将数据添加到此表的代码很简单:
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference userTrailsDB = mDatabase.getReference("user-trails");
userTrailsDB.child(userID).child(trailId).setValue(1);
要获得信息,也不是很复杂:
userTrailsDB.child(userID).child(trialId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Do Whatever if exits
}
@Override
public void onCancelled(DatabaseError error) {
}
};