如何解决Android Fragment 从非父 Activity 获取值
我正在尝试在片段中显示 BLE 设备的加速度值。我在一个活动中获取设备的值,这不是创建片段的活动。 要计算加速度,我需要根据活动中的值进行简单的计算。 如何将值传递给片段?
我尝试过像这样的 ViewModel,但我无法访问 addValues 方法。
public class DataDisplayViewModel extends ViewModel {
private MutableLiveData<ArrayList<Integer>> xList;
private ArrayList<Integer> xValues = new ArrayList<Integer>();
private static MutableLiveData<ArrayList<Integer>> yList;
private static ArrayList<Integer> yValues = new ArrayList<Integer>();
private static MutableLiveData<ArrayList<Integer>> zList;
private static ArrayList<Integer> zValues = new ArrayList<Integer>();
private static final int STACK_SIZE_X = 6;
private static final int STACK_SIZE_Y = 6;
private static final int STACK_SIZE_Z = 6;
void addX(int XValue){
if (xValues.size() >= STACK_SIZE_X) {
xValues.remove(0);
}
xValues.add(XValue);
xList.setValue(xValues);
}
public MutableLiveData<ArrayList<Integer>> getXValues() {
if(xList == null){
xList = new MutableLiveData<ArrayList<Integer>>();
}
return xList;
}
void addY(int YValue){
if (yValues.size() >= STACK_SIZE_Y) {
yValues.remove(0);
}
yValues.add(YValue);
yList.setValue(yValues);
}
public MutableLiveData<ArrayList<Integer>> getYValues() {
if(yList == null){
yList = new MutableLiveData<ArrayList<Integer>>();
}
return yList;
}
void addZ(int ZValue){
if (zValues.size() >= STACK_SIZE_Z) {
zValues.remove(0);
}
zValues.add(ZValue);
zList.setValue(zValues);
}
public MutableLiveData<ArrayList<Integer>> getZValues() {
if(zList == null){
zList = new MutableLiveData<ArrayList<Integer>>();
}
return zList;
}
}
非常感谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。