使用 Livedata 并在显示 MutableLiveData<Float> 结果时遇到问题
Using Livedata and having a problem with displaying MutableLiveData<Float> result
我正在测试 Android 架构组件的 ViewModel 和 LiveData。我有这个基本的 Activity 和一个 EditText 来从用户那里接收一个数字,当单击 Convert 按钮时,Textview 应该给出乘以 0.87 的结果。例如,当我在 EditText 中输入 100 并单击 Convert 按钮时,它应该给出 87 的结果。但是当我单击 Convert 按钮时,Textview 给出 "android.arch.lifecycle.MutableLiveData@eb84a7b" 的值。当我旋转屏幕时,结果变成了 87。请你看看我的代码,告诉我应该在哪里修改代码,以便在我单击“转换”按钮时结果给出数字?
ViewModelLiveDataActivity.java:
public class ViewModelLiveDataActivity extends AppCompatActivity {
private ViewModel2 mViewModel;
private EditText mEditText;
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewmodellivedata_activity_layout);
mViewModel = ViewModelProviders.of(this).get(ViewModel2.class);
mEditText = (EditText) findViewById(R.id.livedata_editText);
mTextView = (TextView) findViewById(R.id.livedata_textView);
mButton = (Button) findViewById(R.id.livedata_button);
final Observer<Float> resultObserver = new Observer<Float>() {
@Override
public void onChanged(@Nullable final Float result) {
mTextView.setText(result.toString());
}
};
mViewModel.getResult().observe(this, resultObserver);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Doing the calculations using ViewModel class
String dollarText = mEditText.getText().toString();
mViewModel.setAmount(dollarText);
mTextView.setText(mViewModel.getResult().toString());
}
});
}
}
ViewModel2.java:
public class ViewModel2 extends ViewModel {
private static final float EURO_DOLLAR_EXCHANGE_RATE = 0.87f;
private String dollarString = ""; //the value of UI EditText
private MutableLiveData<Float> result = new MutableLiveData<>();
public void setAmount(String value) {
this.dollarString = value;
result.setValue(Float.valueOf(dollarString) * EURO_DOLLAR_EXCHANGE_RATE);
}
public MutableLiveData<Float> getResult() {
return result;
}
}
使用 getValue() 而不是 toString()
mTextView.setText(mViewModel.getResult().getValue());
我正在测试 Android 架构组件的 ViewModel 和 LiveData。我有这个基本的 Activity 和一个 EditText 来从用户那里接收一个数字,当单击 Convert 按钮时,Textview 应该给出乘以 0.87 的结果。例如,当我在 EditText 中输入 100 并单击 Convert 按钮时,它应该给出 87 的结果。但是当我单击 Convert 按钮时,Textview 给出 "android.arch.lifecycle.MutableLiveData@eb84a7b" 的值。当我旋转屏幕时,结果变成了 87。请你看看我的代码,告诉我应该在哪里修改代码,以便在我单击“转换”按钮时结果给出数字?
ViewModelLiveDataActivity.java:
public class ViewModelLiveDataActivity extends AppCompatActivity {
private ViewModel2 mViewModel;
private EditText mEditText;
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewmodellivedata_activity_layout);
mViewModel = ViewModelProviders.of(this).get(ViewModel2.class);
mEditText = (EditText) findViewById(R.id.livedata_editText);
mTextView = (TextView) findViewById(R.id.livedata_textView);
mButton = (Button) findViewById(R.id.livedata_button);
final Observer<Float> resultObserver = new Observer<Float>() {
@Override
public void onChanged(@Nullable final Float result) {
mTextView.setText(result.toString());
}
};
mViewModel.getResult().observe(this, resultObserver);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Doing the calculations using ViewModel class
String dollarText = mEditText.getText().toString();
mViewModel.setAmount(dollarText);
mTextView.setText(mViewModel.getResult().toString());
}
});
}
}
ViewModel2.java:
public class ViewModel2 extends ViewModel {
private static final float EURO_DOLLAR_EXCHANGE_RATE = 0.87f;
private String dollarString = ""; //the value of UI EditText
private MutableLiveData<Float> result = new MutableLiveData<>();
public void setAmount(String value) {
this.dollarString = value;
result.setValue(Float.valueOf(dollarString) * EURO_DOLLAR_EXCHANGE_RATE);
}
public MutableLiveData<Float> getResult() {
return result;
}
}
使用 getValue() 而不是 toString()
mTextView.setText(mViewModel.getResult().getValue());