java - Load data into a fragment, then use that data in other classes -


how 1 correctly import data file, inside fragment, use data global variables, in other classes/activities/fragments?

i have this:

assetmanager manager = getactivity().getassets(); 

in myfragment class.

the fragment instantiated inside other fragment:

myfragment mf = new myfragment(); 

in order able variables contain data loaded file.

i had no issues whatsoever using myfragment class in java, , load files whatever folder, here, need context, , files assets folder.

still, when want getassets() in myfragment class, null (even though there files inside folder).

here code of myfragment:

public class filereader extends fragment {     private assetmanager manager;     private inputstream is;      public void loadids(){ //        try {             manager = getactivity().getassets(); //        } catch (nullpointerexception e){ //            e.printstacktrace(); //        }         //file file = new file("ids.txt");         try {             = manager.open("ids.txt");             scanner in = new scanner(is);             totalbuses = in.nextint();             ids = new string[totalbuses];             for(int i=0; i<totalbuses; i++){                 ids[i] = in.next();             }             in.close();         } catch (filenotfoundexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } catch (nullpointerexception e){             e.printstacktrace();         }     } } 

and class fragment called:

public class navigationdrawerfragment extends fragment {     @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         mdrawerlistview = (listview) inflater.inflate(                 r.layout.fragment_navigation_drawer, container, false);         mdrawerlistview.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                 selectitem(position);             }         });          fr = new filereader();         fr.loadids();         string[] list = {"this", "that"};  //        string[] list = new string[fr.gettotalbuses()+1]; //        list[0] = "bun venit!"; //        list[1] = "whatever"; //        list[2] = "something"; //        for(int i=1; i<=fr.gettotalbuses(); i++){ //            list[i] = "autobus " + fr.getschedules()[i-1]; //        }          mdrawerlistview.setadapter(new arrayadapter<string>(                 getactionbar().getthemedcontext(),                 android.r.layout.simple_list_item_1,                 android.r.id.text1,                 list));         mdrawerlistview.setitemchecked(mcurrentselectedposition, true);         return mdrawerlistview;     } } 

enter image description here

it's not practice instantiate fragment using constructor directly. preferred option use newinstance() static method. more info: http://developer.android.com/reference/android/app/fragment.html

but since not using fragment view, helper class, passing context dependency on filereader constructor?

fr = new filereader(getactivity());


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -