Gridview for Android

Source: Internet
Author: User

The usage of gridview is basically similar to that of listview.

ProgramLayout file main. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Textview Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/>
<Gridview Android: Id = "@ + ID/gridview01" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</Linearlayout>

The layout file grid_row.xml for each row of the gridview is 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">
<Imageview Android: Id = "@ + ID/imageview01" Android: scaletype = "fitxy"
Android: layout_width = "50dip" Android: layout_height = "50dip"/>
<Textview Android: Id = "@ + ID/tv01" Android: layout_width = "100dip"
Android: layout_height = "wrap_content" Android: textsize = "24dip"
Android: paddingleft = "5dip"/>
<Textview Android: Id = "@ + ID/tv02" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: textsize = "24dip"
Android: paddingleft = "5dip"/>
</Linearlayout>

Configure the adapter of the gridview in the main function:

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Gridview = (gridview) findviewbyid (R. Id. gridview01 );
Simpleadapter adapter = new simpleadapter (this, generatedatalist (),
R. layout. grid_row, new string [] {"col1", "col2", "col3 "},
New int [] {R. Id. imageview01, R. Id. tv01, R. Id. tv02 });
Gridview. setadapter (adapter );
}

Generatedatalist () generates the data in the adapter. Its type is list <? Extends Map <string,?> :

Private list <? Extends Map <string,?> Generatedatalist (){
// Todo auto-generated method stub
Arraylist <Map <string, Object> List = new arraylist <Map <string, Object> ();
Int rowcount = drawableids. length;
For (INT I = 0; I <rowcount; I ++ ){
Hashmap <string, Object> hmap = new hashmap <string, Object> ();
Hmap. Put ("col1", drawableids [I]);
Hmap. Put ("col2", this. getresources (). getstring (nameids [I]);
Hmap. Put ("col3", this. getresources (). getstring (msgids [I]);
List. Add (hmap );
}
Return list;
}

Add an event for the gridview:

Gridview.Setonitemselectedlistener(New onitemselectedlistener (){

@ Override
Public void onitemselected (adapterview <?> Arg0, view arg1,
Int arg2, long arg3 ){
// Todo auto-generated method stub
Textview = (textview) findviewbyid (R. Id. textview01 );
Linearlayout LL = (linearlayout) Arg1 ;
Textview tv01 = (textview) ll. getchildat (1 );
Textview tv02 = (textview) ll. getchildat (2 );
Stringbuilder sb = new stringbuilder ();
SB. append (tv01.gettext ());
SB. append ("");
SB. append (tv02.gettext ());
Textview. settext (sb. tostring ());

}

@ Override
Public void onnothingselected (adapterview <?> Arg0 ){
// Todo auto-generated method stub

}
});
Gridview.Setonitemclicklistener(New onitemclicklistener (){

@ Override
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2,
Long arg3 ){
// Todo auto-generated method stub
Linearlayout LL = (linearlayout) arg1;
Textview tv01 = (textview) ll. getchildat (1 );
Textview tv02 = (textview) ll. getchildat (2 );
Stringbuilder sb = new stringbuilder ();
SB. append (tv01.gettext ());
SB. append ("");
SB. append (tv02.gettext ());
Toast. maketext (mainactivity. This, SB. tostring (),
Toast. length_long). Show ();
}
});

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.