繁体   English   中英

从ViewPager中的片段读取值

[英]Reading values from Fragments in ViewPager

我的viewpager有三个页面,每个页面都是一个fragment ,每个页面中都有一些EditText ,第三页中有一个名为SAVEButton ,现在在此按钮单击事件中,我必须具有所有EditText的值'秒。 我已经尝试了很多方法,但是没有任何效果,总是出现NullPinterException 任何帮助将是非常可贵的。

谢谢,古娜

我当前的应用程序中的设置非常相似。 我所做的是创建一个具有方法的Fragment子类:

public abstract String[] getForm();

getForm方法本质上返回一个String [],其中包含每种形式存储的字符串。 每个分片必须正确实现。 现在,有了它,在包含ViewPager的Activity中,初始化活动ViewViewrAdapter应该用于显示的片段列表。 这样,现在当您进入最后一个片段并单击此按钮(并且包含按钮单击的片段成功通知活动该按钮单击事件已发生)时,您的活动将知道遍历整个片段列表,分别调用片段的getForm方法实现。

请注意,这仅在不使用ViewStatePagerAdapter的情况下有效。 这样做的原因是因为不能保证ViewStatePagerAdapter将所有片段都保留在内存中。

这是一个代码示例(在代码示例中,我将视图分页器存储在一个片段中,但是如果您将viewpager保留在活动中,则此设计绝对可以工作)。 实际的工作是在Submit方法中完成的。 那就是我们从另一个片段中收集字段的地方(因此应在您的OnButtonClickListener代码中调用此方法):

public class CreateAccountFragment extends RestCallExecutingFragment implements ViewPager.OnPageChangeListener {
    private OnAccountCreationListener onAccountCreationListener;
    public static final int VARIOUS_FRAG_POS = 2;
    public static final int ACCOUNT_INFO_FRAG_POS = 0;
    private static final int ADDRESS_FRAG_POS = 1;
    public static final int CREATE_ACCOUNT_ID = 0;

    public CreateAccountFragment() {
        // Required empty public constructor
    }

    ArrayList<FormFragment> fragmentsToDisplay;

  /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v13.app.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.v13.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link android.support.v4.view.ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @InjectView(R.id.rb_accountInfo)
    RadioButton rb_accountInfo;

    @InjectView(R.id.rb_address)
    RadioButton rb_address;

    @InjectView(R.id.rb_various)
    RadioButton rb_various;

    @InjectView(R.id.rg_createAccount)
    RadioGroup rg_createAccount;

    @InjectView(R.id.tv_pageTitle)
    TextView tv_pageTitle;

    List<RadioButton> radioButtons;

    CreateAccountCommand createAccountCommand;

    private static ArrayList<FormFragment> getCreateAccountFragments(){
        ArrayList<FormFragment> list = new ArrayList<>();
        list.add(AccountInfoFragment.newInstance());
        list.add(AddressFragment.newInstance());
        list.add(VariousFragment.newInstance());
        return list;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.frag_create_account, container, false);
        ButterKnife.inject(this, view);
        fragmentsToDisplay = getCreateAccountFragments();
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
        //todo make it easier to press the radio button
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) view.findViewById(R.id.pager);
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        return view;
    }

    public void submit() {
        //todo move the create account button to this activity's view
        AccountSubmissionRDTO createAccountSubmissionDTO;
        try {
            AccountInfoData accountInfoData =  (AccountInfoData) fragmentsToDisplay.get(ACCOUNT_INFO_FRAG_POS).submitForm();
            AddressData addressData = (AddressData) fragmentsToDisplay.get(ADDRESS_FRAG_POS).submitForm();
            VariousData variousData = (VariousData) fragmentsToDisplay.get(VARIOUS_FRAG_POS).submitForm();
          //  createAccountSubmissionDTO = new CreateAccountSubmissionRDTO(CREATE_ACCOUNT_ID,0, -1, accountInfoData,addressData,variousData); //todo create actual server and local ids

    }

    /**
     * A {@link android.support.v13.app.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) {
            FormFragment selectedFragment = fragmentsToDisplay.get(position);

            Assert.assertNotNull("the fragment selected should be within list", selectedFragment);
            return selectedFragment;
        }

        @Override
        public int getCount() {
            return fragmentsToDisplay.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            String pageTitle = fragmentsToDisplay.get(position).getPageTitle();
            return pageTitle.toUpperCase(l);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM