我在1个活动中有4个片段(MainActivity:ViewPager)
fragment1 = edittext1,fragment2 = edittext2,fragment3 = edittext3& fragment4 = buttonsave
片段4中的只有一个保存按钮,用于从片段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 dan 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();
}
空问题
text1(片段)中的数据始终为null:
我该怎么做才能解决这个问题?
非常感谢...答案 0 :(得分:0)
您必须使用共享首选项来存储FragmentOne数据,FragmentTwo数据,然后在保存按钮上检索到FragmentTree
答案 1 :(得分:0)
您可以使用参数在片段之间传递数据,例如
$("#free").val("y").change();
并检索所需片段中的数据
Bundle bundle = new Bundle();
bundle.putString("someEditTextData",someEditTextData);
SomeFragment fragment = new SomeFragment ();
fragment.setArguments(bundle);
答案 2 :(得分:0)
您可以为此目的使用共享偏好
创建SharedPreferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("DATA", MODE_PRIVATE);
Editor editor = pref.edit();
editor.putString("EDITEXT1", "string value"); // Saving string data of your editext
editor.commit(); // commit and save your data in to SharedPreferences
现在检索您的数据,如下面的代码
SharedPreferences pref = getApplicationContext().getSharedPreferences("DATA", MODE_PRIVATE);
String email=pref.getString("EDITEXT1", ""); // Saving string data of your editext
答案 3 :(得分:0)
您可以在活动中定义一个实例字段,并可以通过向片段中的MainActivity引用getActivity()引用来访问它。
((MainActivity) getActivity()).filedName.setFragmentOneData(data);
答案 4 :(得分:-1)
如果你想访问Fragment 1 editText数据,那么viewPager总是加载两面连续的1页,然后添加这一行
viewPager.setOffscreenPageLimit(4);
或 使用静态变量