と入力データ断片から値を取得する方法fragment4でiは4断片1で活性を有する(MainActivity:ViewPager)をviewpager
fragment1 = edittext1、fragment2 = edittext2、fragment3 = edittext3 & fragment4 = buttonsave
そこ断片1,2および3 からデータを取得するための唯一の保存ボタンがあるが、私はbuttonsaveをクリックすると、fragment1からのデータは常にnull
MainActivityです:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "FRAG1");
adapter.addFragment(new FragmentTwo(), "FRAG2");
adapter.addFragment(new FragmentThree(), "FRAG3");
adapter.addFragment(new FragmentFour(), "FRAG4");
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
フラグメント1,2-ダン3
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
Fragment4
public class FragmentFour extends Fragment {
private EditText txt1, txt2, txt3;
private Button button;
public FragmentFour() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_four, container, false);
txt1 = (EditText)getActivity().findViewById(R.id.txt1);
txt2 = (EditText)getActivity().findViewById(R.id.txt2);
txt3 = (EditText)getActivity().findViewById(R.id.txt3);
button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getData();
}
});
return view;
}
private void getData() {
String fragment1 = txt1.getText().toString();
String fragment2 = txt2.getText().toString();
String fragment3 = txt3.getText().toString();
}
ヌル通報
テキスト1からのデータ常にnull(フラグメント):
私はこの問題を解決するために何をすべき?その後、
多くのおかげで...
スタティック変数 –
フラグメント2のデータはどうですか? – AbhayBohra