java - How to send the right context to the new object -
i have public class pagefragment extends fragment create 6 pages views fills class. here pagefragment class:
public class pagefragment extends fragment { static final string arg_page_number = "arg_page_number"; int pagenumber; int backcolor; public linearlayout framescontainer; executorservice ex = executors.newcachedthreadpool(); future<arraylist<elementdata>> s = ex.submit(new mythread()); public static pagefragment newinstance(int page) { pagefragment pagefragment = new pagefragment(); bundle arguments = new bundle(); arguments.putint(arg_page_number, page); pagefragment.setarguments(arguments); return pagefragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); pagenumber = getarguments().getint(arg_page_number); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.table_row, null); framescontainer = (linearlayout) view.findviewbyid(r.id.frames_container); (int = 0; < 20; += 4) { frame frame = new frame(getapplicationcontext()); try { frame.setstudyname(s.get().get(0).days().get(i)); frame.setstudykindname(s.get().get(0).days().get(i + 1)); frame.setauditorium(s.get().get(0).days().get(i + 2)); frame.setlecturetitle(s.get().get(0).days().get(i + 3)); framescontainer.addview(frame); } catch (exception e) { e.printstacktrace(); } } return view; } class mythread implements callable<arraylist<elementdata>> { public arraylist<elementdata> call() { arraylist<elementdata> elementdataarraylist = parser.parse("url"); return elementdataarraylist; } }}
and myactivity:
public class myactivity extends fragmentactivity { /** * called when activity first created. */ static final string tag = "mylogs"; static final int page_count = 6; viewpager pager; pageradapter pageradapter; sharedpreferences spref; string[] groups = {....}; final string saved_text = "savepref"; private linearlayout framescontainer; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); spref = getpreferences(mode_private); if (spref.getboolean(saved_text, true)) { setcontentview(r.layout.startpage); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, groups); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner spinner = (spinner)findviewbyid(r.id.spinner); spinner.setadapter(adapter); final int selectioncurrent = spinner.getselecteditemposition(); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parentview, view selecteditemview, int position, long id) { if (selectioncurrent != position) { spref = getpreferences(mode_private); sharedpreferences.editor ed = spref.edit(); ed.putboolean(saved_text, false); ed.commit(); setcontentview(r.layout.main); pager = (viewpager) findviewbyid(r.id.frames_container); pageradapter = new myfragmentpageradapter(getsupportfragmentmanager()); pager.setadapter(pageradapter); pager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { log.d(tag, "onpageselected, position = " + position); } @override public void onpagescrollstatechanged(int state) { } }); } } @override public void onnothingselected(adapterview<?> parentview) { } }); } else { setcontentview(r.layout.main); pager = (viewpager) findviewbyid(r.id.frames_container); pageradapter = new myfragmentpageradapter(getsupportfragmentmanager()); pager.setadapter(pageradapter); pager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { log.d(tag, "onpageselected, position = " + position); } @override public void onpagescrollstatechanged(int state) { } }); } } private class myfragmentpageradapter extends fragmentpageradapter { public myfragmentpageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return pagefragment.newinstance(position); } @override public int getcount() { return page_count; } }}
when i'm using getapplicationcontext shows me error of unresolved method. need cast myactivity's context here? how can that?
frame class:
public class frame extends relativelayout { private textview studyname; private textview auditorium; private textview studykindname; private textview lecturetitle; public frame(context context) { super(context); initcomponent(); } private void initcomponent() { layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.table_row, this); studyname = (textview) findviewbyid(r.id.studyname); auditorium = (textview) findviewbyid(r.id.auditorium); studykindname = (textview) findviewbyid(r.id.studykindname); lecturetitle = (textview) findviewbyid(r.id.lecturetitle); } public void setstudyname(string study_name) { studyname.settext(study_name); } public void setauditorium(string auditorium) { auditorium.settext(auditorium); } public void setstudykindname(string studykindname) { studykindname.settext(studykindname); } public void setlecturetitle(string lecturetitle) { lecturetitle.settext(lecturetitle); } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity"> <android.support.v4.view.viewpager android:id="@+id/frames_container" android:layout_width="wrap_content" android:layout_height="fill_parent"> </android.support.v4.view.viewpager> </relativelayout>
table_row.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/program_frame" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dip"> <textview android:id="@+id/studyname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:textcolor="@android:color/white" android:textstyle="normal" android:textsize="16sp" android:text="studyname"/> <textview android:id="@+id/auditorium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:textcolor="@android:color/darker_gray" android:textstyle="bold" android:textsize="16sp" android:singleline="true" android:text="auditorium"/> <textview android:id="@+id/studykindname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/studyname" android:layout_centerhorizontal="true" android:layout_margintop="5dip" android:textcolor="@android:color/darker_gray" android:textstyle="bold" android:textsize="15sp" android:singleline="true" android:text="studykindname"/> <textview android:id="@+id/lecturetitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/studykindname" android:layout_centerhorizontal="true" android:layout_margintop="5dip" android:textcolor="@android:color/darker_gray" android:textstyle="normal" android:textsize="12sp" android:text="lector"/> <imageview android:id="@+id/imageview2" android:layout_below="@id/lecturetitle" android:layout_width="match_parent" android:layout_height="2dp" android:layout_alignparentleft="true" android:layout_marginleft="10dip" android:layout_marginright="10dip" android:layout_margintop="15dip" android:background="#bb333333" /> </relativelayout>
you can use getactivity().getapplicationcontext()
in fragment.
i don't know frame
in frame frame = new frame(getapplicationcontext());
. in case doing ui stuff use getactivity()
.
read
Comments
Post a Comment