gpt4 book ai didi

java - 从父 Activity 中获取 fragment 中不同字段的值

转载 作者:行者123 更新时间:2023-11-30 02:34:33 25 4
gpt4 key购买 nike

我有一个 addActivity 托管 5 个 fragment ,在每个 fragment 上我有一些字段要填充。
我想从 addActivity< 中获取这些字段的值。我试过类似的东西:
AddActivity.java

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; }

其他 fragment .java公共(public)类 OtherFragment 扩展 fragment { 按钮 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;
}
}

已更新
如果它们位于 fragment 1 或 2 或 3 中,我得到的问题总是 EditText(以及要获取的其他字段)等于 null。否则我会得到正确的值

最佳答案

你好 Chlebta 你需要设置 pager.setOffscreenPageLimit(5);。当您在 fragment 之间滑动时, fragment 可能会被破坏。这就是为什么你得到空值。

或者一个简单的解决方案是你设置

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

}
});

关于java - 从父 Activity 中获取 fragment 中不同字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26787650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com