android - Gridview not displaying in fragment -


im trying follow gridview auto resize image example , tried in fragment. here code:

fragmentone

public class fragmentone extends fragment{ gridview gridview;  @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {      view view = inflater.inflate(r.layout.fragment_one, container, false);      gridview = (gridview) view.findviewbyid(r.id.gvlist);     gridview.setadapter(new top_myadapter(getactivity().getapplicationcontext()));       return view; }   private class top_myadapter extends baseadapter {     private list<item> items = new arraylist<item>();     private layoutinflater inflater;       public top_myadapter(context c) {         inflater = layoutinflater.from(c);          items.add(new item("image 1", r.drawable.image_placeone));         items.add(new item("image 2", r.drawable.image_placetwo));         items.add(new item("image 3", r.drawable.image_placethree));         items.add(new item("image 4", r.drawable.image_placefour));         items.add(new item("image 5", r.drawable.image_placefive));     }      @override     public int getcount() {         return items.size();     }      @override     public object getitem(int position) {         return items.get(position);     }      @override     public long getitemid(int position) {         return items.get(position).drawableid;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         view v = convertview;         imageview picture;         textview name;          if(v == null){             v = inflater.inflate(r.layout.gridviewitem, parent, false);             v.settag(r.id.gvitemimage, v.findviewbyid(r.id.gvitemimage));             v.settag(r.id.gvitemtext, v.findviewbyid(r.id.gvitemtext));         }          picture = (imageview) v.gettag(r.id.gvitemimage);         name = (textview) v.gettag(r.id.gvitemtext);          item item = (item) getitem(position);          picture.setimageresource(item.drawableid);         name.settext(item.name);          return v;      }     private class item     {         final string name;         final int drawableid;          item(string name, int drawableid)         {             this.name = name;             this.drawableid = drawableid;         }     }  } 

fragmentone

<framelayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <gridview     android:id = "@+id/gvlist"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:verticalspacing="0dp"     android:horizontalspacing="0dp"     android:stretchmode="spacingwidth"     android:numcolumns="2"/> </framelayout> 

gridviewitem

<framelayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <com.sample.squareimageview     android:id="@+id/gvitemimage"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:scaletype="centercrop"     /> <textview     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:id = "@+id/gvitemtext"     android:paddingleft="10dp"     android:paddingright="10dp"     android:paddingtop="15dp"     android:paddingbottom="15dp"     android:layout_gravity="bottom"     android:textcolor="@android:color/white"     android:background="#55000000"/> </framelayout> 

squareimageview

public class squareimageview extends imageview  {     public squareimageview(context context) {     super(context); }  public squareimageview(context context, attributeset attrs) {     super(context, attrs); }  public squareimageview(context context, attributeset attrs, int defstyle) {     super(context, attrs, defstyle); }  @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {     super.onmeasure(widthmeasurespec, heightmeasurespec);     setmeasureddimension(getmeasuredwidth(), getmeasuredwidth()); //snap width }  } 

the problem here is not displaying in fragment. there's no error it's not displaying, , there scrollbar on rightside ..

there may problem in getview method. looks false implementation of view holder pattern.

we can try without pattern.

can try being sure identify problem.

@override public view getview(int position, view convertview, viewgroup parent) {     convertview = inflater.inflate(r.layout.gridviewitem, parent, false);      imageview image = (imageview) convertview.findviewbyid(r.id.gvitemimage);     textview text = (textview) convertview.findviewbyid(r.id.gvitemtext);      image.setimageresource(getitem(position).drawableid);     text.settext(getitem(position).name);      return convertview; }   static class item {     final string name;     final int drawableid;      item(string name, int drawableid)     {         this.name = name;         this.drawableid = drawableid;     } } 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -