android - How to set Image In Adapter when we have name of file and it is in assets folder? -
i have image file name, , wanted set in layout in adapter, using recycle view.
{ "formules": [ { "title": "freecharge", "imagename": "freerecharge.jpg", "description": "get 3% cashback on bill payments of `50 or more + 1.2% cashback us", "expiry_date": "valid till 12 jul 2016" }, { "title": "airtel", "imagename": "airtel.jpg", "description": "flat 5% off on airtel postpaid bill payments & airtel prepaid recharges made through airtel money + upto 3.85% cashback from", "expiry_date": "valid till 31 jul 2016" }, { "title": "cleartrip", "imagename": "ctrip.jpg", "description": "flat 25% cashback on domestic hotels + upto `340 cashback us", "expiry_date": "valid till 15 jul 2016" }, { "title": "firstcry", "imagename": "fcry.jpg", "description": "flat `400 off on order of `1100 premium store (for first purchase) + upto `40 cashback us", "expiry_date": "valid till 31 jul 2016" }, { "title": "dominos pizza", "imagename": "dominoz.jpg", "description": "buy 1 & 50% off on 2nd pizza + `29 cashback us", "expiry_date": "valid till 31 jul 2016" }, { "title": "lenskart", "imagename": "lenkart.jpg", "description": "get first frame free + upto `360 cashback us", "expiry_date": "valid till 31 jul 2016" }, { "title": "infibeam", "imagename": "infibeam.jpg", "description": "upto 60% off plus 10% off on best-selling books worth `200 + upto 7% cashback us", "expiry_date": "valid till 20 jul 2016" }, { "title": "indiatimes shopping", "imagename": "itimeshoping.jpg", "description": "shop multimedia phones @ `499 + upto `250 cashback us", "expiry_date": "valid till 31 jul 2016" }, { "title": "excitinglives", "imagename": "exiting.jpg", "description": "sound , light camera keychain @ `399 + `105 cashback us", "expiry_date": "valid till 31 jul 2016" }, { "title": "babyoye", "imagename": "baboye.jpg", "description": "end of season sale - buy 5 & 50% off on maternity wear + upto `410 cashback us", "expiry_date": "valid till 31 jul 2016" }, { "title": "ferns n petals", "imagename": "frennpetals.jpg", "description": "flat 18% off sitewide + upto 13% cashback us", "expiry_date": "valid till 30 sep 2016" } ] }
main class doing action
public class cashbackfragments extends fragment { private recyclerview recyclerview; private myrecycleradapter madapter; recyclerview.layoutmanager mlayoutmanager; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.cash_back_screen, container, false); ((homeactivity) getactivity()).settitle("cash offers"); recyclerview = (recyclerview) rootview.findviewbyid(r.id.recycler_view); recyclerview.sethasfixedsize(true); mlayoutmanager = new linearlayoutmanager(getactivity()); recyclerview.setlayoutmanager(mlayoutmanager); arraylist<hashmap<string, string>> rows = new commonfunction(getactivity()).getdata("cashbacklist.json", "title", "imagename", "description", "expiry_date"); arraylist<feeditem> rowitems = new arraylist<>(); (int = 0; rows.size() > i; i++) { feeditem item = new feeditem(rows.get(i).get("title"), rows.get(i).get("imagename"), rows.get(i).get("description"), rows.get(i).get("expiry_date")); rowitems.add(item); } madapter = new myrecycleradapter(rowitems,getactivity()); recyclerview.setadapter(madapter); recyclerview.itemdecoration itemdecoration = new divideritemdecoration(getactivity(), linearlayoutmanager.vertical); recyclerview.additemdecoration(itemdecoration); return rootview; } @override public void onpause() { super.onpause(); ((homeactivity) getactivity()).settitle("dashboard"); } }
my adapter class :
public class myrecycleradapter extends recyclerview.adapter<myrecycleradapter.myviewholder> { private list<feeditem> datalist; context context; public myrecycleradapter(arraylist<feeditem> rowitems, fragmentactivity activity) { this.datalist = rowitems; context = activity; } public class myviewholder extends recyclerview.viewholder { public textview title; public textview description; public textview expiry_date; public imageview img_cashback; public myviewholder(view view) { super(view); title = (textview) view.findviewbyid(r.id.title); description = (textview) view.findviewbyid(r.id.txt_description); expiry_date = (textview) view.findviewbyid(r.id.expiry_date); img_cashback = (imageview) view.findviewbyid(r.id.img_cashback); } } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemview = layoutinflater.from(parent.getcontext()) .inflate(r.layout.cash_back_list, parent, false); return new myviewholder(itemview); } @override public void onbindviewholder(myviewholder holder, int position) { feeditem data = datalist.get(position); holder.title.settext(data.gettitle()); holder.description.settext(data.getdescription()); holder.expiry_date.settext(data.getexpirydate()); int id = context.getresources().getidentifier("com.rayz.digitalpostbox:drawable/" + data.getimagename(), null, null); holder.img_cashback.setimageresource(id); }
and feeditem class as:
public class feeditem { private string title, imagename, description, expirydate; public feeditem(string title, string imagename, string description, string expirydate) { this.title = title; this.imagename = imagename; this.description = description; this.expirydate = expirydate; } public string getexpirydate() { return expirydate; } public void setexpirydate(string expirydate) { this.expirydate = expirydate; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public string getimagename() { return imagename; } public void setimagename(string imagename) { this.imagename = imagename; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } } @override public int getitemcount() { return datalist.size(); } }
problem unable set image in adpater image view. layout screen as:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableitembackground" android:clickable="true" android:focusable="true" android:orientation="vertical" android:paddingbottom="10dp" android:paddingleft="16dp" android:paddingright="16dp" android:paddingtop="10dp"> <textview android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="20dp" android:gravity="center" android:text="@string/app_name" android:textsize="18sp" android:textstyle="bold" /> <imageview android:id="@+id/img_cashback" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_centerhorizontal="true" android:layout_marginbottom="20dp" android:contentdescription="@string/app_name" android:src="@drawable/freerecharge" /> <textview android:id="@+id/txt_description" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/btn_cash_back" android:layout_alignstart="@+id/btn_cash_back" android:layout_below="@id/img_cashback" android:layout_marginbottom="20dp" android:text="@string/txt_name_demo" android:textsize="16sp" /> <textview android:id="@+id/expiry_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/btn_cash_back" android:layout_alignstart="@+id/btn_cash_back" android:layout_below="@id/txt_description" android:layout_marginbottom="20dp" android:text="@string/txt_date_demo" android:textsize="16sp" /> <button android:id="@id/btn_cash_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/expiry_date" android:layout_centerinparent="true" android:background="@color/colorline" android:paddingend="20dp" android:paddingleft="20dp" android:paddingright="20dp" android:paddingstart="20dp" android:text="@string/txt_cash_back" android:textcolor="@color/colorwhite" android:textsize="18sp" /> </relativelayout>
why dont use picasso load image. willl make work easy.
load asset folder images
picasso.with(context).load("file:///android_asset/"+data.getimagename()).into(holder.img_cashback);
put in onbindviewholder
of recycleview adapter.
source here
Comments
Post a Comment