Android Study Notes (7)-add images and titles for the use of the gridview

Source: Internet
Author: User

1. First, let's look at the layout file.

<?xml version="1.0" encoding="utf-8"?><GridView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/gridview"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:columnWidth="170dp"    android:gravity="center"    android:horizontalSpacing="30dp"     android:numColumns="auto_fit"    android:paddingBottom="20dp"    android:paddingLeft="20dp"    android:paddingRight="20dp"    android:paddingTop="20dp"    android:stretchMode="columnWidth"    android:verticalSpacing="20dp" />

2. inherit a baseadapter to write a pictureadapter

Class pictureadapter extends baseadapter {private layoutinflater Inflater; private list <picture> pictures; // constructor public pictureadapter (string [] titles, int [] images, context) {super (); pictures = new arraylist <picture> (); Inflater = layoutinflater. from (context); For (INT I = 0; I <images. length; I ++) {picture = new picture (Titles [I], images [I]); pictures. add (picture) ;}@overridepublic I NT getcount () {// todo auto-generated method stubif (null! = Pictures) {return pictures. size () ;}else {return 0 ;}@ overridepublic object getitem (INT position) {// todo auto-generated method stubsystem. out. println ("--" + position); Return pictures. get (position) ;}@ overridepublic long getitemid (INT position) {// todo auto-generated method stubsystem. out. println ("-- 1 ---" + position); Return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {// todo auto-generated method stubviewholder viewholder; if (convertview = NULL) {convertview = Inflater. inflate (R. layout. picture_item, null); viewholder = new viewholder (); viewholder. title = (textview) convertview. findviewbyid (R. id. title); viewholder. image = (imageview) convertview. findviewbyid (R. id. image); convertview. settag (viewholder);} else {viewholder = (viewholder) convertview. gettag ();} viewholder. title. settext (pictures. get (position ). gettitle (); viewholder. image. setimageresource (pictures. get (position ). getimageid (); Return convertview;} class viewholder {public textview title; Public imageview image;} class picture {private String title; private int imageid; Public picture () {super () ;}public picture (String title, int imageid) {super (); this. title = title; this. imageid = imageid;} Public String gettitle () {return title;} public void settitle (String title) {This. title = title;} public int getimageid () {return imageid;} public void setimageid () {This. imageid = imageid ;}}

3. R. layout. picture_item layout File

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/root"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginTop="5dp"    android:orientation="vertical" >    <ImageView        android:id="@+id/image"        android:layout_width="120dp"        android:layout_height="120dp"        android:layout_gravity="center"        android:padding="4dp"        android:scaleType="fitXY" />    <TextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center_horizontal"         android:textColor="#ff501e58"        android:textSize="22dip"/></LinearLayout>

4. Variable Declaration

private String[] titles;private GridView gridView;private int[] images;

5. Fill in data and call

The following operations assign values to the titles and images arrays first.

Gridview = (gridview) findviewbyid (R. id. gridview); pictureadapter adapter = new pictureadapter (titles, images, this); gridview. setadapter (adapter); gridview. setonitemclicklistener (New onitemclicklistener () {public void onitemclick (adapterview <?> Parent, view V, int position, long ID) {// click the corresponding item ...}});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.