Android 从ViewPager中的片段读取值

Android 从ViewPager中的片段读取值,android,android-fragments,Android,Android Fragments,我的viewpager中有三个页面,每个页面都是一个片段,每个页面中都有一些EditText,我的第三个页面有一个名为保存的按钮,现在在这个按钮点击事件中,我必须从所有EditText中获取值。我尝试了很多方法,但都没有成功,我总是得到NullPinterException。任何帮助都是非常值得的 谢谢, Guna。我在当前的应用程序中有一个非常类似的设置。我所做的是创建一个Fragment的子类,它具有以下方法: public abstract String[] getForm(); get

我的
viewpager
中有三个页面,每个页面都是一个
片段,每个页面中都有一些
EditText
,我的第三个页面有一个名为保存
按钮,现在在这个按钮点击事件中,我必须从所有
EditText
中获取值。我尝试了很多方法,但都没有成功,我总是得到
NullPinterException
。任何帮助都是非常值得的

谢谢,
Guna。

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

public abstract String[] getForm();
getForm方法本质上返回一个字符串[],其中包含存储在每个表单中的字符串。每个片段都必须正确地实现这一点。现在,在包含ViewPager的活动中,初始化活动的ViewPagerAdapter应用于显示的片段列表。这样,现在当您在最后一个片段中并单击此按钮时(并且包含按钮单击的片段成功地通知活动发生了按钮单击事件),您的活动将知道迭代整个片段列表,调用片段各自的getForm方法实现

请注意,这仅在不使用ViewStatePagerAdapter时有效。这是因为ViewStatePagerAdapter不能保证将所有片段都保存在内存中

下面是一个代码示例(在代码示例中,我将我的viewpager存储在一个片段中,但如果您将viewpager保留在活动中,则此设计肯定会起作用)。真正的工作是在submit方法中完成的。这就是我们从另一个片段收集字段的地方(因此应该在OnButtonClickListener代码中调用此方法):

public类CreateAccountFragment扩展RestCallExecutingFragment实现ViewPager.OnPageChangeListener{
私有OnAccountCreationListener OnAccountCreationListener;
公共静态最终int各框架位置=2;
公共静态最终整数账户信息框架位置=0;
专用静态最终整数地址\u FRAG\u POS=1;
公共静态final int CREATE_ACCOUNT_ID=0;
公共CreateAccountFragment(){
//必需的空公共构造函数
}
阵列列表碎片显示;
/**
*将提供的{@link android.support.v4.view.PagerAdapter}
*每个部分的片段。我们使用
*{@link android.support.v13.app.FragmentPagerAdapter}派生,它将保留
*已在内存中加载片段。如果这变得过于内存密集,则
*最好是换成一个
*{@link android.support.v13.app.FragmentStatePagerAdapter}。
*/
分段SPAGERADAPTER mSectionsPagerAdapter;
/**
*将承载节内容的{@link android.support.v4.view.ViewPager}。
*/
ViewPager mViewPager;
@InjectView(R.id.rb_accountInfo)
RadioButton rb_accountInfo;
@InjectView(R.id.rb_地址)
无线电按钮rb_地址;
@InjectView(R.id.rb_各种)
无线电按钮rb_各种;
@InjectView(R.id.rg_createAccount)
放射组rg_CreateCount;
@InjectView(R.id.tv_页面标题)
TextView tv_页面标题;
列出单选按钮;
CreateAccountCommand CreateAccountCommand;
私有静态ArrayList getCreateAccountFragments(){
ArrayList=新建ArrayList();
add(AccountInfoFragment.newInstance());
add(AddressFragment.newInstance());
add(VariousFragment.newInstance());
退货清单;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.frag\u创建账户,容器,false);
注入(这个,视图);
fragmentsToDisplay=getCreateAccountFragments();
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//活动的主要部分。
mSectionsPagerAdapter=新的SectionsPagerAdapter(getChildFragmentManager());
//todo使按下单选按钮更容易
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)view.findViewById(R.id.pager);
mViewPager.setOnPageChangeListener(此);
mViewPager.setAdapter(mSectionsPagerAdapter);
返回视图;
}
公开作废提交(){
//todo将“创建帐户”按钮移动到此活动的视图
AccountSubmissionrTo创建AccountSubmissiondTo;
试一试{
AccountInfoData AccountInfoData=(AccountInfoData)fragmentsToDisplay.get(ACCOUNT_INFO_FRAG_POS).submitForm();
AddressData AddressData=(AddressData)fragmentsToDisplay.get(ADDRESS_FRAG_POS.submitForm();
VariousData VariousData=(VariousData)fragmentsToDisplay.get(variable_FRAG_POS.submitForm();
//createAccountSubmissionDTO=新建CreateAccountSubmissionRDTO(创建帐户ID,0,-1,accountInfoData,addressData,variousData);//创建实际服务器和本地ID
}
/**
*一个{@link android.support.v13.app.FragmentPagerAdapter},返回对应于
*其中一个部分/选项卡/页面。
*/
公共类节SpagerAdapter扩展了FragmentPagerAdapter{
公共部分SpagerAdapter(碎片管理器fm){
超级(fm);
}
@凌驾
公共片段getItem(int位置){
FormFragment selectedFragment=fragmentsToDisplay.get(位置);
Assert.assertNotNull(“所选片段应在列表中”,selectedFragment);
返回selectedFragment;
}
@凌驾
public int getCount(){
返回fragmentsToDisplay.size();
}
@凌驾
公共字符序列getPageTitle(int posi
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);
        }
    }
}