Android在下一个选项卡中显示并保存数据
问题描述:
我已经创建了一个包含2个选项卡的FragmentActivity
。这是Android开发者教程使用FragmentActivity
和FragmentPagerAdapter
Code HereAndroid在下一个选项卡中显示并保存数据
public class FragmentPagerSupport extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
static final int NUM_ITEMS = 10;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
...........
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case 0:
fragment = new Fragment01();
args.putInt(Fragment01.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
break;
case 1:
fragment = new Fragment02();
args.putInt(Fragment02.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
break;
return fragment;
}
}
然后一个非常基本的Tab
例子中,我创建了2个不同的:
public static class Fragment01 extends Fragment {
private EditText mName;
private EditText mEmail1;
public static final String ARG_SECTION_NUMBER = "2";
public Fragment01() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_customer_add, container, false);
mName = (EditText) v.findViewById(R.id.customer_add_name);
mEmail = (EditText) v.findViewById(R.id.customer_add_email);
View confirmButton = v.findViewById(R.id.customer_add_button);
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
......................
}
});
}
}
现在,在第二个选项卡,我需要证明从第一个选项卡输入值Name
和Email
,并从第二个选项卡确认需要保存在database
。但在这里我卡住了。我不知道我如何保留first tab
的数据?请帮忙。
答
一种方法可以是使用Singleton DP。只有一个对象具有姓名,电子邮件等字段。在第一个片段中使用setter设置Name和Email字段,并在第二个片段中使用getter访问它们。
确认后,您可以将整个对象插入数据库。
可以使用SharedPreference保存数值也很容易 http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html 检查此链接 – Syn3sthete 2013-02-27 04:33:59
看一看这个:http://developer.android.com/training/basics/fragments/communicating.html – nedaRM 2013-02-27 04:40:02