2017-04-09 44 views
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

+0

請出示你有 –

+2

簡短的回答是使用Parcelable並通過捆綁的參數爲所有碎片 –

+1

我已經把我的代碼 – jeeva

回答

1

獲取列表數據如果你可以讓你的菜單項的類實現Parcelable

How can I make my custom objects Parcelable?

然後它只是傳遞參數給片段

Bundle args = new Bundle(); 
args.putParcelableArrayList("list", mainList) ; 
Fragment first = new First(); 
first.setArguments(first); 
adapter.addFragment(first, "FIRST"); 
問題

在片段內,您可以使用

getArguments().getParcelableArrayList("list") 

或者,您也可以將JSON字符串傳遞給參數並在Fragment中進行解析。

注意:但是,這隻適用於在onResponse中移動viewpager設置方法的情況。否則,您的碎片已經創建了空列表。

或者你可以在這裏實現在適配器上面的邏輯

@Override 
public Fragment getItem(int position) { 
    Fragment f = mFragmentList.get(position); 
    // TODO f.setArguments() 
    return f; 
} 
相關問題