0

I'm going to be having multiple instances of the same fragments, and I'm trying to get values from within these fragments. The problem I'm having is that, because these are instances of the same fragment/s, the EditTexts/Spinners/Toggle Buttons naturally have the same id's if there are several instances of them. How would I go about getting values from them?

If I use something like

EditText exampleEditText = (EditText) findViewById(R.id.exampleId);

exampleEditText.getText().toString() 

I get the value of the first instance, and none of the others.

therealone
  • 421
  • 10
  • 22
  • how about put some method after commit your fragment... and store as shared preference... – Iqbal Rizky Apr 16 '16 at 18:38
  • its simple.. use bundle to store each fragment here is your solution,http://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment – Sagar Chavada Apr 16 '16 at 18:51

2 Answers2

0

Just to make sense how to get data from multiple instances of same fragment.

To simulate your example. You,

  • Have a SampleFragment
  • Have three instances of SampleFragment
  • Get value of EditText from an instance

Sample fragment

public class SampleFragment extends Fragment {

    private EditText mSampleEditText;

    public String getSampleValue(){
        String value;
        if(mSampleEditText != null){
            value = mSampleEditText.getText().toString();
        }
        return value;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSampleEditText= (EditText) getActivity().findViewById(R.id.editText_sample);
    }
}

Sample activity

public class SampleActivity extends AppCompatActivity {

    String mInstance1Tag = "instance1";
    String mInstance2Tag = "instance2";
    String mInstance3Tag = "instance3";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        // First instance
        transaction.replace(R.id.fragment_container1, new SampleFragment(), mInstance1Tag);

        // Second instance
        transaction.replace(R.id.fragment_container2, new SampleFragment(), mInstance2Tag);

        // Third instance
        transaction.replace(R.id.fragment_container3, new SampleFragment(), mInstance3Tag);
        transaction.commit();
    }

    private String getValueFromInstance(String instanceTag) {
        SampleFragment fragment = (SampleFragment) getSupportFragmentManager()
                    .findFragmentByTag(instanceTag);
        return fragment.getSampleValue();
    }
}

EDIT

After your comment, the question is more clear and also you are right on the questions about tags like 'what would", "where you have".

I'm going to be having multiple instances of the same fragments,

Before start my post, i had supposed you'll have specified fragment instance count. So i set instance count to 3 in my example and then paired instances and tags. Because if you add fragment instances via unique tags, then you can get them.

And dont get stuck on their tags, to make simple i defined them as "instance1", "instance2"...

blackkara
  • 4,900
  • 4
  • 28
  • 58
  • Hm, what would instanceTag be in getValueFromInstance? For example, where you have mInstance1Tag, I have a programmatically added number (every time an instance is created, it increments by one and converts to a string). How would I find that id with getValueFromInstance? And if it matters, I'm trying to call getValueFromInstance from within a button listener in my main activity. The instances that I'm trying to get are nested 3 fragments deep inside this main activity. – therealone Apr 17 '16 at 01:06
  • OK, that edit helped a lot. So now I've got it set up like you did in the example. How do I go about calling this? What am I passing into the method? I was wondering about (String instanceTag). Would I just do it like this?: getValueFromInstance(tag1)? Because one of my main problems is I don't have a clue on how to get the tag of the particular instance. I can't pass a string tag into the method if I don't know my tags. How can I do this from my main activity? Or, how would you go about it? Because I'm still pretty lost. – therealone Apr 17 '16 at 02:32
0

it depends on for how long you need the data to be there, i mean if u need it even after the app restarts, the best approach would be to create a database and second approach would be TinyDB.

Hope it helps

Atiq
  • 14,435
  • 6
  • 54
  • 69
  • Well, that's my next step. I really just can't figure out right now how I'm going to get the values from all these different instances. I feel like it shouldn't be this hard. I just have a save button and when clicked it should grab whatever values are in the various edit texts. Once I figure that out I can look into putting those values in a database. – therealone Apr 17 '16 at 01:55