android - GridView getChildAt() returns null -


i'm trying view gridview. unfortunately, returns null.

oncreate():

gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.getchildat(3).setbackgroundresource(r.drawable.list_selector_holo_light); 

gridview.getchildcount() returns 0 too!

what can view? know there option change background in adapter, have dynamically.

thanks help!


edit

setadapter:

gridview.setadapter(new imageadapter(this)); 

imageadapter:

public view getview(int position, view convertview, viewgroup parent) {     final imageview imageview;     if (convertview == null) {  // if it's not recycled, initialize attributes         imageview = new imageview(mcontext);         imageview.setlayoutparams(new gridview.layoutparams(85, 85));         imageview.setscaletype(imageview.scaletype.center_crop);         imageview.setpadding(8, 8, 8, 8);     } else {         imageview = (imageview) convertview;     }      imageview.setimageresource(mthumbids[position]);     return imageview; } 

gridview populate adapter during first layout pass. means won't have children in oncreate(). easiest way wait first layout pass use viewtreeobserver (see view.getviewtreeobserver()) register global layout listener. first time listener invoked, grid should contain children.

note if want change background of 1 of children of gridview should adapter. since views recycled background may appear "move" view later on.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -