【问题标题】:How to deal with multiple tabs, EditText and save to database?如何处理多个选项卡、EditText 并保存到数据库?
【发布时间】:2013-02-26 09:09:39
【问题描述】:

我正在创建一个现金销售应用程序,我将在其中实现 4 个选项卡。从第一个选项卡用户将从客户列表中选择客户,第二个选项卡从项目列表中选择项目,第三个选项卡在 7 EditText 中设置付款详细信息,第四个选项卡查看草稿并确认保存在 SQLite 中。我有几个问题:

  1. 对于标签,我应该先通过扩展FragmentActivity 来创建一个标签容器,如下所示:

    public class CashSales extends FragmentActivity {
    
        private FragmentTabHost mTabHost;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.cash_sales_tab);
            mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    
            mTabHost.addTab(mTabHost.newTabSpec("customer").setIndicator("customer"),
                    CustomerSelect.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("item").setIndicator("item"),
                    ItemSelect.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("payment"),
                    SetPayment.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("payment"),
                    DraftViewAndSave.class, null);
        }
    }
    
  2. 我应该为CustomerSelectItemSelect 等每个活动创建不同的类吗?如果需要创建不同的类,应该从Fragment 类或FragmentActivity 类扩展?

  3. 当用户从第一个选项卡转到第二个选项卡时,我如何记住数据?我应该使用 Session 并最终将数据从 session 保存到数据库吗?

伙计们,我是 Android 新手。请帮助我或发送任何示例链接。

【问题讨论】:

    标签: android sqlite android-fragments


    【解决方案1】:

    我已经实现了同样的事情,所以我分享我的想法。

    For tab, should I create a tab container first by extending FragmentActivity
    

    您应该创建一个扩展 FragmentActivity 的类。而是使用我使用ViewPager 和custome FragmentPagerAdapter 的选项卡,它们包含您的四个不同的Fragnment(在您的情况下为CustomerSelect、ItemSelect 等)并在滑动时您可以将数据保存在您的片段中的Bundle 中,然后调用从您的 FragmentActivity 中获取 public 方法以从您的 fragment 类中获取 Bundle 对象

    Should I create different classes for each of the activities like CustomerSelect, ItemSelect etc?

    是的,你应该这样做。

    How can I memorize the data when user will go from 1st tab to 2nd tab? Should I use Session and finally save data from session to database?

    如上所述,您可以将数据保存在 bundle 对象中,然后在 FragmentActivity 中,您可以在 onPageSelected 方法中调用 save 方法,例如。

    private Bundle firstFragmentData;
    
      mPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                @Override
                public void onPageSelected(int arg0) {
    
                    switch (arg0) {
                    case 1:
    
                        fragment1 = (MyFirstFragment) getSupportFragmentManager()
                                .findFragmentByTag(
                                        "android:switcher:" + R.id.pager + ":"
                                                + (arg0 - 1));
                        firstFragmentData = fragment1.SaveDatainFragment1();
    
                        break;
    

    实际上,您需要为从主要 FragmentActivity 中的不同片段获取的所有捆绑对象设置getters,例如

    public Bundle getFirstFragmentData() {
         return firstFragmentData;  
     }
    

    现在在你的任何 Fragment 中,你可以得到任何这样的 Fragment 数据..

    Bundle firstFragmentData = ((MainFragmentActivity) getActivity())
                            .getFirstFragmentData(); // here you got the bundle 
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2013-01-06
      • 2013-03-21
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多