Sunday, 15 April 2012

java - Why is the ListView empty? -



java - Why is the ListView empty? -

i trying create listview contains only images without texts gives white screen , not getting error or anything.

here codes:

xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightsum="3"> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2"> <listview android:id="@+id/setonelistview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" /> </relativelayout> ......... </linearlayout> </linearlayout>

activity:

public class set1_activity extends activity { listview list; int[] images = { r.drawable.iv1, r.drawable.iv2, r.drawable.iv3, r.drawable.iv4, r.drawable.iv5, r.drawable.iv6, r.drawable.iv11, r.drawable.iv12, r.drawable.iv13, r.drawable.iv14, r.drawable.iv15, r.drawable.iv16, r.drawable.iv111, r.drawable.iv112, r.drawable.iv113, r.drawable.iv114, r.drawable.iv115, r.drawable.iv116, r.drawable.iv7, r.drawable.iv8, r.drawable.iv9, r.drawable.iv19, r.drawable.iv49, r.drawable.iv50, r.drawable.iv52, r.drawable.iv54 }; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_main2); list = (listview) findviewbyid(r.id.setonelistview); heartlessadapter adapter = new heartlessadapter(this, images); list.setadapter(adapter); list.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { switch(position){ case 0: display.setimageresource(r.drawable.iv1); tophone = r.drawable.iv1; break; ......... class heartlessadapter extends arrayadapter<string> { context context; int[] images; heartlessadapter(context c, int imgs[]) { super(c, r.layout.imageview); this.context=c; this.images=imgs; } class myviewholder { imageview myimage; myviewholder(view v) { myimage = (imageview) v.findviewbyid(r.id.imageview); } } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub view row = convertview; myviewholder holder = null; if(row==null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.imageview, parent, false); holder = new myviewholder(row); row.settag(holder); } else { holder = (myviewholder) row.gettag(); } holder.myimage.setimageresource(images[position]); homecoming row; } }

all works fine, no error , emulator show empty white screen nil inside. missing?

thanks!

1) allow heartlessadapter extend arrayadapter<integer> instead of arrayadapter<string>.

2) on constructor, phone call super(c, r.layout.imageview, imgs); should work that.

3) if wanna optimize lot, remove references context , array of images adapter. can item need calling getitem(position) , context calling getcontext().

edit:

in order work, phone call this:

super(c, r.layout.imageview, heartlessadapter.tointegerarray(imgs));

and add together method heartlessadapter:

private static integer[] tointegerarray(int[] array){ integer[] finalarray = new integer[array.length]; (int i=0; i<array.length; i++){ finalarray[i] = array[i]; } homecoming finalarray; }

java android xml listview android-listview

No comments:

Post a Comment