尝试使用getParentFragment从父片段获取值,返回null

时间:2016-05-08 23:09:00

标签: java android android-fragments

编辑4:这将是我最新的代码,我会在此处删除并更新。

movementSetsRepsFrag.java:https://codeshare.io/GZm47

setSchemes.java:https://codeshare.io/kMdj5

ControllerData.java: https://codeshare.io/nAiSh

Results.java:https://codeshare.io/vIs5Q

编辑3:

在setSchemes.java中,我删除了这段代码:

    // Here I'm trying to assign a name to the Child Fragment Manager
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();

    // And here I'm trying to use that to call my getSpinner method
    // But "getSpinnerValue()" is red in the IDE and won't compile
    String parentSpinnerValue = fragmentTransaction.getSpinnerValue();

我现在正尝试在另一个论坛上建议的东西,一个有人向我展示的回调技术(作为getParentFragment的一种方式):

在setSchemes.java中

    // above onCreateView
    public interface setSchemesCallback{
        String getSpinnerValue();
    }

    private setSchemesCallback callback;

    // in onCreateView
    callback = (setSchemesCallback) getParentFragment();

    // in onPause
    String parentSpinnerValue = callback.getSpinnerValue();

编辑2:

以下是我用来收集和整理数据的对象ControllerData.java

public class ControllerData {

    public String exSpinnerValue1;

    // DECLARE ARRAYS

    String[] exArray1 = new String[15];
    String[] exArray2 = new String[15];   

    private static ControllerData controller;

    public static ControllerData getInstance(){
       if (controller == null){
           controller = new ControllerData();
       }

       return controller;
    }

    // PARENT SPINNER SETTERS
    public void setExSpinnerValue1(String text){
       if(exArray1[0] == null){
           exArray1[0] = text;
       }else if(exArray2[0] == null){
           exArray2[0] = text;
       }
    }

    int a1 = 1;
    int b1 = 1;

    // CHILD STRING VALUE SETTERS
    public void setEditTextValue1(String setSchemeValue, String parentSpinnerValue){

        if(exArray1[0].equals(parentSpinnerValue)){
            if(exArray1[a1] == null){
                exArray1[a1] = setSchemeValue;
                a1++;
            }
        }
        if(exArray2[0].equals(parentSpinnerValue)){
            if(exArray2[b1] == null){
                exArray2[b1] = setSchemeValue;
                b1++;
            }
        }
    }
}

编辑:好的,现在我正在尝试在setSchemes(嵌套的frag)中使用getChildFragmentManager,因为如果父级是活动的子级,我似乎可以调用getParentFragment。这就是我现在所拥有的,我很确定我的语法都搞砸了:

setSchemes.java

@Override
public void onPause(){
    super.onPause();

    // Here I'm trying to assign a name to the Child Fragment Manager
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();

    // And here I'm trying to use that to call my getSpinner method
    // But "getSpinnerValue()" is red in the IDE and won't compile
    String parentSpinnerValue = fragmentTransaction.getSpinnerValue();


    EditText setsEditText = (EditText) getView().findViewById(R.id.sets);
    EditText repsEditText = (EditText) getView().findViewById(R.id.reps);
    EditText weightEditText = (EditText) getView().findViewById(R.id.weight);


    String value = setsEditText.getText().toString() + " x " + repsEditText.getText().toString() + " @ " +
            weightEditText.getText().toString();


    ControllerData.getInstance().setEditTextValue1(value, parentSpinnerValue);

}

所以,我“在”嵌套片段中。此嵌套片段是在从父片段单击按钮时创建的。根据用户单击按钮的次数,可以创建许多实例。父母本身也是动态添加的。它们之间的差异是选择的微调值。然后,我将这些嵌套片段的值传递给对象。我需要嵌套的片段“知道”它们“属于”哪个父片段,我试图这样做的方法是调用父节点的微调器值,然后用嵌套片段自己的值传递该值。这样我就可以通过编程方式确定将嵌套值传递给哪个数组。

这是父片段,movingSetsReps.java

// Here in onPause I send my spinner value to my ControllerData.
// I send the spinner value to an array at index [0]
// This is so that later I can compare the value in array[0]
//with the value passed with the nested fragment 
@Override
public void onPause() {
    super.onPause();

    Spinner exerciseSpinner = (Spinner) getView().findViewById(R.id.movementName);
    String spinnerText = exerciseSpinner.getSelectedItem().toString();

    ControllerData.getInstance().setExSpinnerValue1(spinnerText);

}


// Here is where I set up the method that should be called by the nested fragment
public String getSpinnerValue(){
    Spinner exerciseSpinner = (Spinner) getView().findViewById(R.id.movementName);
    String spinnerText = exerciseSpinner.getSelectedItem().toString();

    return spinnerText;
}

现在对于嵌套片段,setSchemes.java

@Override
public void onPause(){
    super.onPause();

    // I thought this would work but it's not. This should call
    // the method in whatever is the parent fragment
    String parentSpinnerValue = ((movementSetsRepsFrag) getParentFragment()).getSpinnerValue();


    // Everything below here pertains to the sending of a string to ControllerData's arrays. 
    EditText setsEditText = (EditText) getView().findViewById(R.id.sets);
    EditText repsEditText = (EditText) getView().findViewById(R.id.reps);
    EditText weightEditText = (EditText) getView().findViewById(R.id.weight);

    String value = setsEditText.getText().toString() + " x " + repsEditText.getText().toString() + " @ " +
            weightEditText.getText().toString();

    ControllerData.getInstance().setEditTextValue1(value, parentSpinnerValue);

}

现在在我的results.java中我有一些日志方法告诉我我的数组最终包含什么。我每次都得到null。

Log.d("array1", ControllerData.getInstance().exArray1[0] + " " + ControllerData.getInstance().exArray1[1]);
Log.d("array2", ControllerData.getInstance().exArray2[0] + " " + ControllerData.getInstance().exArray2[1]);

我的输出看起来像:“array1 null null”而不是,例如“bench press 2x15 @ 135 3x10 @ 115”

0 个答案:

没有答案