Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Sending a value from one fragment to another fragment.

In FirstFragment I have an on click listener that does an equation and sends a value to an EditText within FirstFragment. I want to get that value in a EditText in SecondFragment under the same activity (but each fragment has their own .Java).

This is activity_testing.xml.

'''

android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dfsdfd.MainActivity" >

'''

This is the Testing.Java ''' public class Testing extends FragmentActivity {

public void saveData(int id, Bundle data) {
    // based on the id you'll know which fragment is trying to save data(see
    // below)
    // the Bundle will hold the data
}

public Bundle getSavedData() {
    return null;
    // here you'll save the data previously retrieved from the fragments and
    // return it in a Bundle

}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing);

    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}

private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {

        super(fm);
    }


    public Fragment getItem(int pos) {
        switch (pos) {

        case 0:
            return FirstFragment.newInstance("FirstFragment, Instance 1");
        case 1:
            return SecondFragment.newInstance("SecondFragment, Instance 1");

        default:
            return ThirdFragment.newInstance("ThirdFragment, Default");

        }
    }


    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        return "Step " + (position + 1);
    }
}

'''

FirstFragment ''' public class FirstFragment extends Fragment implements OnClickListener {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.first_frag, container, false); 

return v; }

public void onClick(View v) {
    // TODO Auto-generated method stub

if (US.isChecked()) {

                        double personWeight = Double.valueOf(Weight
                                .getText().toString());
                        double personFeet = 12 * Double.valueOf(Feet
                                .getText().toString());
                        double personInches = Double.valueOf(Inches
                                .getText().toString());
                        double personAge = Double.valueOf(Age.getText()
                                .toString());

                        if (Men.isChecked()) {

                            double personResults = (66
                                    + (6.23 * personWeight)
                                    + (12.7 * (personFeet + personInches)) - (6.8 * personAge));

                            results.setText("" + d.format(personResults));

                        }
                        if (Women.isChecked()) {

                            double personResults = (655
                                    + (4.35 * personWeight)
                                    + (4.7 * (personFeet + personInches)) - (4.7 * personAge));
                            results.setText("" + d.format(personResults));

                        }

                    }

'''

I would want, results = (EditText) v.findViewById(R.id.editTextFinish); to be the value in which I am trying send to the SecondFragment.

SecondFragment.Java.

''' public class SecondFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.second_frag, container, false);

    return v;
}

public static SecondFragment newInstance(String text) {

    SecondFragment f = new SecondFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}

} '''

The second_frag.xml ''' <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/topbackgroundboom" android:orientation="vertical" >

    <EditText
        android:id="@+id/editTextFinish"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="false"
        android:cursorVisible="false"
        android:ems="10"
        android:gravity="center"
        android:hint="BMR"
        android:inputType="none"
        android:textColor="@android:color/black" />

</LinearLayout>

'''

If there is any advice other than a reference to the android development page; or to an online tutorial. I have tried them all and even made it to page 3 on google. That is pretty desperate.

Thanks in advance!