如何将数据从活动发送到片段

我知道这里有很多关于这个的话题。 我也多次阅读文档,但是我找不到将数据从活动传递到片段的最佳方法。

我想能够显示我的search活动的结果在两个不同的布局(列表和地图)使用滑动视图与选项卡 。 我必须传递2个数据到片段:当前用户位置的“currentLocation”和作为对象列表的“result”。

我已经省略了我的代码的一些部分,使其更容易理解。

SearchableActivity.java

public class SearchableActivity extends ActionBarActivity implements TabListener { List<PlaceModel> result = new ArrayList<PlaceModel>(); private SearchView mSearchView; private String currentLocation; AppSectionsPagerAdapter mAppSectionsPagerAdapter; ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_searchable); final ActionBar actionBar = getSupportActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mAppSectionsPagerAdapter); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); actionBar.addTab(actionBar.newTab().setText("List").setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("Map").setTabListener(this)); // get currentLocation here handleIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { handleIntent(intent); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { final String query = intent.getStringExtra(SearchManager.QUERY); // get result here } } @Override public void onTabReselected(Tab arg0, FragmentTransaction arg1) { // TODO Auto-generated method stub } @Override public void onTabSelected(Tab tab, FragmentTransaction arg1) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { // TODO Auto-generated method stub } 

}

PlaceListFragment.java

 public class PlaceListFragment extends Fragment { ListView listViewData; PlaceAdapter placeAdapter; List<PlaceModel> result = new ArrayList<PlaceModel>(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_list, container, false); Bundle args = getArguments(); listViewData = (ListView) rootView.findViewById(android.R.id.list); // I will pass result and currentLocation here placeAdapter = new PlaceAdapter(getActivity(), R.layout.fragment_list_item, result, currentLocation); listViewData.setAdapter(placeAdapter); return rootView; } } 

AppSectionsPagerAdapter.java

 public class AppSectionsPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { Bundle data = new Bundle(); switch(arg0) { case 0: PlaceListFragment fragment1 = new PlaceListFragment(); fragment1.setArguments(data); return fragment1; default: PlaceListFragment fragment2 = new PlaceListFragment(); fragment2.setArguments(data); return fragment2; } } @Override public int getCount() { return PAGE_COUNT; } } 

Solutions Collecting From Web of "如何将数据从活动发送到片段"

通常这些活动都会参考他们的片段。 在您的SearchableActivity.java中,您还将PlaceListFragment.java加载到setContentView(activity_searchable.xml); 或者您需要创build片段的实例并使用FragmentTransaction添加/replaceFragmentTransaction

你可以在这里find一个很好的例子,如何在片段之间或活动与片段之间进行通信。

培训链接

从活动中发送数据意图为:

 Bundle bundle=new Bundle(); bundle.putString("name", "From Activity"); //set Fragmentclass Arguments Fragmentclass fragobj=new Fragmentclass(); fragobj.setArguments(bundle); 

并在片段onCreateView方法:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String strtext=getArguments().getString("name"); return inflater.inflate(R.layout.fragment, container, false); } 

在Activity的onCreate中查找片段,并将数据设置为您在片段中写入的方法:

  ExampleFragment rf = (ExampleFragment) getSupportFragmentManager().findFragmentById(R.id.exampleFragment); if(rf!=null){ rf.setExample(currentExample); } 

“CurrentExample”是任何你想要发送到你的片段中的“setExample”方法。

 public void setExample(ExampleObject currentExample){ currentExampleInFragment = currentExample; } 

你可以使用Fragment的onActivityCreated方法中的数据。

不知道这是否是一个好的解决scheme,但我发现它是最简单的传递对象。

 Bundle bundle = new Bundle(); bundle.putString("edttext", "From Activity"); // set Fragmentclass Arguments Fragmentclass fragobj = new Fragmentclass(); fragobj.setArguments(bundle); 

并在片段onCreateView方法:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String strtext = getArguments().getString("edttext"); return inflater.inflate(R.layout.fragment, container, false); } 

在这里看到详细的答案

“从活动发送数据到片段”

活动:

  Bundle bundle = new Bundle(); bundle.putString("message", "Alo Stackoverflow!"); FragmentClass fragInfo = new FragmentClass(); fragInfo.setArguments(bundle); transaction.replace(R.id.fragment_single, fragInfo); transaction.commit(); 

分段:

读取片段中的值

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Bundle bundle = this.getArguments(); String myValue = bundle.getString("message"); ... ... ... } 

要么

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String myValue = this.getArguments().getString("message"); ... ... ... } 

创build一个会话模型类,并在活动设置你想要的数据值需要发送,然后在Fragment中,你可以从会话模型类

例如。 从你的活动你可以设置这样的。

  AllEventDetails.getInstance().setEvent_description(event_Description); AllEventDetails.getInstance().setDj_name(dj_name); AllEventDetails.getInstance().setMusic_theme(music_theme); AllEventDetails.getInstance().setClub_name(club_name); AllEventDetails.getInstance().setDate(event_date); AllEventDetails.getInstance().setBanner_image_path(banner_image_path); AllEventDetails.getInstance().setEvent_title(event_title); 

从你的碎片你可以这样回顾。

 AllEventDetails.getInstance().getClub_name() ......... 

创build会话模型类是这样的。

 public class AllEventDetails { private static AllEventDetails mySession ; private String event_description; private String dj_name; private String music_theme; private String club_name; private String date; private String banner_image_path; private String event_title; private AllEventDetails() { event_description = null; dj_name = null; music_theme = null; club_name = null; date = null; banner_image_path = null; event_title = null; } public static AllEventDetails getInstance() { if( mySession == null ) { mySession = new AllEventDetails() ; } return mySession ; } public void resetSession() { mySession=null; } public String getEvent_description() { return event_description; } public void setEvent_description(String event_description) { this.event_description = event_description; } public String getDj_name() { return dj_name; } public void setDj_name(String dj_name) { this.dj_name = dj_name; } public String getMusic_theme() { return music_theme; } public void setMusic_theme(String music_theme) { this.music_theme = music_theme; } public String getClub_name() { return club_name; } public void setClub_name(String club_name) { this.club_name = club_name; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getBanner_image_path() { return banner_image_path; } public void setBanner_image_path(String banner_image_path) { this.banner_image_path = banner_image_path; } public String getEvent_title() { return event_title; } public void setEvent_title(String event_title) { this.event_title = event_title; } }