android - Why image is not displayed into list view? -
this code image not displayed list view how can solve problem?i set uri value bitmap.so problem?changed array , error solved bt still image not displayed list view. 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,bitmap,bitmap,bitmap,bitmap,bitmap,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; } } }
customadapter.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]); if(result != null && result.length > position) { holder.tv.settext(result[position]); } if(imageid != null && imageid.length > 0) { holder.img.setimagebitmap(imageid[0]); } 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; } }
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="25dp" android:textstyle="bold" android:text=" theaters..." /> <listview android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" > </listview> </linearlayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/imageview1" android:layout_gravity="center" android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/ic_launcher" /> <textview android:id="@+id/textview1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="25dp" android:text="textview" /> </linearlayout>
it happens because before bitmaps loaded , saved array, call adapter. downloading images takes time , adapter not waiting them. see example, using image loader: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Comments
Post a Comment