Android GridView example (1)

Source: Internet
Author: User
1 The main. xml file is as follows <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/linearlayout" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "vertical"> <textview Android: Id = "@ + ID/textview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "@ string/tip" Android: textcolor = "@ color/White" Android: textsize = "24dip"> </Textview> <gridview Android: Id = "@ + ID/gridview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: verticalspacing = "5dip" Android: horizontalspacing = "5dip" Android: stretchmode = "columnwidth"> </gridview> </linearlayout>: the overall layout is a linear vertical layout, A textview followed by a gridview, which is similar to listview 2 grid_row.xml as follows: <? 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 = "match_parent" Android: Orientation = "horizontal" Android: id = "@ + ID/linerlayout_grid_row"> <imageview Android: Id = "@ + ID/imageview_grid_row" Android: scaletype = "fitxy" Android: layout_width = "100dip" Android: layout_height = "98dip"> </imageview> <textview Android: Id = "@ + ID/textview _ Grid_row "Android: layout_width =" 140dip "Android: layout_height =" wrap_content "Android: textcolor =" @ color/White "Android: textsize =" 24dip "Android: paddingleft = "5dip"> </textview> </linearlayout> note: the overall layout is a horizontal linear layout. The first is an imageview, on the right is a textview 3 activity package cn.com. baidu; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. activity; import Android. OS. bundle; import android. view. view; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. adapterview. onitemselectedlistener; import android. widget. gridview; import android. widget. linearlayout; import android. widget. simpleadapter; import android. widget. textview; // Note: // 1 The data binding between the gridview and listview is very similar. in hashmap. put ("col1", drawableids [I ]); In this hashmap, // puts the image ID (that is, the image ID generated in the r file), rather than the real image data. when binding, the system automatically loads the corresponding image based on this ID // 2 method new simpleadapter (this, createdatalist (), R. layout. grid_row, new string [] {"col1", "col2"}, new int [] {R. id. imageview_grid_row, R. id. textview_grid_row}); // gridview. setadapter (simpleadapter); the parameters of this method are basically the same as those of the adapter parameter when using listview // The first parameter: context object // The second parameter: the data to be bound (list) // The third parameter: receives the layout of the bound data // The fourth parameter: the Key of the bound data // The Fifth parameter: which of the following controls is bound to the layout? // the second and the third Parameters are described from a macro perspective. The fourth and fifth parameters are further described from a micro perspective. // 3 in this example, the listener obtains the focus and the processing status of the clicked parameters. this is a reference for public class testgridviewactivity extends activity {private gridview; private arraylist <Map <string, Object> arraylist; private textview; int drawableids [] = {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e}; int jjs [] = {R. string. JJA, R. string. jjb, R. string. JJC, R. string. jjd, R. string. jje}; public list creat Edatalist () {arraylist = new arraylist <Map <string, Object> (); For (INT I = 0; I <jjs. length; I ++) {hashmap <string, Object> hashmap = new hashmap <string, Object> (); hashmap. put ("col1", drawableids [I]); // The first column is the photo hashmap. put ("col2", this. getstring (jjs [I]); // The second column is named arraylist. add (hashmap);} return arraylist;} @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gridview = (gridview) findviewbyid (R. id. gridview); simpleadapter = new simpleadapter (this, createdatalist (), R. layout. grid_row, new string [] {"col1", "col2"}, new int [] {R. id. imageview_grid_row, R. id. textview_grid_row}); gridview. setadapter (simpleadapter); // set the adapter gridview for the gridview. setonitemselectedlistener (New onitemselectedlistener () {// processing when the entry gets the focus @ overridepublic void oni Temselected (adapterview <?> Parent, view, int position, long ID) {textview = (textview) findviewbyid (R. id. textview); linearlayout = (linearlayout) view; // The entry that obtains the focus. each entry is a linearlayouttextview childtextview = (textview) linearlayout. getchildat (1 );//. obtain the name displayed by textview in this entry. it starts from 0, so it is 1textview. settext (childtextview. gettext (). tostring (); // then display it in another (that is, the main textview)} @ overridepublic void onnothingselected (adapterview <?> Parent) {}}); gridview. setonitemclicklistener (New onitemclicklistener () {// processing when an entry is clicked. Same as above. @ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {textview = (textview) findviewbyid (R. id. textview); linearlayout = (linearlayout) view; textview childtextview = (textview) linearlayout. getchildat (1 );//??? Textview. settext (childtextview. gettext (). tostring ());}});}}

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.