android how to show a fragment taking a list of object -
button b = (button) findviewbyid(r.id.button); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.i("hi","onclick()"); fragment f = productlistfragment.newinstance(productarray); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.fragment, f); ft.commit(); } });
this in main activity letting user click button create fragment containing productarray data.
import android.support.v4.app.fragment; import android.os.bundle; import android.support.annotation.nullable; import android.util.log; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.gridview; import java.util.arraylist; public class productlistfragment extends fragment{ private arraylist<product> productarray; public arraylist<product> getproductarray() { return productarray; } public void setproductarray(arraylist<product> productarray) { this.productarray = productarray; } public static fragment newinstance(arraylist<product> productarray){ productlistfragment productlistfragment = new productlistfragment(); productlistfragment.setproductarray(productarray); return productlistfragment; } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { gridview gridview = (gridview) inflater.inflate(r.layout.product_list_fragment, container, false); gridview.setnumcolumns(2); gridview.setgravity(gravity.center); gridview.setadapter( new customgridadapter( getactivity(), getproductarray() ) ); return gridview; } }
this fragment class putting data in productarray grid view fine in previous tests.
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="open fragment" android:id="@+id/button"/> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.it.fyp.fyprestaurant.productlistfragment" android:id="@+id/fragment"/> </linearlayout>
this activity_main.xml. knew if make fragment in way, error occur because productarray null. fragment tag needed, reserving place fragment or whatever reasons? how can show new fragment? pls help!
you should add productarray bundle , productarray bundle , product
model needs implement parcelable.
private static final string bundle_product_list = "bundle_product_list"; public static fragment newinstance(arraylist<product> productarray){ productlistfragment productlistfragment = new productlistfragment(); bundle bundle = new bundle(); bundle.putparcelablearraylist(bundle_product_list, productarray); productlistfragment.setarguments(bundle); return productlistfragment; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); if (getarguments() != null) { productarray = getarguments().getparcelablearraylist(bundle_product_list); } }
========================================
updated:
parcelable example:
public class product implements parcelable { private string id; private string name; public product(string id, string name) { this.id = id; this.name = name; } @override public int describecontents() { return 0; } public static final parcelable.creator<product> creator = new parcelable.creator<product>() { public product createfromparcel(parcel in) { return new product(in); } public product[] newarray(int size) { return new product[size]; } }; private product(parcel in) { id = in.readstring(); name = in.readstring(); } @override public void writetoparcel(parcel out, int i) { // make sure order of writestring must same order // in.readstring of constructor above. out.writestring(id); out.writestring(name); } }
Comments
Post a Comment