android - Pass arraylists from fragment to root activity -
i building order receiving app waiter in half page activity layout contains listview , half viewpager contains json arraylist in fragment. want add menu data fragment when clicked on + button number of quantity add on root activity's listview
<relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <framelayout android:id="@+id/my_container_1_id" android:layout_width="fill_parent" android:layout_height="wrap_content"> <android.support.design.widget.appbarlayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/themeoverlay.appcompat.dark.actionbar"> <android.support.design.widget.collapsingtoolbarlayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="140dp" android:fitssystemwindows="true" app:contentscrim="?attr/colorprimary" app:layout_scrollflags="scroll|exituntilcollapsed"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:contentdescription="@string/app_name" app:layout_collapsemode="parallax" android:background="@mipmap/bgactionbar"> <imageview android:id="@+id/logo" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginleft="30dp" android:layout_centervertical="true" android:layout_alignparentleft="true" android:background="@mipmap/logoapp" /> <imageview android:id="@+id/triangle" android:layout_width="280dp" android:layout_height="100dp" android:layout_torightof="@+id/logo" android:background="@mipmap/caley" /> <relativelayout android:id="@+id/searchlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_torightof="@+id/triangle" android:background="#f3eee8" android:descendantfocusability="beforedescendants" android:focusableintouchmode="true"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:src="@android:drawable/ic_menu_search" android:layout_torightof="@+id/search_menu" /> <edittext android:id="@+id/search_menu" android:layout_width="350dp" android:layout_height="40dp" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:hint="search menu..." android:textcolorhint="@color/tab_text" android:textcolor="@color/tab_text" android:background="@android:color/transparent" android:layout_alignparentleft="true" android:inputtype="textvisiblepassword"/> </relativelayout> <imageview android:id="@+id/coffee" android:layout_width="110dp" android:layout_height="140dp" android:layout_torightof="@+id/searchlayout" android:background="@mipmap/coffee" android:layout_alignparentright="true" /> </relativelayout> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" app:layout_collapsemode="pin" app:popuptheme="@style/themeoverlay.appcompat.light" /> </android.support.design.widget.collapsingtoolbarlayout> <android.support.design.widget.tablayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabgravity="fill" style="@style/mycustomtablayout" android:background="@mipmap/background" app:tabmode="fixed" /> </android.support.design.widget.appbarlayout> </framelayout> <relativelayout android:id="@+id/content" android:layout_width="360dp" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentbottom="true" android:background="@mipmap/background" android:layout_below="@id/my_container_1_id"> <textview android:id="@+id/txtorder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:text="order" android:textstyle="bold" android:textcolor="@color/tab_text" android:textsize="20sp" /> <android.support.v7.widget.recyclerview android:id="@+id/orderlist" android:layout_width="340dp" android:layout_height="match_parent" android:layout_above="@+id/submit_order" android:layout_below="@+id/txtorder" android:background="@mipmap/background" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <button android:id="@+id/submit_order" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignend="@+id/orderlist" android:layout_alignparentbottom="true" android:layout_alignright="@+id/orderlist" android:layout_marginbottom="10dp" android:layout_marginleft="10dp" android:background="#ee6426" android:textcolor="@android:color/white" android:text="submit" /> </relativelayout> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignparentright="true" android:layout_below="@+id/my_container_1_id" android:layout_torightof="@+id/content" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </relativelayout>
public class menu extends appcompatactivity implements coffee.onmenuinteractionlistener { // private arraylist<menudatamodel> allorders; private toolbar toolbar; private tablayout tablayout; private viewpager viewpager; listview listview; recyclerview recyclerview; recycleradapter adapter; // menutabadapter adapter; // arraylist<menudatamodel> allorders; private list<menudatamodel> allorders = new arraylist<menudatamodel>(); // private list<string> orderlist = new arraylist<>(); private string quantity, name; edittext count, inputsearch; textview order; string searchvalue; private arraylist<string> stringarraylist; collapsingtoolbarlayout collapsingtoolbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_menu); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); collapsingtoolbar= (collapsingtoolbarlayout) findviewbyid(r.id.collapsing_toolbar); // collapsingtoolbar.settitle(getstring(r.string.app_name)); viewpager = (viewpager) findviewbyid(r.id.viewpager); setupviewpager(viewpager); tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(viewpager); // listview = (listview) findviewbyid(r.id.orderlist); // adapter = new menutabadapter(this, allorders); // listview.setadapter(adapter); textview orddd = (textview) findviewbyid(r.id.txtorder); inputsearch = (edittext) findviewbyid(r.id.search_menu); // inputsearch.setonclicklistener(new view.onclicklistener() { // @override // public void onclick(view v) { // // collapsingtoolbar.setvisibility(view.gone); // // } // }); // recyclerview = (recyclerview) findviewbyid(r.id.orderlist); // recyclerview.sethasfixedsize(true); // linearlayoutmanager layoutmanager = new linearlayoutmanager(this); // recyclerview.setlayoutmanager(layoutmanager); // // adapter = new recycleradapter(this, allorders); // recyclerview.setadapter(adapter); } private void setupviewpager(viewpager viewpager) { viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager()); adapter.addfragment(new coffee(), "coffee"); adapter.addfragment(new coffee(), "breakfast"); adapter.addfragment(new coffee(), "beverage"); viewpager.setadapter(adapter); } @override public void onfragmentsetorders(arraylist<menudatamodel> menulist) { allorders = menulist; } @override public void onmenulistitemclick(int position) { //musicservice.setsong(position); menudatamodel menuorder = allorders.get(position); // menuorder.setname(menuorder.getname()); // menuorder.setname(allorders); allorders.add(menuorder); } class viewpageradapter extends fragmentpageradapter { private final list<fragment> mfragmentlist = new arraylist<>(); private final list<string> mfragmenttitlelist = new arraylist<>(); public viewpageradapter(fragmentmanager manager) { super(manager); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } public void addfragment(fragment fragment, string title) { mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } @override public charsequence getpagetitle(int position) { return mfragmenttitlelist.get(position); } } } public class coffee extends fragment { listview listview; menusadapter adapter; // movies json url private static final string url = "url"; private progressdialog pdialog; private list<menudatamodel> menulist = new arraylist<menudatamodel>(); onmenuinteractionlistener menuitemclick; private string searchdata; private edittext inputsearch; // activity activity; //onmenuinteractionlistener mcallback; public coffee() { // required empty public constructor } public interface onmenuinteractionlistener { public void onfragmentsetorders(arraylist<menudatamodel> menulist); public void onmenulistitemclick(int position); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public void onattach(activity activity) { super.onattach(activity); menuitemclick = (onmenuinteractionlistener) getactivity(); } @override public void ondetach() { super.ondetach(); menuitemclick = null; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // searchdata = getarguments().getstring("search"); inputsearch = (edittext) getactivity().findviewbyid(r.id.search_menu); inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { // when user changed text //adapter.getfilter().filter(cs.tostring()); if (count < before) { // we're deleting char need reset adapter data adapter.resetdata(); } coffee.this.adapter.getfilter().filter(s); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } }); view view = inflater.inflate(r.layout.fragment_coffee, container, false); listview = (listview) view.findviewbyid(r.id.list); adapter = new menusadapter(getactivity(), menulist); listview.setadapter(adapter); pdialog = new progressdialog(getactivity()); // showing progress dialog before making http request pdialog.setmessage("loading..."); pdialog.setcancelable(false); showpdialog(); // creating volley request obj jsonobjectrequest bookingreq = new jsonobjectrequest(request.method.get, "" + url + "?", null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { log.d("bsd", response.tostring()); // parsing json try { jsonarray menu = response.getjsonarray("menus"); int length = menu.length(); (int = 0; < menu.length(); i++) { jsonobject obj = menu.getjsonobject(i); menudatamodel dm = new menudatamodel(); // log.d("vdata", string.valueof(menu.length())); dm.setid(obj.getstring("id")); dm.setname(obj.getstring("name")); dm.setthumbnailurl(obj.getstring("photo")); log.d("image", string.valueof(obj.getstring("photo"))); dm.setdescription(obj.getstring("description")); dm.setrate(obj.getstring("price")); dm.setstatus(obj.getstring("status")); // adding movie movies array menulist.add(dm); // log.d("nth", string.valueof(i)); } } catch (jsonexception e) { e.printstacktrace(); } hidepdialog(); // notifying list adapter data changes // renders list view updated data adapter.notifydatasetchanged(); } }, new response.errorlistener() { @suppresswarnings("deprecation") @override public void onerrorresponse(volleyerror error) { volleylog.d("b", "error: " + error.getmessage()); hidepdialog(); } }); // adding request request queue appcontroller.getinstance().addtorequestqueue(bookingreq); return view; } private void showpdialog() { if (!pdialog.isshowing()) pdialog.setmessage("please wait..."); pdialog.show(); } private void hidepdialog() { if (pdialog != null) { pdialog.dismiss(); pdialog = null; } } } public class menusadapter extends baseadapter implements filterable { private static final string tag = menusadapter.class.getsimplename(); list<menudatamodel> menuitems; list<menudatamodel> msearchvalues; private android.widget.filter menufilter; coffee.onmenuinteractionlistener mcallback; //private activity activity; imageloader imageloader = appcontroller.getinstance().getimageloader(); public menusadapter(activity activity, list<menudatamodel> menuitems) { //this.activity = activity; this.menuitems = menuitems; this.msearchvalues = menuitems; } @override public int getcount() { return menuitems.size(); // total number of elements in list } @override public object getitem(int i) { return menuitems.get(i); // single item in list } @override public long getitemid(int i) { return i; // index number } @override public view getview(final int index, view view, final viewgroup parent) { if (view == null) { layoutinflater inflater = layoutinflater.from(parent.getcontext()); view = inflater.inflate(r.layout.menu_item, parent, false); } // if (imageloader == null) // imageloader = appcontroller.getinstance().getimageloader(); final imageview increase = (imageview) view.findviewbyid(r.id.icon_increase); imageview decrease = (imageview) view.findviewbyid(r.id.icon_decrease); final edittext count = (edittext) view.findviewbyid(r.id.count_menu); networkimageview thumbnailurl = (networkimageview) view.findviewbyid(r.id.menu_image); textview name = (textview) view.findviewbyid(r.id.menu_items); // textview description = (textview) view.findviewbyid(r.id.description); // textview rate = (textview) view.findviewbyid(r.id.price); final menudatamodel data = menuitems.get(index); name.settext(string.valueof(data.getname())); thumbnailurl.setimageurl(data.getthumbnailurl(), imageloader); thumbnailurl.setdefaultimageresid(r.mipmap.logoapp); thumbnailurl.seterrorimageresid(r.mipmap.logoapp); // description.settext(string.valueof(data.getdescription())); // title.settext(data.gettitle()); // rate.settext(string.valueof(data.getrate())); // final double dis = double.valueof(data.getrate()); final int[] quantity = {menuitems.get(index).getanint()}; increase.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub //toast.maketext(parent.getcontext(), "button clicked"+ datamodel.getname(),toast.length_long).show(); //intent yes= new intent(parent.getcontext(), yes(datamodel.getname().class)); quantity[0]++; count.settext(quantity[0] + ""); count.settag(quantity[0] + ""); // mcallback.onfragmentsetorders(all); mcallback.onmenulistitemclick(menuitems.get(index).getanint()); // coffee.onmenuinteractionlistener listener = (coffee.onmenuinteractionlistener) activit; // mcallback.onfragmentsetorders(menu); // bundle bundle = new bundle(); // bundle.putstring("quantity", quantity[0] + ""); // bundle.putstring("name", string.valueof(data.getname())); // q.putextra("bookingid", datamodel.getbkid()); // parent.getcontext().startactivity(q); } }); decrease.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub count.gettag(); count.settag(quantity[0] + ""); quantity[0]--; count.settext(quantity[0] + ""); } }); view.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); return view; } @override public filter getfilter() { if (menufilter == null) menufilter = new menufilter(); return menufilter; } public void resetdata() { menuitems = msearchvalues; } private class menufilter extends android.widget.filter { @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); // implement here filter logic if (constraint == null || constraint.length() == 0) { // no filter implemented return list results.values = msearchvalues; results.count = msearchvalues.size(); } else { // perform filtering operation list<menudatamodel> ndriverlist = new arraylist<menudatamodel>(); (menudatamodel p : menuitems) { if (p.getname().touppercase().startswith(constraint.tostring().touppercase())) ndriverlist.add(p); } results.values = ndriverlist; results.count = ndriverlist.size(); } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { // have inform adapter new list filtered if (results.count == 0) notifydatasetinvalidated(); else { menuitems = (list<menudatamodel>) results.values; notifydatasetchanged(); } } } ; }
i getting null pointer exception on mcallback.onmenulistitemclick(menuitems.get(index).getanint());
step 1: create interface
public interface activitycommunicator{ public void passdatatoactivity(arraylist<string> arraylist); }
step 2:initialize interface object in fragment class
private activitycommunicator activitycommunicator;; public void onattach(activity activity) { super.onattach(activity); context = getactivity(); activitycommunicator =(activitycommunicator)context; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); init(); } public void init() { activitybutton.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { activitycommunicator.passdatatoactivity("your array list"); } }); }
step 3: access arraylist fragment in activity class.
public class mainactivity extends fragmentactivity implements activitycommunicator{ public static arraylist<string> alist; @override public void passdatatoactivity(arraylist<string> arraylist){ alist = arraylist; } }
Comments
Post a Comment