android片段从非父活动获取值

klh5stk1  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(232)

我试图在碎片中显示一个可移动设备的加速度值。我在活动中获取设备的值,而不是创建片段的活动。为了计算加速度,我需要从活动中的值做一个简单的计算。如何将值传递给片段?
我试过使用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;
}

}
非常感谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题