activity가 Fragment에 값 전달

1871 단어 Android
.activity가 Fragment에 전달된 값:
(1)Bundle  :
        Fragment.setArguments(Bundle)     
        Fragment.getArguments(Bundle)     
    Activity     :
        a:             
            FragmentTransaction ft = getFragmentManager().beginTransaction();
        b:   Fragment ,    ;
            ContentFragment cf = new ContentFragment();//              
        c:   Bundle,   put...  
            Bundle bundle = new Bundle();
            bundle.putString(key,"     "+new date());
        d:  setArguments() Fragmenu     
            cf.setArgument(bundle);
        e: Frangment        
            ft.add(R.id.contentId,cf);
        f:    
            ft.commit();//commit   Fragment         
    Fragment      :
        a:  Bundle  ,getArguments();
            Bundle bundle = getArguments();
            String data = bundle.getString(key);
                        

(2)Actvity       :
    a: Activity        ,          ,  get,      ,     
          :getList();
    b:   Fragment   getActivity()      Fragment   Activity  ,
                     ,              。
        ((MainActivity)getActivity).getList()--->     list
            

Demo:
    Activity :
        private List list = new ArrayList();
        //       
        public List getList() {
            return list;
        }
        // List     

    Fragment :  
    //    
        View view = inflater.inflate(R.layout.fragment_layout, null);
    //2.  activity     
        List list = ((MainActivity) getActivity()).getList();
        ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, list);

        ListView listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(adapter);
        return view;    

좋은 웹페이지 즐겨찾기