在本课程中,我尝试更改评分。我应该收到两个变量,但我不能这样做。检查我的ChildChanged
监听器,错误就在附近。
这是我的数据库:
这是我的代码:
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private Button btn;
String TAG = "first";
float r,n,r1,r2,rat; String LEC_ID = "5058586";
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt = (TextView)findViewById(R.id.txt);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
//mDatabase.addValueEventListener(postListener);
final RatingBar rBar = (RatingBar) findViewById(R.id.rBar);
rBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
r1 = rBar.getNumStars();
//r1=rating;
mDatabase = database.getReference();
mDatabase.child(LEC_ID).addChildEventListener(childEventListener);
rat = (rating+r)/n;
rBar.setRating(rat);
Toast.makeText(MainActivity.this, "рейтинг: " + String.valueOf(rating)+" "+r,
Toast.LENGTH_LONG).show();
mDatabase.child(LEC_ID).child("n").setValue(n);
mDatabase.child(LEC_ID).child("r").setValue(r2);
}
});
}
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
GenericTypeIndicator<Map<String, Float>> genericTypeIndicator = new GenericTypeIndicator<Map<String, Float>>() {}; //here it crashes
Map<String, Float> map = dataSnapshot.getValue(genericTypeIndicator );
n = map.get("n")+1;
r = map.get("r");
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
}