android listview asynctask for textview and imageview -
i trying generate android listview google books api call, google books api call return json data 1 of these properties url book thumbnail thought running first asynctask call json google books api , onpostexecute run second asynctask fetch thumbnails , set them imageview behavior not correct , thumbnails not associated correct row in listview while fetching thumbnails imageview in first row being updated , when download completes order messed up.
below extract code:
private class booksearchlistadapter extends baseadapter { private arraylist<book> mbooks = new arraylist<>(); private layoutinflater mlayoutinflater; public booksearchlistadapter(context c, arraylist<book> books) { mlayoutinflater = layoutinflater.from(c); this.mbooks = books; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = mlayoutinflater.inflate(r.layout.view_book_search_row, null); holder = new viewholder(); holder.mimageview = (imageview) convertview.findviewbyid(r.id.book_cover_thumbnail); holder.mtitletextview = (textview) convertview.findviewbyid(r.id.book_title); holder.mauthortextview = (textview) convertview.findviewbyid(r.id.book_author); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } book b = mbooks.get(position); holder.mauthortextview.settext(textutils.join(", ", b.getauthors())); holder.mtitletextview.settext(b.gettitle()); if (!b.getthumbnail().equalsignorecase("")) { new thumbnaildownloadertask(holder.mimageview).execute(b.getthumbnail()); } return convertview; } @override public int getcount() { return mbooks.size(); } @override public object getitem(int position) { return mbooks.get(position); } @override public long getitemid(int position) { return position; } } private static class viewholder { imageview mimageview; textview mtitletextview; textview mauthortextview; } private class searchbooktask extends asynctask<string, void, string> { private final string tag = "searchbooktask"; private arraylist<book> mbooks = new arraylist<>(); private final static string book_items_key = "items"; private final static string book_volumeinfo_key = "volumeinfo"; private final static string book_imagelinks_key = "imagelinks"; private final static string book_id_key = "id"; private final static string book_title_key = "title"; private final static string book_authors_key = "authors"; private final static string book_description_key = "description"; private final static string book_thumbnail_key = "thumbnail"; @override protected string doinbackground(string... params) { string bookquery = params[0]; string url = uri.parse(endpoint).buildupon() .appendqueryparameter("q", bookquery) .appendqueryparameter("maxresults", "10") .appendqueryparameter("key", api_key) .build().tostring(); string result = new googlefetcher().geturl(url); return result; } @override protected void onpreexecute() { memptytextview.setvisibility(view.gone); mprogressbar.setvisibility(view.visible); } @override protected void onpostexecute(string s) { if (s != null) { mprogressbar.setvisibility(view.gone); constructbooksfromjsonresponse(s); msearchresultlistview.setadapter(new booksearchlistadapter(getactivity(), mbooks)); } } @override protected void oncancelled() { mprogressbar.setvisibility(view.gone); } private void constructbooksfromjsonresponse(string jsonresponse) { try { jsonobject obj = new jsonobject(new jsontokener(jsonresponse)); jsonarray jsonarray = obj.getjsonarray(book_items_key); (int = 0; < jsonarray.length(); i++) { jsonobject currentobj = jsonarray.getjsonobject(i); string bookid = ""; if (currentobj.has(book_id_key)) { bookid = currentobj.getstring(book_id_key); } jsonobject volumeinfo = currentobj.getjsonobject(book_volumeinfo_key); string booktitle = ""; if (volumeinfo.has(book_title_key)) { booktitle = volumeinfo.getstring(book_title_key); } jsonarray bookauthors = new jsonarray(); if (volumeinfo.has(book_authors_key)) { bookauthors = volumeinfo.getjsonarray(book_authors_key); } arraylist<string> authors = new arraylist<>(); if (bookauthors != null) { (int j = 0; j < bookauthors.length(); j++) { authors.add(bookauthors.getstring(j)); } } string bookdescription = ""; if (volumeinfo.has(book_description_key)) { bookdescription = volumeinfo.getstring(book_description_key); } jsonobject imagelinks = volumeinfo.getjsonobject(book_imagelinks_key); string bookthumbnail = ""; if (imagelinks.has(book_thumbnail_key)) { bookthumbnail = imagelinks.getstring(book_thumbnail_key); } book b = new book(bookid, authors, booktitle, bookdescription, bookthumbnail); mbooks.add(b); } } catch (jsonexception e) { mbooks.clear(); } } } private class thumbnaildownloadertask extends asynctask<string, void, bitmap> { private final weakreference<imageview> imageviewreference; private thumbnaildownloadertask(imageview imageview) { imageviewreference = new weakreference<imageview>(imageview); } @override protected bitmap doinbackground(string... params) { return downloadbitmap(params[0]); } private bitmap downloadbitmap(string param) { httpurlconnection urlconnection = null; try { url url = new url(param); urlconnection = (httpurlconnection) url.openconnection(); int statuscode = urlconnection.getresponsecode(); if (statuscode != httpurlconnection.http_ok) { return null; } inputstream in = urlconnection.getinputstream(); if (in != null) { bitmap bitmap = bitmapfactory.decodestream(in); return bitmap; } } catch (ioexception e) { if (urlconnection != null) { urlconnection.disconnect(); } } { if (urlconnection != null) { urlconnection.disconnect(); } } return null; } @override protected void onpostexecute(bitmap bitmap) { if (iscancelled()) { bitmap = null; } if (imageviewreference != null) { imageview imageview = imageviewreference.get(); if (imageview != null) { if (bitmap != null) { imageview.setvisibility(view.visible); imageview.setimagebitmap(bitmap); } } } } }
and search button triggers search follows:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_addbook, container, false); msearchedittext = (edittext) v.findviewbyid(r.id.book_title_search_field); msearchbutton = (button) v.findviewbyid(r.id.search_button); msearchbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string bookquery = msearchedittext.gettext().tostring(); new searchbooktask().execute(bookquery); } }); return v; }
you try using networkimageview
of volley
library. more info on image cache. find decent tutorial here , here. should make listview
images load correctly.
Comments
Post a Comment