Reputation: 151
I have MainActivity that has two fragment (Tab1 and Tab2). I have fetched data from URL through XML parsing 2 times and store in 2 different ArrayList in MainActivity.
These two fragment has list views. Now i have to pass 1 Arraylist to one fragment and pass 2nd ArrayList to second fragment and then show these ArrayList into ListViews of these two fragments.
How i pass 2 Arraylists to these two fragment and how to show them in these 2 fragments.
This is onCreate method of MinActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadPage();
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
I have two ArrayList in another function(OnPostExecute) where i received data thorugh XML parsing.
I haven't edit anything in Two fragments. I just create these two fragment with List View up till now
Upvotes: 1
Views: 2673
Reputation: 4072
Fragment
s have a method called setArguments(Bundle)
. Bundle
has built-in support for various ArrayList
. You simply put the ArrayList
inside the Bundle
using a key of your choosing.
However, I do suggest that you follow the newInstance(<parameters>)
pattern whereby your Fragment has a static
newInstance() method which takes any arguments the Fragment
requires to function, in this case an ArrayList
of some sort.
Quoting an example from the official Fragment documentation:
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
This Bundle
can be retrieved later (at any time), but is typically done in onCreate()
or similar. The "index"
stored above could be retrieved as follows:
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
mIndex = args.getInt("index");
}
}
In the example above the parameter is an int
, but the same logic can be followed with an ArrayList
.
Upvotes: 3