i've tried build app on top of opensource project. basic function use gridview show couple of images. following code snippet. comment line original code. in original code, images.imagethumburls
string array of urls , size(images.imagethumburls.length
) 99. i'd replace original urls own version , did. there error. please see log.
according log, seems limit getview
99(the original code). aside changing getcount
, there else need do?
in createview
method:
mgridview.getviewtreeobserver().addongloballayoutlistener( new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { if (madapter.getnumcolumns() == 0) { final int numcolumns = (int) math.floor( mgridview.getwidth() / (mimagethumbsize + mimagethumbspacing)); if (numcolumns > 0) { final int columnwidth = (mgridview.getwidth() / numcolumns) - mimagethumbspacing; madapter.setnumcolumns(numcolumns); madapter.setitemheight(columnwidth); if (buildconfig.debug) { log.d(tag, "oncreateview - numcolumns set " + numcolumns); } } } } });
imageadapter class:
private class imageadapter extends baseadapter { public int getcount() { //return images.imagethumburls.length + mnumcolumns; return datastore.photosinfooftag.total + mnumcolumns; } @override public view getview(int position, view convertview, viewgroup container) { // first check if top row if (position < mnumcolumns) { if (convertview == null) { convertview = new view(mcontext); } // set empty view height of actionbar convertview.setlayoutparams(new abslistview.layoutparams( viewgroup.layoutparams.match_parent, mactionbarheight)); return convertview; } // handle main imageview thumbnails imageview imageview; if (convertview == null) { // if it's not recycled, instantiate , initialize imageview = new recyclingimageview(mcontext); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setlayoutparams(mimageviewlayoutparams); } else { // otherwise re-use converted view imageview = (imageview) convertview; } // check height matches our calculated column width if (imageview.getlayoutparams().height != mitemheight) { imageview.setlayoutparams(mimageviewlayoutparams); } // load image asynchronously imageview, takes care of // setting placeholder image while background thread runs photo photo = datastore.photosinfooftag.photo.get(position - mnumcolumns); string photourl = datastore.imageurlpart1 + integer.tostring(photo.getfarm()) + datastore.imageurlpart2 + photo.getserver() + "/" + photo.getid() + "_" + photo.getsecret() + datastore.imageurlpart3; mimagefetcher.loadimage(photourl, imageview); //mimagefetcher.loadimage(images.imagethumburls[position - mnumcolumns], imageview); return imageview; } public void setitemheight(int height) { if (height == mitemheight) { return; } mitemheight = height; mimageviewlayoutparams = new gridview.layoutparams(layoutparams.match_parent, mitemheight); mimagefetcher.setimagesize(height); notifydatasetchanged(); } public void setnumcolumns(int numcolumns) { mnumcolumns = numcolumns; } }
05-06 01:04:39.736: e/androidruntime(16356): fatal exception: main
05-06 01:04:39.736: e/androidruntime(16356):
java.lang.indexoutofboundsexception: invalid index 100, size 10005-06 01:04:39.736: e/androidruntime(16356): @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:251)
05-06 01:04:39.736: e/androidruntime(16356): @ java.util.arraylist.get(arraylist.java:304)
05-06 01:04:39.736: e/androidruntime(16356): @ com.example.android.bitmapfun.ui.imagegridfragment$imageadapter.getview(imagegridfragment.java:292)
05-06 01:04:39.736: e/androidruntime(16356): @ android.widget.abslistview.obtainview(abslistview.java:2143)
05-06 01:04:39.736: e/androidruntime(16356): @ android.widget.gridview.makeandaddview(gridview.java:1341)
05-06 01:04:39.736: e/androidruntime(16356): @ android.widget.gridview.makerow(gridview.java:341)
05-06 01:04:39.736: e/androidruntime(16356): @ android.widget.gridview.filldown(gridview.java:283)
05-06 01:04:39.736: e/androidruntime(16356): @ android.widget.gridview.fillgap(gridview.java:243)
05-06 01:04:39.736: e/androidruntime(16356): @ android.widget.abslistview.trackmotionscroll(abslistview.java:4930)
05-06 01:04:39.736: e/androidruntime(16356): @ android.widget.abslistview$flingrunnable.run(abslistview.java:4087)
05-06 01:04:39.736: e/androidruntime(16356): @ android.view.choreographer$callbackrecord.run(choreographer.java:749)
05-06 01:04:39.736: e/androidruntime(16356): @ android.view.choreographer.docallbacks(choreographer.java:562)
05-06 01:04:39.736: e/androidruntime(16356): @ android.view.choreographer.doframe(choreographer.java:531)
05-06 01:04:39.736: e/androidruntime(16356): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735)
05-06 01:04:39.736: e/androidruntime(16356): @ android.os.handler.handlecallback(handler.java:725)
05-06 01:04:39.736: e/androidruntime(16356): @ android.os.handler.dispatchmessage(handler.java:92)
05-06 01:04:39.736: e/androidruntime(16356): @ android.os.looper.loop(looper.java:137)
05-06 01:04:39.736: e/androidruntime(16356): @ android.app.activitythread.main(activitythread.java:5041)
05-06 01:04:39.736: e/androidruntime(16356): @ java.lang.reflect.method.invokenative(native method)
05-06 01:04:39.736: e/androidruntime(16356): @ java.lang.reflect.method.invoke(method.java:511)
05-06 01:04:39.736: e/androidruntime(16356): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793)
05-06 01:04:39.736: e/androidruntime(16356): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560)
05-06 01:04:39.736: e/androidruntime(16356): @ dalvik.system.nativestart.main(native method)
be carefull, when returning value on getcount() greater number of items @ adapter (usually fill cells) need check if current bounds never bypassed, , if return empty view (or 1 background).
your code:
public int getcount() { //return images.imagethumburls.length + mnumcolumns; return datastore.photosinfooftag.total + mnumcolumns; }
should return datastore.photosinfooftag.total
also, not use static things this, should use list instead data of adapter.
Comments
Post a Comment