Gridview table control,
Gridview Table Control
:
Analysis:
Similar to ListvVew, it is used to bind data to the control through the adapter.
The procedure is as follows:
1. Create a GridView control and specify the number of Columns
Android: numColumns = "3"
2. Create a data container that displays data items. It is a Lauout file with an ImageView and a TextView.
The above is ImageView, and "10" is displayed in TextView.
3. Create data. Here we use List to implement it.
Private List <HashMap <String, Object> mData;
MData = new ArrayList <HashMap <String, Object> ();
Then create a HashMap
HashMap <String, Object> map = new HashMap <String, Object> ();
Map. put ("txt", "" + I );
Map. put ("image", images [I]);
MData. add (map );
4. Create a data adapter and bind the data to the data adapter
Private SimpleAdapter adapter;
Adapter = new SimpleAdapter (this, mData, R. layout. itemview1, new String [] {"txt", "image"}, new int [] {R. id. textView1, R. id. iv_avator });
5. Set a data adapter for the GridView
Gridview. setAdapter (adapter );
Code:
Fry. Activity01
1 package fry; 2 3 import java. util. arrayList; 4 import java. util. hashMap; 5 import java. util. list; 6 import java. util. map; 7 8 import com. example. gridView. r; 9 10 import android. app. activity; 11 import android. OS. bundle; 12 import android. widget. gridView; 13 import android. widget. simpleAdapter; 14 15 public class Activity01 extends Activity {16 private List <HashMap <String, Object> mData; 17 private GridView; 18 private SimpleAdapter adapter; 19 private int [] images = new int [] {20 R. drawable. image1, R. drawable. image2, R. drawable. image3, 21 R. drawable. image5, R. drawable. image6, R. drawable. image7, 22 R. drawable. image8, R. drawable. image9, R. drawable. image10, 23 R. drawable. image11, R. drawable. image12, R. drawable. image13, 24 R. drawable. image14, R. drawable. image425}; 26 27 @ Override28 protected void onCreate (Bundle savedInstanceState) {29 // TODO Auto-generated method stub30 super. onCreate (savedInstanceState); 31 setContentView (R. layout. activity01); 32 gridview = (GridView) findViewById (R. id. gridView1); 33 34 mData = new ArrayList <HashMap <String, Object> (); 35 for (int I = 0; I <images. length; I ++) {36 HashMap <String, Object> map = new HashMap <String, Object> (); 37 map. put ("txt", "" + I); 38 map. put ("image", images [I]); 39 mData. add (map); 40} 41 42 adapter = new SimpleAdapter (this, mData, R. layout. itemview1, 43 new String [] {"txt", "image"}, new int [] {R. id. textView1, R. id. iv_avator}); 44 45 gridview. setAdapter (adapter); 46} 47}Main Interface
/GridView/res/layout/activity01.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <GridView 8 android:id="@+id/gridView1" 9 android:layout_width="match_parent"10 android:layout_height="match_parent"11 android:numColumns="3"12 ></GridView>13 14 </LinearLayout>
Create a GridView Control
/GridView/res/layout/itemview1.xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: gravity = "center_horizontal" 6 android: orientation = "vertical"> 7 8 <ImageView 9 android: id = "@ + id/iv_avator" 10 android: layout_width = "wrap_content" 11 android: layout_height = "wrap_content" 12 android: src = "@ drawable/image1"/> 13 14 <TextView 15 android: id = "@ + id/textView1" 16 android: layout_width = "wrap_content" 17 android: layout_height = "wrap_content" 18 android: text = "" 19/> 20 21 </LinearLayout>
Create a data item container