我addActivity托管了5个片段,在每个片段上我都需要填写一些字段。 我想从中获取这些字段的值addActivity。我尝试了类似的东西: AddActivity.java
addActivity
public class AddActivity extends FragmentActivity { ViewPager vp; ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); vp = (ViewPager) findViewById(R.id.pager); PagerAdapter pa = new FragPagerAdapter(getSupportFragmentManager()); vp.setAdapter(pa);} public class getValueOfFields() { //here I'm trying to get the value of differents fields in the different fragments but I get always "editText == Null" If the editText is in The fragment 1 or 2 or 3 it will get null EditText editTextFromFrag1 = (EditText) findViewById(R.id.visitDateFiled); // this == null // If the EditText is in Fragment 4 or 5 it will get the value normaly without any issue ...}
FragPagerAdapter.java
public class FragPagerAdapter extends FragmentPagerAdapter { public FragPagerAdapter(FragmentManager fm) { super(fm);} @Override public Fragment getItem(int index) { switch (index) { case 0: return new FirstFragment(); case 1: return new SecondFragment(); case 2: return new HousingFragment(); case 3: return new FeedingFragment(); case 4: return new OtherFragment(); } return null; } @Override public int getCount() { return 5; }
OtherFragment.java 公共类OtherFragment扩展Fragment {Button btn;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_other, container, false); btn = (Button) view.findViewById(R.id.send); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Call the getValueOfFields (After filling the differnts fields the user click the send button) ((AddActivity) getActivity()).getValueOfFields(); } }); return view; } }
更新 的问题我总是得到EditText(而其他字段要获得)的问题等于null如果它们位于片段1或2或3中。否则我得到了正确的值
您好Chlebta您需要设置pager.setOffscreenPageLimit(5);。在片段之间滑动时,片段可能会被破坏。这就是为什么你得到空的原因。
pager.setOffscreenPageLimit(5);
另外,一个简单的解决方案是您设置
pager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // Now here you get your fragment object. // To get the Fragment Object you need to store them in an ArrayList in your FragPagerAdapter class. // create a method `getMyFraggment ` which return your Fragment from Arraylist. // Now create a method in your each Fragment which return your EditText values. Here you can use HashMap to store the values. // Here you use switch cases to get your Fragment Object I just wrote 1 here MyFragment f = ((FragPagerAdapter)pager.getAdapter()).getMyFraggment(arg0); HashMap <String, String> hashMap = f.getValuesFromFragment(); myValues.putAll(hashMap); // myValues is global hashmap in your Activity. // Now you get All values in your Global HashMap. But remember here you can get duplicate values if user swipe a fragment more than 1 time. You should do a logic here to remove duplicate values. } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } });