我想将我的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;
}
}
答案 0 :(得分: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");
}
答案 1 :(得分:0)
您可以通过获取Bundle
的引用并使用其Fragment
将Fragment
传递给setArguments()
;您将Bundle
传递给setArguments()
。然后,在Fragment
内使用其getArguments()
来检索Bundle
。