1

Edit 4: Here will be my most up to date code, I'll erase and update here as I go.

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

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

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

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

Edit 3:

In setSchemes.java I removed this bit of code:

    // 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();

And I'm now trying something I was suggested on another forum, a callback technique someone was showing me (as a way to getParentFragment):

in setSchemes.java

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

    private setSchemesCallback callback;

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

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

Edit 2:

Here is the object that I use to collect and organize my data, 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++;
            }
        }
    }
}

Edit: Alright, so now I'm trying to use getChildFragmentManager in setSchemes(the nested frag), because it seems I can call getParentFragment if the parent is a child of an activity. This is what I have now, I'm pretty sure my syntax is all messed up though:

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);

}

So, I'm "in" a nested fragment. This nested fragment is created on a button click from a parent fragment. Many instances can be created, as many as the user wants based on how many times they click the button. The parents themselves are added dynamically as well. The differences between them are the spinner values selected. I then pass those nested fragments' values into an object. I need the nested fragments to "know" which parent fragment they "belong" to, and the way I'm trying to do that is call for the parent's spinner value and then pass that value with the nested fragment's own value. This way I can determine programmatically which array to pass the nested values to.

This is the parent fragment, movementSetsReps.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;
}

Now for the nested fragment, 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);

}

Now in my results.java I have some log methods that tell me what my arrays end up containing. I get null every time.

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

My output looks like: "array1 null null" instead of, for example, "bench press 2x15@135 3x10@115"

