android - non-empty constructor issue -
i have tried make non empty constructor no success. eclipse says :
from fragment documentation: every fragment must have empty constructor, can instantiated when restoring activity's state. recommended subclasses not have other constructors parameters, since these constructors not called when fragment re-instantiated; instead, arguments can supplied caller setarguments(bundle) , later retrieved fragment getarguments().
so how can make non empty constructor? here code :
import java.util.arraylist; import java.util.list; import android.annotation.suppresslint; import android.content.context; import android.os.bundle; import android.app.listfragment; import android.app.loadermanager.loadercallbacks; import android.content.asynctaskloader; import android.content.loader; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; public class mylistfragment extends listfragment implements loadercallbacks<void> { private static final string tag = "fragmenttabs"; private string mtag; private myadapter madapter; private arraylist<string> mitems; private layoutinflater minflater; private int mtotal; private int mposition; private static final string[] = { "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "fusce", "pharetra", "luctus", "sodales" }; private static final string[] faq = { "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii", "xiii", "xiv", "xv" }; private static final string[] tips = { "hello" , "bitch" , "ass" , "partners" , "screw", "you" ,"all", "peace" , "out"}; private static final int sleep = 1000; private final int wordbarcolor = r.color.word_bar; private final int numberbarcolor = r.color.number_bar; public mylistfragment(string tag) { mtag = tag; mtotal = helpfragment.tab_about.equals(mtag) ? about.length : about.length; log.d(tag, "constructor: tag=" + tag); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // important in order save state across screen // configuration changes example setretaininstance(true); minflater = layoutinflater.from(getactivity()); // need instantiate these first time fragment // created; then, method above rest if (madapter == null) { mitems = new arraylist<string>(); madapter = new myadapter(getactivity(), mitems); } getlistview().setadapter(madapter); // initiate loader background work getloadermanager().initloader(0, null, this); } @override public loader<void> oncreateloader(int id, bundle args) { asynctaskloader<void> loader = new asynctaskloader<void>(getactivity()) { @override public void loadinbackground() { try { // simulate time consuming operation going on in // background thread.sleep(sleep); } catch (interruptedexception e) { } return null; } }; // somehow asynctaskloader doesn't want start job without // calling method loader.forceload(); return loader; } @override public void onloadfinished(loader<void> loader, void result) { // add new item , let adapter know in order refresh // views mitems.add(helpfragment.tab_about.equals(mtag) ? about[mposition] : faq[mposition]); madapter.notifydatasetchanged(); // advance in list 1 step mposition++; if (mposition < mtotal - 1) { getloadermanager().restartloader(0, null, this); log.d(tag, "onloadfinished(): loading next..."); } else { log.d(tag, "onloadfinished(): done loading!"); } } @override public void onloaderreset(loader<void> loader) { } private class myadapter extends arrayadapter<string> { public myadapter(context context, list<string> objects) { super(context, r.layout.list_item, r.id.text, objects); } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; wrapper wrapper; if (view == null) { view = minflater.inflate(r.layout.list_item, null); wrapper = new wrapper(view); view.settag(wrapper); } else { wrapper = (wrapper) view.gettag(); } wrapper.gettextview().settext(getitem(position)); wrapper.getbar().setbackgroundcolor( mtag == helpfragment.tab_about ? getresources().getcolor( wordbarcolor) : getresources().getcolor( numberbarcolor)); return view; } } // use wrapper (or view holder) object limit calling // findviewbyid() method, parses entire structure of // xml in search id of view private class wrapper { private final view mroot; private textview mtext; private view mbar; public wrapper(view root) { mroot = root; } public textview gettextview() { if (mtext == null) { mtext = (textview) mroot.findviewbyid(r.id.text); } return mtext; } public view getbar() { if (mbar == null) { mbar = mroot.findviewbyid(r.id.bar); } return mbar; } } }
thanks provided u.
so how can make non empty constructor?
generally, don't create non-empty constructor on fragment
. instead, use factory pattern, described in comments:
static editorfragment newinstance(int position) { editorfragment frag=new editorfragment(); bundle args=new bundle(); args.putint(key_position, position); frag.setarguments(args); return(frag); }
your fragment can retrieve bundle
via getarguments()
, , can retrieve whatever data put bundle
.
Comments
Post a Comment