gpt4 book ai didi

android - 将字符串从 FragmentActivity 传递到 Fragment

转载 作者:行者123 更新时间:2023-11-30 03:27:23 25 4
gpt4 key购买 nike

我能够从 Activity 获取 FragmentActivity 中的 String。但是,如何将我的数据从 FragmentActivity 传递到 Fragments?不过,我可以在 FragmentActivity 中获取我的数据。

(我不想在 SharedPreferences 中存储数据。)

public class IndividualDetailsActivity extends IndividualBasePager {

ViewPager mPager;
IndividualDetailsBaseAdapter mAdapter;
TitlePageIndicator mIndicator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.individual_pager);
mAdapter = new IndividualDetailsBaseAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setOffscreenPageLimit(4);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mIndicator.setCurrentItem(1);
Intent i = getIntent();
getSupportActionBar().setTitle(i.getStringExtra("name"));
}
}

public class IndividualBasePager extends SherlockFragmentActivity {
IndividualDetailsBaseAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;

@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
setSupportProgressBarIndeterminateVisibility(true);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}


public class IndividualDetailsBaseAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "Comments",
"Details", "Screens", "Tutorial" };

private int mCount = CONTENT.length;

public IndividualDetailsBaseAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
Log.e("PAGE", "Position " + position);
switch (position) {
case 0:
return new CommentsFragment();
case 1:
return new DetailsFragment();
case 2:
return new CommentsFragment();
case 3:
return new CommentsFragment();
}
return null;
}

@Override
public int getCount() {
return mCount;
}

@Override
public CharSequence getPageTitle(int position) {
return IndividualDetailsBaseAdapter.CONTENT[position % CONTENT.length];
}

public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}

最佳答案

在您的 fragment 中,您可以通过调用以下方法获取 FragmentActivity getActivity() 。在您的 FragmentActivity 中创建一个类似 getValue() 的方法,它将返回您所需的数据。

关于android - 将字符串从 FragmentActivity 传递到 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18030202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com