簡體   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