将捆绑数据传递到以viewPager开头的片段

时间:2014-06-20 19:12:21

标签: android android-viewpager fragmentpageradapter

在Eclipse v22.6.2中,一个名为Act1的Activity以意图启动 一些数据使用putExtras作为包传递。

Intent intent = new Intent(MyMain.this, Act1.class);
Bundle bundle = new Bundle();               
bundle.putString("id", _productid);
bundle.putString("name", _productname);
bundle.putString("price", _productprice);
bundle.putString("t1", _t1);
intent.putExtras(bundle);
startActivity(intent);

在Act1中,一些textView用来自bundle

中传递的参数的数据填充
txtpid = (TextView) getView().findViewById(R.id.txtdisplaypid);
txtpname = (TextView) getView().findViewById(R.id.txtdisplaypname);
txtpprice = (TextView) getView().findViewById(R.id.txtdisplaypprice);
txtt1 = (TextView) getView().findViewById(R.id.txtdisplayt1);
txtpid.setText(getArguments().getString("id"));
txtpname.setText(getArguments().getString("name"));
txtpprice.setText(getArguments().getString("price"));
txtt1.setText(getArguments().getString("t1"));

在Act1中,使用FragmentPagerAdapter的ViewPager用于启动名为Frag1的片段。没有使用意图。

我的两个问题:
如何将捆绑的数据传递给Frag1?
一旦Frag1拥有数据,它是如何访问的?

示例代码将不胜感激。

1 个答案:

答案 0 :(得分:0)

原来这很容易。 形成一个捆绑包以发送调用应用程序Act1

           public Bundle getDetailBundle(){

              // define variables and populate
              t1 = (i.getExtras().getString("t1"));
              t2 = (i.getExtras().getString("t2"));
              t3 = (i.getExtras().getString("t3"));
              t4 = (i.getExtras().getString("t4"));

              Bundle bundle = new Bundle();

              bundle.putString("t1", t1);
              bundle.putString("t2", t2);                                         
              bundle.putString("t3", t3);
              bundle.putString("t4", t4);             

              return bundle;
       }

使用frag

中的包
        public String stringo;
        public Bundle bunny;

        // be sure to place the following two lines of code in or after OnCreateView or //else they will end up null.

        bunny = ((Act1) getActivity()).getDetailBundle();
        textView.setText(bunny.getString("t1"));
相关问题