0

I want to pass an intent from my SherlockActivity to my SherlockFragment but I am not getting a clue on how to do this. I want to pass the CategoryID in MaterialsActivity to BooksFragment below. The code of the MaterialActivity is below and at the far end the BooksFragment is pasted.

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

}
4

2 回答 2

1

你可以这样做:

    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");
   }
于 2013-09-30T16:04:21.663 回答
0

您可以通过获取 a 的引用并使用它的;将 a 传递Bundle给 a 你通过to 。然后,在内部使用它来检索.FragmentFragmentsetArguments()BundlesetArguments()FragmentgetArguments()Bundle

于 2013-09-30T15:57:17.980 回答