android - Listview inside Cardview using RecyclerView -
i want add dynamically loading listview inside cardviews arranged in grid layout loads dynamically according data present. how can done?
all listviews different according data fetched links:
1) http://www.json-generator.com/api/json/get/btarfsabkg?indent=2 (used)
or
2) http://www.json-generator.com/api/json/get/cnwvbvagoo?indent=2
supporting code same below:
mainactivity.java
import android.content.context; import android.os.asynctask; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.gridlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; public class mainactivity extends appcompatactivity { recyclerview recyclerview; context context; recyclerview.adapter recyclerview_adapter; recyclerview.layoutmanager recyclerviewlayoutmanager; fooditem fooditem; arraylist<fooditem> items; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); fooditem = new fooditem(); items = new arraylist<fooditem>(); new apifetchorder().execute(""); context = getapplicationcontext(); recyclerview = (recyclerview) findviewbyid(r.id.recycler_view1); } public class apifetchorder extends asynctask<string, void, string> { private string jsonresponse = null; @override protected void onpreexecute() { super.onpreexecute(); } @override protected string doinbackground(string... params) { jsonresponse = null; jsonarray jsonarray = null; httpurlconnection urlconnection = null; bufferedreader reader = null; try { url url = new url("http://www.json-generator.com/api/json/get/btarfsabkg?indent=2"); log.d("urlupadteimage", string.valueof(url)); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setconnecttimeout(30000); urlconnection.setrequestmethod("get"); log.d("urlconnection", string.valueof(urlconnection)); inputstream inputstream = urlconnection.getinputstream(); stringbuffer buffer = new stringbuffer(); if (inputstream == null) { return null; } reader = new bufferedreader(new inputstreamreader(inputstream)); string inputline; while ((inputline = reader.readline()) != null) buffer.append(inputline + "\n"); if (buffer.length() == 0) { return null; } jsonresponse = buffer.tostring(); jsonarray = new jsonarray(jsonresponse); log.v("jsonres",jsonarray.getjsonobject(0).getjsonobject("fooditem").getjsonarray("item1").getstring(0)); //log.v("jsonresponse",jsonresponse); int j=1; while(j<=4) { (int = 0; < jsonarray.length(); i++) { jsonarray arr = jsonarray.getjsonobject(i).getjsonobject("fooditem").getjsonarray("item" + j); fooditem.setname(arr.getstring(0)); fooditem.setquantity(arr.getstring(1)); log.v("jsonarray", arr.getstring(0)+arr.getstring(1)); j++; items.add(fooditem); } j++; } } catch (ioexception | jsonexception e) { e.printstacktrace(); } { if (urlconnection != null) { urlconnection.disconnect(); } if (reader != null) { try { reader.close(); } catch (final ioexception e) { } } } return null; } @override protected void onpostexecute(string s) { //change 3 choice because here 3 number of grid layout columns in each row. recyclerviewlayoutmanager = new gridlayoutmanager(context,3); recyclerview.setlayoutmanager(recyclerviewlayoutmanager); recyclerview_adapter = new recyclerviewadapter(context,items); recyclerview.setadapter(recyclerview_adapter); } } }
recyclerviewadapter.java
import android.annotation.suppresslint; import android.app.activity; import android.content.context; import android.graphics.color; import android.os.asynctask; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.listadapter; import android.widget.listview; import android.widget.textview; import org.json.jsonarray; import org.json.jsonexception; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.viewholder> { arraylist<fooditem> values; context context1; itemsadapter listviewadapter; fooditem fooditem; public recyclerviewadapter(context context2,arraylist<fooditem> values2){ context1 = context2; values= values2; } public static class viewholder extends recyclerview.viewholder{ public listview listview; public viewholder(view v){ super(v); listview = (listview) v.findviewbyid(r.id.list); } } @override public recyclerviewadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype){ view view1 = layoutinflater.from(context1).inflate(r.layout.item_view,parent,false); listviewadapter = new itemsadapter(context1,r.layout.items,values); fooditem = new fooditem(); return new viewholder(view1); } @override public void onbindviewholder(viewholder vholder, int position){ vholder.listview.setadapter(listviewadapter); } @override public int getitemcount(){ return values.size(); } }
itemsadapter.java
import android.annotation.suppresslint; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; import java.util.arraylist; public class itemsadapter extends arrayadapter<fooditem> { arraylist<fooditem> itemlist; layoutinflater vi; int resource; viewholder holder; public itemsadapter(context context, int resource , arraylist<fooditem> objects) { super(context, resource , objects); vi = (layoutinflater) context.getsystemservice(context.layout_inflater_service); resource = resource; itemlist = objects; } @suppresslint("settexti18n") @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if (v == null) { holder = new viewholder(); v = vi.inflate(resource, null); holder.itemname = (textview) v.findviewbyid(r.id.itemname); holder.itemquantity = (textview) v.findviewbyid(r.id.itemquantity); v.settag(holder); } else { holder = (viewholder) v.gettag(); } holder.itemname.settext(itemlist.get(position).getname()); holder.itemquantity.settext(itemlist.get(position).getquantity()); return v; } static class viewholder { public textview itemname; public textview itemquantity; } }
fooditem.java
public class fooditem { string name,quantity,tableno; public fooditem(){} public fooditem(string name, string quantity, string tableno) { name = name; quantity = quantity; tableno = tableno; } public string getname() { return name; } public void setname(string name) { name = name; } public string getquantity() { return quantity; } public void setquantity(string quantity) { quantity = quantity; } public string gettableno() { return tableno; } public void settableno(string tableno) { tableno = tableno; } }
layout files:
activity_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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:id="@+id/relativelayout"> <android.support.v7.widget.recyclerview android:id="@+id/recycler_view1" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"> </android.support.v7.widget.recyclerview> </relativelayout>
item_view.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/cardview1" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardelevation="7dp" card_view:contentpadding="7dp" card_view:cardcornerradius="7dp" card_view:cardmaxelevation="7dp" > <listview android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.v7.widget.cardview> </linearlayout>
items.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/itemname" tools:text="item name" android:layout_width="0dp" android:layout_weight="3" android:layout_height="wrap_content" /> <textview android:id="@+id/itemquantity" tools:text="quantity" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </linearlayout>
Comments
Post a Comment