2013-09-30 1 views
0

Je veux passer une intention de mon SherlockActivity à mon SherlockFragment mais je ne comprends pas comment faire cela. Je veux passer le CategoryID dans MaterialsActivity à BooksFragment ci-dessous. Le code de l'MaterialActivity est ci-dessous et à l'extrémité lointaine le BooksFragment est collé.Essayer de passer une intention d'un SherlockFragment à SherlockFragment

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

} 

Répondre

1

Vous pouvez faire quelque chose comme ceci:

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

pour récupérer les informations faire dans votre fragment:

@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"); 
    } 
+0

J'essaie la solution mais je reçois un pointeur nul dans mon fragment – user2754532

+0

J'ai effectué les changements dans mon code. J'ai ajouté l'ensemble que j'ai passé dans onTabSelected (Onglet Tab, FragmentTransaction ft). J'ai donc appelé le paquet pour récupérer. Mais j'ai le pointeur Null dans le fragment. – user2754532

+0

êtes-vous sûr categoryID n'est pas nul? Cela fonctionne. 'categoryID = intent.getExtras() getString ("categoryID");'? – Dyna

0

Il est possible de passer un Bundle à un Fragment par l'obtention d'une référence de la Fragment et en utilisant son setArguments(); vous passez le Bundle à setArguments(). Ensuite, à l'intérieur du Fragment, utilisez pour récupérer le Bundle.

+0

Merci beaucoup. J'essaie votre solution maintenant. @Emmanuel – user2754532

+0

Je l'ai fait. mais malheureusement, j'ai couru une exception NullPointer dans mon fragment. Vous pouvez vérifier le code ci-dessus, je modifie mon code précédent affiché. à ma version de travail actuelle. Vous verriez si j'ai utilisé le Bundle. – user2754532

+0

Pouvez-vous poster le stacktrace? – Emmanuel

Questions connexes