java - Member variable null on function return -
possible duplicate, doesn't apply example: here
i have 2 member variables mselectedorderitem , mselectedorderitemid, both of un-initialised , therefore null. (still doesn't work if assign them null in oncreate). there's arraylist morderlist.
in list view, if select item, these variables assigned values corresponding item.
i want use assigned values in method onclick, onclick sees mselectedorderitem null, regardless of whether initialised in onitemclick , can't figure out why.
import java.util.arraylist; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttabhost; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; public class menufragment extends fragment { public static string tag = "menufragment"; public static final string title= "menu"; private fragmenttabhost mtabhost; private listview morderlistview; private arraylist<orderitem> morderlist; private arrayadapter<orderitem> morderlistadapter; private orderitem mselectedorderitem; private integer mselectedorderitemid; /** * returns new instance of menufragment managing fragments * * @return new instance of menufragment */ static menufragment newinstance() { return new menufragment(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { linearlayout layout = (linearlayout) inflater.inflate(r.layout.menu_fragment, container, false); // set inner food/drink tabs mtabhost = (fragmenttabhost) layout.findviewbyid(android.r.id.tabhost); mtabhost.setup(getactivity(), getchildfragmentmanager(), r.id.menu_items_fragment_container); mtabhost.addtab(mtabhost.newtabspec("food").setindicator(fooditemsfragment.title), fooditemsfragment.class, null); mtabhost.addtab(mtabhost.newtabspec("drinks").setindicator(drinkitemsfragment.title), drinkitemsfragment.class, null); // set list view morderlistview = (listview) layout.findviewbyid(r.id.ordered_items); morderlist = new arraylist<orderitem>(); morderlist.add(new orderitem("first item")); morderlist.add(new orderitem("second item")); morderlist.add(new orderitem("third item")); morderlist.add(new orderitem("fourth item")); // set array adapter list view display contents of standing order list morderlistadapter = new arrayadapter<orderitem>(getactivity(), android.r.layout.simple_list_item_1, morderlist); morderlistview.setadapter(morderlistadapter); return layout; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // set list view that, when clicked, details shown in details section // set happens when item in view clicked morderlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // identify element of order list // since list positions , array positions same // i.e. first position in list corresponds morderlist.get(0) // use knowledge mselectedorderitem = morderlist.get(position); mselectedorderitemid = position; // set text of details section // worth storing these member variables since they're accessed // lot? ((textview) getactivity().findviewbyid(r.id.order_name)).settext(mselectedorderitem.getitemname()); ((edittext) getactivity().findviewbyid(r.id.order_note)).settext(mselectedorderitem.getitemnote()); } }); // set happens when delete button pressed ((button) getactivity().findviewbyid(r.id.delete_button)).setonclicklistener(new onclicklistener() { @override public void onclick(view view) { // if there's no item selected, nothing if (mselectedorderitem == null) { log.d(tag, "mselectedorderitem == null"); return; } morderlist.remove(mselectedorderitem); morderlistadapter.notifydatasetchanged(); } }); } }
any ideas?
cheers.
edit: added entire class code.
just add log.i("fragment","mselectedorderitemid = " + mselectedorderitemid); in
public void onitemclick(adapterview<?> parent, view view, int position, long id)
and if called method correct value find solution in other place.
Comments
Post a Comment