android - Filter a recyclerview from menu item selection -
in app have recyclerview of images populated json file. in toolbar menu, have included selection filter images based on selection. each item in json tagged 1 of selection categories (example included). trying figure out how filter recyclerview list based on user selection. i've searched , searched, can't seem find solution. if sudocode or example, i'd appreciate it. below classes.
json example:
{ "image_url": "http://www.nm-studios.com/trek_lines.png", "title":"trek1", "package_url":"http://www.nm-studios.com/trek_lines.png", "id":lines}, { "image_url": "http://www.nm-studios.com/stuffs/trek_lines2.png", "title":"trek2", "premium":"pro only", "package_url":"http://www.nm-studios.com/trek_lines2.png", "id":lines}, { "image_url": "http://www.nm-studios.com/wall_start.png", "title":"greyshadowed", "premium":"pro only", "package_url":"http://www.nm-studios.com/wall_start.png", "id":lines},
my fragment displaying list:
public class wallpapersfragment extends fragment { private static final string tag = "recyclerviewexample"; private list<feedwallpapers> feeditemlist = new arraylist<feedwallpapers>(); //added static might remove private static recyclerview mrecyclerview; private static myrecycleradapterwallpapers adapter; public string filter_list; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate( r.layout.fragment_wall_recycler, container, false); sethasoptionsmenu(true); /* allow activity show indeterminate progress-bar */ //getactivity().requestwindowfeature(window.feature_indeterminate_progress); /* initialize recycler view */ mrecyclerview = (recyclerview) view.findviewbyid(r.id.my_wall_recycler); mrecyclerview.sethasfixedsize(true); //mrecyclerview.setlayoutmanager(new gridlayoutmanager(getactivity(),2)); mrecyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); /*downloading data below url*/ final string url = "http://www.nm-studios.com/json/wallpaper_list.json"; new asynchttptask().execute(url); return view; } public class asynchttptask extends asynctask<string, void, integer> { /* @override protected void onpreexecute() { setprogressbarindeterminatevisibility(true); }*/ @override protected integer doinbackground(string... params) { inputstream inputstream = null; integer result = 0; httpurlconnection urlconnection = null; try { /* forming th java.net.url object */ url url = new url(params[0]); urlconnection = (httpurlconnection) url.openconnection(); /* request */ urlconnection.setrequestmethod("get"); int statuscode = urlconnection.getresponsecode(); /* 200 represents http ok */ if (statuscode == 200) { bufferedreader r = new bufferedreader(new inputstreamreader(urlconnection.getinputstream())); stringbuilder response = new stringbuilder(); string line; while ((line = r.readline()) != null) { response.append(line); } parseresult(response.tostring()); result = 1; // successful }else{ result = 0; //"failed fetch data!"; } } catch (exception e) { log.d(tag, e.getlocalizedmessage()); } return result; //"failed fetch data!"; } @override protected void onpostexecute(integer result) { // setprogressbarindeterminatevisibility(false); /* download complete. lets update ui */ if (result == 1) { adapter = new myrecycleradapterwallpapers(getactivity(), feeditemlist); mrecyclerview.setadapter(adapter); } else { log.e(tag, "failed fetch data!"); } } } private void parseresult(string result) { try { jsonobject response = new jsonobject(result); jsonarray products = response.optjsonarray("products"); /*initialize array if null*/ if (null == feeditemlist) { feeditemlist = new arraylist<feedwallpapers>(); } (int = 0; < products.length(); i++) { jsonobject product = products.optjsonobject(i); feedwallpapers item = new feedwallpapers(); item.settitle(product.optstring("title")); item.setimage_url(product.optstring("image_url")); item.setpackage_url(product.optstring("package_url")); item.setpremium_wall(product.optstring("premium")); item.setids(product.optstring("ids")); feeditemlist.add(item); } } catch (jsonexception e) { e.printstacktrace(); } } @override public void oncreateoptionsmenu( menu menu, menuinflater inflater) { inflater.inflate(r.menu.wall_menu, menu); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.all: if (item.ischecked()) item.setchecked(false); else item.setchecked(true); filter_list = ""; return true; case r.id.text: if (item.ischecked()) item.setchecked(false); else item.setchecked(true); filter_list = "android_text"; return true; case r.id.lines: if (item.ischecked()) item.setchecked(false); else item.setchecked(true); filter_list = "lines"; return true; case r.id.windows: if (item.ischecked()) item.setchecked(false); else item.setchecked(true); filter_list = "windows"; return true; case r.id.material: if (item.ischecked()) item.setchecked(false); else item.setchecked(true); filter_list = "material"; return true; case r.id.rivalry: if (item.ischecked()) item.setchecked(false); else item.setchecked(true); filter_list = "rivalry"; return true; default: return super.onoptionsitemselected(item); } } }
my holder:
public class feedwallpapersholder extends recyclerview.viewholder implements view.onclicklistener{ public imageview thumbnail; public textview titles; public context context; public textview wall_url; public int wallposition; public textview premiumtag; public feedwallpapersholder(view view) { super(view); view.setonclicklistener(this); this.thumbnail = (imageview) view.findviewbyid(r.id.wall_image); this.titles = (textview) view.findviewbyid(r.id.wall_title); this.wall_url = (textview) view.findviewbyid(r.id.wall_download_url); this.premiumtag = (textview) view.findviewbyid(r.id.premium_tag_walls); context = view.getcontext(); } @override public void onclick(view view) { wallposition = getposition(); //toast.maketext(view.getcontext(), "i'm clicked!! " + wallposition, toast.length_short).show(); //doing stuff here maybe final intent wall_intent = new intent(view.getcontext(), fullscreenactivity.class); final string wallurl = ((textview) view.findviewbyid(r.id.wall_download_url)).gettext().tostring(); final string premium = ((textview) view.findviewbyid(r.id.premium_tag_walls)).gettext().tostring(); final string fn_wall = ((textview) view.findviewbyid(r.id.wall_title)).gettext().tostring() +".png"; wall_intent.putextra("fullimageurl", wallurl); wall_intent.putextra("fnwallfinal", fn_wall); log.i("premium check", premium); //this starts premium check log.i("launch download dialog", "launching"); log.i("url", wallurl); context.startactivity(wall_intent); } }
my adapter:
public class myrecycleradapterwallpapers extends recyclerview.adapter<feedwallpapersholder> { private list<feedwallpapers> feeditemlist; private context mcontext; public myrecycleradapterwallpapers(context context, list<feedwallpapers> feeditemlist) { this.feeditemlist = feeditemlist; this.mcontext = context; } @override public feedwallpapersholder oncreateviewholder(viewgroup viewgroup, int i) { view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.wall_card_layout, null); feedwallpapersholder mh = new feedwallpapersholder(v); return mh; } @override public void onbindviewholder(feedwallpapersholder feedlistrowholder, int i) { feedwallpapers feeditem = feeditemlist.get(i); int n = feedlistrowholder.thumbnail.getwidth(); point size = new point(); windowmanager wm = (windowmanager) mcontext.getsystemservice(context.window_service); display display = wm.getdefaultdisplay(); display.getsize(size); int scrwidth = size.x; string nstring = integer.tostring(n); string nwidth = integer.tostring(scrwidth); log.i("width", nwidth); picasso.with(mcontext).load(feeditem.getimage_url()) .error(r.drawable.placeholder) //.placeholder(r.drawable.placeholder) .transform(new fittransformations(scrwidth,false)) //.fit().centercrop() .into(feedlistrowholder.thumbnail); log.i("the string", feeditem.getimage_url()); feedlistrowholder.titles.settext(html.fromhtml(feeditem.gettitle())); feedlistrowholder.premiumtag.settext(html.fromhtml(feeditem.getpremium_wall())); feedlistrowholder.wall_url.settext((html.fromhtml(feeditem.getpackage_url()))); } @override public int getitemcount() { return (null != feeditemlist ? feeditemlist.size() : 0); } }
don't have time analyze code im not sure you've done already, how it. first make objects out of json objects , put them inside list.
make method takes list of objects , sorts them way want (or remove items dont want) simple iteration.
private list<object> filtermylist(list<object> list, string category) { //... (object obj : list) { // if (!obj.getcategory().equals(category)) -> remove list. } //... return editedlist; }
in adapter create method like
public void updaterecyclerdata(list<object> feeditemlist) { this.feeditemlist = feeditemlist; notifydatasetchanged(); //this update recyclerview }
final product should like:
adapter.updaterecyclerdata(filtermylist(mylist, "material"));
obviously want make categories constants.
Comments
Post a Comment