The example of this article analyzes the use of GridView and Arrayadapter in Android. Share to everyone for your reference, specific as follows:
The GridView is a tabular two-dimensional layout view, when the GridView text will appear scrolling effect, the GridView element named item, to put the item into the GridView, need Arrayadapter object.
Examples are as follows:
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.Button;
Import Android.widget.GridView;
Import Android.widget.TextView; public class A06activity extends activity {private TextView TV;//Two buttons are dynamically placed in the GridView switch, B01 is set the GridView to two-column format, and put 4//item
, B02 set to three column format, and put 9 item private Button b01,b02;//private GridView GV;
Private string[] S1,S2;
Private arrayadapter<string> AA; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
tv= (TextView) Findviewbyid (r.id.tv);
b01= (Button) Findviewbyid (R.ID.BUTTON01);
B02= (Button) Findviewbyid (R.ID.BUTTON02);
gv= (GridView) Findviewbyid (R.ID.GV); S1=new string[]{getresources(). getString (r.string.str_list01), Getresources (). getString (r.string.str_list02), Getresources (). getString (R.str
ing.str_list03), Getresources (). getString (r.string.str_list04)};
S2=new string[]{getresources (). getString (r.string.str_list01), Getresources (). getString (r.string.str_list02), Getresources (). getString (r.string.str_list03), Getresources (). getString (r.string.str_list04), getresources () . getString (r.string.str_list01), Getresources (). getString (r.string.str_list02), Getresources (). getString (R.strin
g.str_list03), Getresources (). getString (r.string.str_list04), Getresources (). getString (r.string.str_list04)
}; B01.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method
Stub gv.setnumcolumns (2);
Aa=new arrayadapter<string> (A06ACTIVITY.THIS,R.LAYOUT.LIST_ITEM_L_SMALL,S1);
Gv.setadapter (AA);
Gv.setselection (2); Gv.refreshdrawablestate ();
}
}); B02.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method
Stub gv.setnumcolumns (2);
Aa=new arrayadapter<string> (A06ACTIVITY.THIS,R.LAYOUT.LIST_ITEM_L_SMALL,S2);
Gv.setadapter (AA);
}
});
Use the Onclicklistener () method of the GridView to capture which item gv.setonitemclicklistener the user clicks (new Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) {//To determine the number of elements in the adapter that are clicked
Name Switch (Aa.getcount ()) {case 4:tv.settext (s1[arg2]);
Break
Case 9:tv.settext (S2[ARG2]);
Break
}
}
});
}
}
Res/layout/main.xml is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<textview
android:id= "@+id/tv" android:layout_width= "Fill_parent"
android:layout_height= "wrap_content"
android:text= "@string/hello"/>
<button
android: Id= "@+id/button01"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
/ >
<button
android:id= "@+id/button02"
android:layout_width= "Fill_parent"
android: layout_height= "Wrap_content"
/>
<gridview
android:id= "@+id/gv"
android:layout_width= "Fill_parent"
android:layout_height= "wrap_content"
></GridView>
</LinearLayout>
The Res/layout/list_item_l_small function is to configure the Arrayadapter's second parameter Textviewresourceid, which must be configured for TextView. If you need to change the text mode into item, you can use this to set the size of the text, the mode of the property settings, and once placed in the Arrayadapter, this configuration will take effect immediately. You can also set up a lot of layout XML to be used by different GridView, so that each GridView has a different style.
<?xml version= "1.0" encoding= "Utf-8"?> <textview xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:id=" @+id/mycheckedtextview1 "
android:layout_width=" Match_parent "
android: layout_height= "Match_parent"
android:textcolor= "@drawable/blue"
android:textsize= "12DP"
android: Textappearance= "@android: Attr/textappearancelarge"
android:gravity= "center_vertical"
android: paddingleft= "6dip"
android:minheight= "@android: Attr/listpreferreditemheight"
/>
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Basic Components Usage Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.