Use of listactivity
A ListView control is integrated in the Listactivity class.
By inheriting the Listactivity class, you can easily use the ListView control
1 Public classclass Name extends Listactivity {2 protected voidonCreate (Bundle savedinstancestate) {3 Super. OnCreate (savedinstancestate);4 //gets the array in the resource file5String [] Data=getresources (). Getstringarray (r.array.city);6Arrayadapter<string> adapter=NewArrayadapter<string> ( This,7Android. R.layout.simple_list_item_multiple_choice,data);//style is a check box8 Setlistadapter (adapter);9ListView Listview=getlistview ();//Gets the ListView object that listactivity comes withTen //to set the selection behavior of a listview: Check One Listview.setchoicemode (listview.choice_mode_multiple); A } -}
part of the code that applies listactivityGrid list GridView (displays information by multiple rows and columns)
Method |
Property |
Describe |
Public GridView (Context context) |
|
Create a GridView Object |
public void Setstretchmode (int stretchmode) |
Android:stretchmode |
Zoom mode |
public void setverticalspacing (int verticalspacing) |
Android:verticalspacing |
Set Vertical Spacing |
public void sethorizontalspacing (int horizontalspacing) |
Android:horizontalspacing |
Set Horizontal Spacing |
public void setnumcolumns (int numcolumns) |
Android:numcolumns |
Sets the amount of data to display per column, and if set to Auto--fit, indicates the automatic setting |
public void setselection (int position) |
|
Set the default check |
public void setgravity (int gravity) |
Android:gravity |
Sets the alignment mode, specified by the Grivity class |
public void Setadapter (ListAdapter adapter) |
|
Set up a picture set |
<LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"><ImageViewAndroid:id= "@+id/img"Android:layout_width= "90DP"Android:layout_height= "100DP"Android:scaletype= "Fitxy"//Set all picture sizes consistent android:padding= "1px"/>//Set four peripheral pitch to 1 pixels</LinearLayout>
ImageView
<LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"> <GridViewAndroid:id= "@+id/gv"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:numcolumns= "3"//Set all picture sizes consistent Android:stretchmode= "ColumnWidth"/>//Set all picture sizes to match</LinearLayout>
GridView
1GridView GV = (GridView) Findviewbyid (R.ID.GV);//Locate the control2field[] fields = r.drawable.class. Getdeclaredfields ();//read the picture at the end of the PNG3list<map<string, integer>> data =NewArraylist<map<string, integer>>();4 for(Field field:fields) {5map<string, integer> map =NewHashmap<string, integer>();6 Try { 7Map.put ("img", Field.getint (r.drawable.class));8 data.add (map);9}Catch(IllegalArgumentException e) {Ten e.printstacktrace (); One}Catch(illegalaccessexception e) { A E.printstacktrace (); - } - } theSimpleadapter adapter =NewSimpleadapter ( This, data, r.layout.gridview0, - Newstring[] {"img"},New int[] {r.id.gimg});//Create an adapter -Gv.setadapter (adapter);//Add GridView Object - +Gv.setonitemclicklistener (NewOnitemclicklistener () { - Public voidOnitemclick (adapterview<?> ADP, view view,intPositionLongArg) { +Map<string, integer> map= (map<string, integer>) adp.getitematposition (position); A intImgid=map.get ("img"); atImageView iv=NewImageView (GridView0. This); - Iv.setscaletype (ImageView.ScaleType.CENTER); - Iv.setimageresource (imgid); - FinalDialog da=NewAlertdialog.builder (GridView0. This). Settitle ("Show Pictures"). Setview (iv). Create (); - NewThread () {//set the thread so that the dialog box closes after 5 seconds - Public voidrun () { in Try { -Thread.Sleep (5000); to}Catch(interruptedexception e) { + E.printstacktrace (); -}finally{ the Da.dismiss (); * } $ }Panax Notoginseng }.start (); - da.show (); the } +});
GridView Section Java code
Advanced Controls "Android 5"--listactivity