Android to GridView

Source: Internet
Author: User


GridView: Grid list, also supports adapters.

 Packagecom.sxt.day05_01;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.AdapterView.OnItemLongClickListener;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageView;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;Importcom.sxt.day05_01.entity. Generalbean; Public classMainactivityextendsActivity {GridView mgvgeneral;//the V layer of MVC,List<generalbean> mgenerals;//The m layer of MVC, representing a collection of 10 strategistsGeneraladapter Madapter;//the C-layer of MVC,    int[] resid={R.drawable.baiqi,r.drawable.caocao,r.drawable.chengjisihan, r.drawable.hanxin,r.drawable.lishimin,r.dr    Awable.nuerhachi, R.drawable.sunbin,r.drawable.sunwu,r.drawable.yuefei, R.drawable.zhuyuanzhang}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); InitData ();//Initializing DataInitview ();    Setlistener (); }        Private voidSetlistener () {Setonitemclicklistener ();            Setonitemlongclicklistener (); }    Private voidSetonitemlongclicklistener () {Mgvgeneral.setonitemlongclicklistener (NewOnitemlongclicklistener () {@Override Public BooleanOnitemlongclick (adapterview<?>Parent, view view,intPositionLongID) {toast.maketext (mainactivity. This, Mgenerals.get (position). GetName () + "Be Long pressed", 2000). Show (); return true;    }        }); }    Private voidSetonitemclicklistener () {Mgvgeneral.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {toast.maketext (mainactivity. This, Mgenerals.get (position). GetName () + "Short Press", 2000). Show ();    }        }); }    Private voidInitview () {mgvgeneral= (GridView) Findviewbyid (r.id.gvgeneral);//Get GvgeneralMadapter=NewGeneraladapter ();//Create an adapterMgvgeneral.setadapter (Madapter);//setting up an adapter for gvgeneral    }    Private voidInitData () {//converts the number of string groups in a resource to a Java arrayString[] Names=getresources (). Getstringarray (R.array.generals); Mgenerals=NewArraylist<generalbean>();  for(inti = 0; i < names.length; i++) {Generalbean Bean=NewGeneralbean (Resid[i], names[i]);        Mgenerals.add (Bean); }    }    //Adapter    classGeneraladapterextendsbaseadapter{@Override Public intGetCount () {returnmgenerals.size (); } @Override PublicGeneralbean GetItem (intposition) {            returnMgenerals.get (position); } @Override Public LongGetitemid (intposition) {            returnposition; } @Override//get a line on the V layer         PublicView GetView (intposition, View Convertview, ViewGroup parent) {            //inflate can be used to find layout controls defined in an XML, get a row of layouts Item_generals.xml and convert to objects of the view type            /*<?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=" Wrap_content "                    android:orientation= "vertical" > <imageview android:id= "@+id/ivthumb" Android:layout_width= "80DP" android:layout_height= "80DP" Android:scaletype = "Fitxy" android:src= "@drawable/baiqi"/> <textview android:id=                    "@+id/tvname" android:layout_width= "80DP" android:layout_height= "Wrap_content" android:text= "White" android:textsize= "20sp" android:gravity= "Center_hori Zontal "/> </LinearLayout>*/            //View layout=view.inflate (mainactivity. This, R.layout.item_generals,NULL); //Set the rowImageView ivthumb=(ImageView) Layout.findviewbyid (R.ID.IVTHUMB); TextView Tvname=(TextView) Layout.findviewbyid (r.id.tvname); Generalbean Bean=Mgenerals.get (position);            Ivthumb.setimageresource (Bean.getresid ());            Tvname.settext (Bean.getname ()); returnLayout//returns a row of view that is Item_generals.xml linearlayout        }    }}

Main.xml

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    <GridViewAndroid:id= "@+id/gvgeneral"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:columnwidth= "60DP"android:horizontalspacing= "2DP"android:verticalspacing= "5DP"Android:numcolumns= "Auto_fit"/></Relativelayout>
Item_generals.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >    <ImageViewAndroid:id= "@+id/ivthumb"Android:layout_width= "80DP"Android:layout_height= "80DP"Android:scaletype= "Fitxy"android:src= "@drawable/baiqi"/>    <TextViewAndroid:id= "@+id/tvname"Android:layout_width= "80DP"Android:layout_height= "Wrap_content"Android:text= "White Up"android:textsize= "20SP"android:gravity= "Center_horizontal"/></LinearLayout>

Generalbean.java

 Public class Generalbean {    privateint resid;   The ID value of the picture is    private String name;  Military Strategist's name public    int  getresid ()        {return resid;    }

Android to GridView

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.