Скользящие вкладки: как передать переменные в несколько действий вкладок
У меня есть MainActivity, которая вычисляет 3 числовых результата (двойные A, B, C) и 3 числовых результата (двойные D, E, F) при нажатии кнопки "вычислить". Затем я создаю слайд-вкладку с двумя вкладками для отображения результатов. То, что я пытаюсь сделать, это передать переменную (A, B, C) для печати в TextView Tab-1; и передайте переменные (D, E, F) для печати в TextView из Tab-2.
Где я могу разместить код, показанный ниже, в MainActivity, чтобы передать переменные в Tab-1 и Tab-2? Может кто-нибудь помочь или, возможно, я использовал неправильный метод передачи переменных? Спасибо за помощь.
Intent intent1 = new Intent(this, form_tab1.class );
intent1.putExtra(EX_A, A);
intent1.putExtra(EX_B, B);
intent1.putExtra(EX_C, C);
Intent intent2 = new Intent(this, form_tab2.class );
intent2.putExtra(EX_D, D);
intent2.putExtra(EX_E, E);
intent2.putExtra(EX_F, F);
Вот код MainActivity Java:
package com.abc.www.apps;
import ...
public class MainActivity extends AppCompatActivity {
public static final String EX_A = "com.abc.www.apps.EX_A";
public static final String EX_F = "com.abc.www.apps.EX_B";
public static final String EX_C = "com.abc.www.apps.EX_C";
public static final String EX_D = "com.abc.www.apps.EX_D";
public static final String EX_E = "com.abc.www.apps.EX_E";
public static final String EX_F = "com.abc.www.apps.EX_F";
double ax, bx, cx, dx;
double A, B, C;
double D, E, F;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ax = 2;
bx = 3;
cx = 4;
dx = 5
}
//Compute button is pressed
public void compute(View view)
{
A = ax+bx;
B = ax+cx;
C = ax+dx;
D = bx+cx;
E = bx+dx;
F = cx+dx;
//Open the Sliding Tab Activity
Intent intent = new Intent(this, SlideTabActivity.class );
startActivity(intent);
}
}
Вот код активности скользящих вкладок:
package com.abc.www.apps;
import ...
public class SlideTabActivity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
public TextView outTV;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slidetab_form);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1:
// do something
rootView = inflater.inflate(R.layout.form_tab1, container, false);
break;
case 2:
// load another page
rootView = inflater.inflate(R.layout.form_tab2, container, false);
break;
}
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}
}
1 ответ
send all variables to slidetabactivity and get all values there
public void compute(View view)
{
A = ax+bx;
B = ax+cx;
C = ax+dx;
D = bx+cx;
E = bx+dx;
F = cx+dx;
Intent intent1 = new Intent(this, SlideTabActivity.class );
intent1.putExtra(EX_A, A);
intent1.putExtra(EX_B, B);
intent1.putExtra(EX_C, C);
intent1.putExtra(EX_D, D);
intent1.putExtra(EX_E, E);
intent1.putExtra(EX_F, F);
//Open the Sliding Tab Activity
Intent intent = new Intent(this, SlideTabActivity.class );
startActivity(intent);
}
}
then create a two bundle variables in slidetabActivity
Bundle tab1=new Bundle();
tab1.putdouble(EX_A, A);
tab1.putdouble(EX_B, B);
tab1.putdouble(EX_C, C);
Bundle tab2=new Bundle();
tab2.putdouble(EX_D, D);
tab2.putdouble(EX_E, E);
tab2.putdouble(EX_F, F);
and pass different bundle to different tab according to your condition`enter code here`