android - How to solve array out of bound exception? -
this code , getting array out of bound exception.how solve error?i used bitmap object instead of string .when store bitmap image array list got error.
mainactivity.java
public class mainactivity extends activity { listview lv; context context; arraylist prgmname; bitmap bitmap; //bitmap=getbitmapfromurl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg"); public static string [] prgmnamelist={"let c","c++","java","jsp","microsoft .net","android","php","jquery","javascript"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bitmap=getbitmapfromurl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg"); context=this; bitmap [] prgmimages ={bitmap,bitmap}; lv=(listview) findviewbyid(r.id.listview); lv.setadapter(new customadapter(this, prgmnamelist,prgmimages)); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } private bitmap getbitmapfromurl(string src) { // todo auto-generated method stub try { url url =new url(src); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input=connection.getinputstream(); bitmap mybibitmap=bitmapfactory.decodestream(input); return mybibitmap; } catch (exception e) { // todo: handle exception e.printstacktrace(); return null; } } }
customeradapter.java
public class customadapter extends baseadapter{ string [] result; context context; bitmap[] imageid; private static layoutinflater inflater=null; public customadapter(mainactivity mainactivity, string[] prgmnamelist, bitmap[] prgmimages) { // todo auto-generated constructor stub result=prgmnamelist; context=mainactivity; imageid=prgmimages; inflater = ( layoutinflater )context. getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return result.length; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } public class holder { textview tv; imageview img; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub holder holder=new holder(); view rowview; rowview = inflater.inflate(r.layout.list_item, null); holder.tv=(textview) rowview.findviewbyid(r.id.textview1); holder.img=(imageview) rowview.findviewbyid(r.id.imageview1); holder.tv.settext(result[position]); holder.img.setimagebitmap(imageid[position]); rowview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub toast.maketext(context, "you clicked "+result[position], toast.length_long).show(); } }); return rowview; } }
you getting arrayindexoutofbound because imageid don't have same array size of result.
you can see result array has 9 element , imageid has 2 element, , both element same, why using array, pass simple bitmap instead of array.
your result array:
public static string [] prgmnamelist={"let c","c++","java","jsp","microsoft .net","android","php","jquery","javascript"};
your imageid array:
bitmap [] prgmimages ={bitmap,bitmap};
solution :
1) if want display different images row item pass imageid array same size of result array. 2) if want display same image in row item pass single bitmap instead of array.
Comments
Post a Comment