therealone
  • 421
  • 10
  • 22
  • Is your parent fragment attached to an Activity? – Dave May 09 '16 at 00:14
  • Yes it is, if you need the code I can put it up – therealone May 09 '16 at 00:20
  • With respect to your comment is setSchemes.java, your call to getParentFragment could be returning null because of that. Check this link out first: http://stackoverflow.com/questions/14804526/getparentfragment-returning-null – Dave May 09 '16 at 00:21
  • Hm, so I'm now trying to use getChildFragmentManager as recommended in the link. I'm pretty sure I'm not implementing it correctly though. Could you take a look at my edit in the main post? I've posted some code that shows what I've changed. – therealone May 09 '16 at 01:25
  • @Dave what would you recommend as an alternative to getParentFragment()? – therealone May 10 '16 at 14:32
  • I apologize for not answering, I've been a bit busy the past few days...Could you explain what exactly you are trying to achieve with this code? (i.e. a brief overview) I'm not quite understanding from the original description. – Dave May 10 '16 at 19:55
  • I'm organizing child fragment's values based on what spinner value their parent has. I'm able to pass the values in the child fragments, and I'm able to pass the values in the parent fragments. I pass these values into arrays inside an object. Each parent sends its spinner value to an according array at index [0]. When I gather the children values, they need to pass their value (1) and their parent's spinner value(2). The object takes that second value and compares it to all arrays' index[0]. If there is a match, it puts that child value in that array. If Im still not making sense, let me know – therealone May 11 '16 at 03:28
  • Can you post your ControllerData.java? Also, I suggest you try printing out the value of your "text" variable in setExSpinnerValue1 and make sure that that isn't null. – Dave May 11 '16 at 03:44
  • Sure thing, it's edited into the OP. – therealone May 11 '16 at 03:48
  • Let me know what the "text" variable prints out to in the setExSpinnerValue1 method. I want to be sure that it's not setting that value to null. – Dave May 11 '16 at 03:56
  • So I'm trying to do this inside of setExSpinnerValue1 like this: Log.d("spinner1", text) but nothing is showing up in logcat when I filter for "spinner1." I'm not sure if that means anything, so I'm just posting this as an update. I'll keep trying to get it to print out – therealone May 11 '16 at 04:04
  • Oh yes, by the way I had changed something in setSchemes(the child) since I originally posted. I'm not sure if that changes the situation at all, but I'll post another edit with the changed code just to be sure – therealone May 11 '16 at 04:06
  • It might not be reaching that point, i.e. the method isn't being called. Make sure that onPause is being called. You could try debugging it and setting a break point in onPause. – Dave May 11 '16 at 04:08
  • Sounds good, keep me updated on the changes. – Dave May 11 '16 at 04:08
  • Alright, updated code is in the OP. I'll try the debugging thing. As you can probably tell, I'm pretty new to all this. But I'll try to get that going – therealone May 11 '16 at 04:12
  • It's all good, we all have to start somewhere. We'll get this sorted out for you soon enough. – Dave May 11 '16 at 05:21
  • BTW, please excuse me not answering/updating for a day or two, very close to being done with finals. – therealone May 11 '16 at 23:10
  • Alright, done with finals. Still not getting anywhere with this though. – therealone May 14 '16 at 01:20
  • I hope they went well. Did you test out to see if onPause is getting called? – Dave May 14 '16 at 01:26
  • Alright, so I set a breakpoint in onPause but I'm not entirely sure how to interpret it. This is what happens: http://i.imgur.com/2t83KTy.png – therealone May 14 '16 at 01:32
  • That value of callback you see to the right of that line is that variables value at the moment this line is called. You need to use the controls (the ones with the arrows) to the right of the tab that says "Logcat" to step through the program. I suggest you read up on this, it's pretty simple and will help you a lot. What you want to do is use the "step over" (or use F8) button to move to the next line until you reach the one that calls your ControllerData method and the use "step into". – Dave May 14 '16 at 01:41
  • Also, looking at the error output, I'm getting a "caused by trying to perform a .equals on a null" inside of the controller object. What's being compared there is String parentSpinnerValue which is being passed by setSchemes.java method in onPause setEditText1. That just confirms to me that I'm still getting null when I try to call my parent fragment.. Edit: Ok, I'll try that/read up on it – therealone May 14 '16 at 01:41
  • Can you post the error output you're getting? – Dave May 14 '16 at 01:43
  • Wasn't sure how much you wanted, so here's all of it: https://codeshare.io/4cVUI – therealone May 14 '16 at 01:55
  • This is very interesting, I was doing the "step over" thing you were saying, and when I got to the line where I call ControllerData and do .setEditTextValue1(value, parentSpinnerValue) it actually does have a value! "Bench Press" just like it should. Maybe this means that I am successfully calling the parent fragment and getting the parentSpinnerValue, but it's somehow becoming null on its way to ControllerData? – therealone May 14 '16 at 01:55
  • Yeah, this is odd. I set a break point in ControllerData.java and when setEditTextValue1 is called, String parentSpinnerValue does indeed have the right string value in it. So, even though it crashes and error outputs that .equals is being performed on a null, in this debug mode it says that there's a string there! So I don't understand why it's erroring out when there is a string value there.. – therealone May 14 '16 at 02:00
  • Keep running it on debug. Once you reach the line that calls the ControllerData method use the "step into" button. This should take you into the setEditTextValue1 method. In the debug tab you'll see a list of all the variables that you can look at in that moment. Here you should see the value for the two variables you passed in (setSchemeValue and parentSpinnerValue). You can also see the contents of your two arrays.edit: haha you beat me to it – Dave May 14 '16 at 02:01
  • Remember that you are calling the equals method on the value in your array, not the parentSpinnerValue. This is why you are getting a NullPointerReference. If you flipped them and made your code do this instead you would not get this error: if(parentSpinnerValue.equals(exArray1[0])){ – Dave May 14 '16 at 02:06
  • What this means is also that your exArray1[0] is null, so you haven't initialized that value. Since you call this in the parentFragment's onPause it could be that the parent's onPause hasnt been called. Which makes sense but I'm not too familiar with nested fragments. – Dave May 14 '16 at 02:08
  • Ok, this is unexpected. I set the break point in setSchemes in onPause when I call ControllerData.setEditTextValue1. When I stepped into it, I came into this part of ControllerData instead of into the actual setEditTextValue1 method in ControllerData. Being totally honest, I don't really know how this part of ControllerData works. When someone else was helping me set up ControllerData, they had this in there and I haven't messed with it since then. http://i.imgur.com/ClYL0yZ.png Edit: Ok, reading your other posts real quick – therealone May 14 '16 at 02:09
  • Don't worry that's normal. Remember that the first thing you did in that line was call getInstance in ControllerData. And because the code is executed from left to right it will enter that method first, then return with the instance and then call the method. So, all you have to do is "step out" of the getInstance method and when you're back in setSchemes just press "step into" again. – Dave May 14 '16 at 02:12
  • Also, with respect to your last comment, that method is returning an instance of ControllerData. I'm sure you noticed that it's a bit different from other classes you may have seen and that's because it follows a programming design pattern called "Singleton". Long story short, it makes sure that you only ever have 1 ControllerData object in our program. I suggest you read up on this too if you want to know more. – Dave May 14 '16 at 02:15
  • Is there any particular reason why you are calling these ControllerData methods in your Fragments' onPause events? – Dave May 14 '16 at 02:22
  • I tried the flipping of the method, and now there's no error or crash! Now what's happening is the results page is printing out only "Bench Press" and not the child value ie "2x3@5." I did some debugging, and in results.java, I took out the code to print everything in the array, but instead just print what's in [1], which should be the "2x3@5", but apparently in results.java, but after setting a break point there, it says [1] is null... – therealone May 14 '16 at 02:27
  • Well, I'm calling them in onPause because if I do it in onCreateView it doesn't ensure that there's any data in them yet, since the user can add/change the values after onCreate. I may be wrong about that, but that's how it was acting before I moved the methods to onPause – therealone May 14 '16 at 02:30
  • All we did was avoid the crash by telling Java "compare this object to nothing/null" rather than "compare this nothing/null to an object". You still have the problem that you have a null because you haven't set the array's value. – Dave May 14 '16 at 02:33
  • In what fragment does the data have to be present? – Dave May 14 '16 at 02:35
  • Results.java I'd initially think because that's where I display them. But in ControllerData's array-building methods rely on knowing what is in the arrays – therealone May 14 '16 at 02:40
  • The parent's value is available to you when you create the fragment so you can call the method to set that when you add the fragment, you don't need to do it in onPause. As for the child, you can do the same and then also call the setEditTextValue1 method whenever a user changes the value. Can you post the code where you add the nested fragments? – Dave May 14 '16 at 02:46
  • Sure, here it is: https://codeshare.io/6Yua1 Also, I'll mess around with putting the method calls in onCreate. I'm just worried that, keeping in mind how setEditText works, I won't be able to "know" which array value to change. For example, if I initially setEditText in onCreate, it'd be added to array[a1(a1 being [1])] (a1++), then the next time I called it, it would be at [2] – therealone May 14 '16 at 02:58
  • I'll be afk for a little while, will be back tomorrow. I'll experiment with all of your suggestions and write up the results – therealone May 14 '16 at 03:10
  • Whoaaa, it's kind of working! I moved my methods into onCreateView, but that kept resulting in crashes, so instead I removed the parent's (movementSetsRepsFrag) onPause method (setSpinnerValue), and instead had that get called in the child's (setSchemes) onPause, and now it's correctly working. It's bugging out if I add more than one child per parent, but it's actually working if I have one child per parent. I'll keep working with it – therealone May 14 '16 at 17:22
  • How exactly is it bugging out when you have more than one child? – Dave May 14 '16 at 17:31
  • Easier for me to cleanly explain in this format, so here you go: https://codeshare.io/MPYO4 – therealone May 14 '16 at 17:59
  • I'll keep commenting on the codeshare instead of here as well. Also, try to keep updating the code on here as you change it so that I can see what it looks like. – Dave May 14 '16 at 18:15
  • Up-to-date full versions of code added in OP – therealone May 14 '16 at 19:19

0 Answers0