我刚刚开始尝试使用Firebase,而我遇到了一些问题。我的Activity中有3个小部件,一个开关,一个进度条和一个显示进度条值的TextView。我尝试使用Firebase存储进度条的状态,然后在 @Override
protected void onStart() {
super.onStart();
//Get values from Firebase
mSettingsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// No settings data.
if (dataSnapshot.getValue(SettingsConfig.class) == null) {
mSettingsConfig = new SettingsConfig();
//Change hardcoded values
mSettingsConfig = new SettingsConfig();
mSettingsConfig.setIsFahrenheit(true);
mSettingsConfig.setRefreshRate(1.0);
mSettingsRef.setValue(mSettingsConfig);
mTempSwitch.setChecked(mSettingsConfig.isFahrenheit());
}
else {
mSettingsConfig = dataSnapshot.getValue(SettingsConfig.class);
mRefreshRateText.setText(mSettingsConfig.getRefreshRate().toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// Sets up SeekBar
mRefreshRateBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String barValueText = String.valueOf(progress) + refreshUnit;
mRefreshRateText.setText(barValueText);
refreshRateValue = ((double) mRefreshRateBar.getProgress());
mSettingsConfig.setRefreshRate(refreshRateValue);
mSettingsRef.child("refreshRate").setValue(mSettingsConfig.getRefreshRate());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mTempSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSettingsConfig.setIsFahrenheit(isChecked);
mSettingsRef.child("isFahrenheit").setValue(mSettingsConfig.isFahrenheit());
}
});
方法中相应地更新其TextView。它用于与开关(切换)相同的目的。
我已经在活动中更新了小部件,或者刚刚使用空的Firebase启动了活动,但是我无法更新小部件。在我的Firebase包含数据时启动活动。我可以看到问题所在,但实际上似乎无法找到解决方案。真的很感激任何帮助。
public class SettingsConfig {
private Double refreshRate;
private Boolean isFahrenheit;
public SettingsConfig(){
}
public SettingsConfig(Double refreshRate, Boolean isFahrenheit){
this.refreshRate = refreshRate;
this.isFahrenheit = isFahrenheit;
}
public Double getRefreshRate() {
return refreshRate;
}
public void setRefreshRate(Double refreshRate) {
this.refreshRate = refreshRate;
}
public Boolean isFahrenheit(){
return isFahrenheit;
}
public void setIsFahrenheit(Boolean isFahrenheit){
this.isFahrenheit = isFahrenheit;
}
以下是Firebase正在使用的POJO(mSettingsConfig):
{{1}}