繁体   English   中英

试图将意图从SherlockActivity传递到SherlockFragment

[英]Trying to Pass an Intent from a SherlockActivity to a SherlockFragment

我想将意图从我的SherlockActivity传递给我的SherlockFragment,但我对如何执行此操作一无所知。 我想将MaterialsActivity中的CategoryID传递给下面的BooksFragment。 MaterialActivity的代码在下面,并且在远端粘贴了BooksFragment。

      public class MaterialsActivity extends SherlockFragmentActivity{

        String categoryID = null;
        Context context = null;
        Resources resources = null;

        IDatabaseHelper databaseHelper = null;

         // store the active tab here
          public static String ACTIVE_TAB = "activeTab";

          //Initializing the Tab Fragments that would be added to this view
          AudiosFragment audioFragment = null;
          BooksFragment bookFragment = null;
          VideosFragment videoFragment = null;

        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.materials);

            Intent intent = getIntent();

            categoryID  = intent.getExtras().getString("categoryID");

            context = MaterialsActivity.this;
            resources = getResources();

            databaseHelper = new DatabaseHelper(context);


            final ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            // add tabs
            Tab tab1 = actionBar.newTab()
                      .setText("Books")
                      .setTabListener(new BooksListener<BooksFragment>(
                       this, "book", BooksFragment.class));
            actionBar.addTab(tab1);


            // check if there is a saved state to select active tab
            if( savedInstanceState != null ){
              getSupportActionBar().setSelectedNavigationItem(
                          savedInstanceState.getInt(ACTIVE_TAB));
            }


            new CollectMaterials(context,resources).execute();

        }   





//This is the BooksFragment Listener that would make the Tab components to listener to a tab click events
  public class BooksListener<T extends SherlockListFragment> implements ActionBar.TabListener{
      private BooksFragment mFragment;
      private final Activity mActivity;
      private final String mTag;
      private final Class<T> mClass;


      public BooksListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;

      }

      public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
          Bundle bundle = new Bundle();
          bundle.putString("CategoryID", categoryID);

        if (mFragment == null){
          // If not, instantiate and add it to the activity
          mFragment = (BooksFragment) Fragment.instantiate(
                            mActivity, mClass.getName());
          mFragment.setArguments(bundle);

         // mFragment..setProviderId(mTag); // id for event provider
          ft.add(android.R.id.content, mFragment, mTag);
        } else {
          // If it exists, simply attach it in order to show it
          mFragment.setArguments(bundle);
          ft.attach(mFragment);
        }

      }

      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null){
          // Detach the fragment, because another one is being attached
          ft.detach(mFragment);
        }
      }

      public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.

      }
    }




//This is my fragment code below. I would to get the CategoryID here


 public class BooksFragment extends SherlockListFragment{

    TextView textview = null;

     @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.books, container, false);
        // do your view initialization heres
        textview = (TextView)view.findViewById(R.id.textView1);

        Bundle bundle =this.getArguments();

        if(bundle != null){
            String id = bundle.getString("CategoryID");
            Log.i("CategoryID",id);

            textview.setText(id);
        }
        return view;
      }

}

您可以执行以下操作:

    YourFragment yourFrag = new YourFragment();
    Bundle bundle = new Bundle();
    bundle.putString("CATEGORY_ID", CategoryID);
    yourFrag.setArguments(bundle);

要检索信息,请在片段中执行:

 @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.books, container, false);      

   Bundle bundle = getArguments();
   if(bundle != null){
    String CategoryID = bundle.getString("CATEGORY_ID", "no argument pass");
   }

你可以传递一个Bundle到一个Fragment通过获得的参考Fragment ,并使用其setArguments() ; 您将Bundle传递给setArguments() 然后,在Fragment内部使用其getArguments()检索Bundle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM