1
我有MainActivity和三個片段活動。在MainActivity中,它從REST服務調用中獲取數據。我想將REST數據傳遞給所有三個片段。如何實現它?如何將其餘api列表傳遞給android中的片段活動
這裏我的代碼
MainActivty
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private CustomListAdapter adapter;
private static final String TAG = MainActivity.class.getSimpleName();
public List<MenuItem> mainList = new ArrayList<MenuItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JsonArrayRequest itemReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
JSONArray jsonArray = response.getJSONArray(0);
// Parsing json
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject obj = jsonArray.getJSONObject(i);
MenuItem menuItem = new MenuItem();
menuItem.setName(obj.getString("name"));
menuItem.setImage(obj.getString("image"));
menuItem.setPrice(obj.getString("price"));
menuItem.setDesc(obj.getString("desc"));
mainList.add(menuItem);
} catch (JSONException e) {
e.printStackTrace();
}
}
}catch (JSONException e){
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new First(), "FIRST");
adapter.addFragment(new Second(), "SECOND");
adapter.addFragment(new Third(), "THIRD");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
片段活動
public class First extends Fragment{
private ProgressDialog pDialog;
private GridView gridView;
private CustomListAdapter adapter;
public First() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
gridView = (GridView) view.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), startersList);
gridView.setAdapter(adapter);
return view;
}
}
在這一行adapter = new CustomListAdapter(getActivity(),startersList); 我想從MainActivity
請出示你有 –
簡短的回答是使用Parcelable並通過捆綁的參數爲所有碎片 –
我已經把我的代碼 – jeeva