android - 数据库写入率
问题描述
我正在尝试为一位医生编写许多用户的比率,当我单击按钮时,比率变量不要写入firebase数据库,它的值为 null 任何人都知道为什么变量为 null 而我将其声明为全局变量
public class RatingProvider extends AppCompatActivity {
private Button rate;
private RatingBar bar;
private TextView providerName, fees;
private DatabaseReference myRef,ref;
private String ProvName, ProvID, ProvFees;
private float barRate;
private int count;
private float newRate;
private static Rating Rate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_provider);
rate = (Button) findViewById(R.id.submit);
bar = (RatingBar) findViewById(R.id.ratingBar);
providerName = (TextView) findViewById(R.id.prov);
fees = (TextView) findViewById(R.id.provFee);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
ProvName = prefs.getString("provName", "");
ProvID = prefs.getString("provID", "");
ProvFees = prefs.getString("Fees", "");
myRef = FirebaseDatabase.getInstance().getReference("Rating").child(ProvID);
ref = FirebaseDatabase.getInstance().getReference("Rating").child(ProvID);
providerName.setText(ProvName);
fees.setText(ProvFees);
bar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
barRate = v;
}
});
rate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
Rate = new Rating(1, barRate);
ref.setValue(Rate);
} else {
Rating r = dataSnapshot.getValue(Rating.class);
newRate = ((int) r.getCounter() * ((float) r.getRate()) + barRate) / ((int) r.getCounter() + 1);
count = (int) r.getCounter() + 1;
Rate = new Rating(count, newRate);
ref.setValue(Rate);
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("", "Failed to read value.", error.toException());
}
});
Intent intent = new Intent(getApplicationContext(), SignIn.class);
startActivity(intent);
}
});
}
}
解决方案
推荐阅读
- javascript - 从 DIV 元素中获取值
- kendo-asp.net-mvc - 将 JSON 序列化从 camelCase 更改为 PascalCase
- reactjs - 如何在 react-datetime-range-picker 中启用手动范围日期时间输入?
- javascript - 我在 Schema 中收到一条错误消息,指出它未注册。请参阅下面的说明
- r - 所有变量的直方图,不同的 y 轴刻度
- pandas - 熊猫更改数据透视表中列的顺序
- java - 如何在另一个jsp中正确包含一个jsp
- linux - 如何将选定的文件从一个文件夹移动到另一个带有子文件夹的文件夹
- html - 我的菜单和徽标图像需要拉伸到屏幕的最边缘
- ios - Swift:获取结果集中的行数