View-baseadapter and simplecursoradapter of listview and Code view of grid View

Source: Internet
Author: User

I. baseadapter

1. Data Source list <Object>

It is generally an entity class or a pojo class. It generally only contains the getter and setter methods of private attributes and attributes.

2. Layout: You can use a layout that contains multiple views.

3. abstract class: Four abstract methods must be inherited and implemented.

1-int getcount () returns the number of records of the data source

2-object getitem (INT position) returns the data object corresponding to the index

3-long getitemid (INT position) returns the ID of the data object corresponding to the index

4-view getview (INT position, view convertview, viewgroup parent) is used to adapt data to the view. The returned view with data loaded is convertview, which indicates reusable view.

Ii. simplecursoradapter

Data source: the database query result set cursor (cursor ).

3. gridview grid view: listview with Grid

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:tools="http://schemas.android.com/tools" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:paddingBottom="@dimen/activity_vertical_margin" 7     android:paddingLeft="@dimen/activity_horizontal_margin" 8     android:paddingRight="@dimen/activity_horizontal_margin" 9     android:paddingTop="@dimen/activity_vertical_margin"10     tools:context="com.example.wang.testapp2.TestActivity10">11 12     <GridView13         android:layout_width="match_parent"14         android:layout_height="match_parent"15         android:id="@+id/gv_1"16         android:numColumns="4"17         android:stretchMode="columnWidth"18         android:gravity="center"19         android:horizontalSpacing="30dp"20         ></GridView>21 22 </LinearLayout>
, XML
1 package COM. example. wang. testapp2; 2 3 Import android. app. actionbar; 4 Import android. support. v7.app. appcompatactivity; 5 import android. OS. bundle; 6 Import android. view. view; 7 Import android. view. viewgroup; 8 Import android. widget. abslistview; 9 Import android. widget. baseadapter; 10 Import android. widget. gridview; 11 import android. widget. imageview; 12 13 Import Java. util. arraylist; 14 Import Java. util. list; 15 16 public class testactivity10 extends appcompatactivity {17 18 gridview gv_1; 19 20 list <integer> Liv; 21 22 @ override23 protected void oncreate (bundle savedinstancestate) {24 super. oncreate (savedinstancestate); 25 setcontentview (R. layout. activity_test10); 26 27 gridview gv_1 = (gridview) findviewbyid (R. id. gv_1); 28 29 // id30 Liv = new arraylist of 1 data image <> (); 31 Liv. add (R. drawable. f1); 32 Liv. add (R. drawable. f2); 33 Liv. add (R. drawable. f3); 34 Liv. add (R. drawable. f4); 35 Liv. add (R. drawable. f5); 36 Liv. add (R. drawable. f6); 37 Liv. add (R. drawable. f7); 38 Liv. add (R. drawable. f8); 39 Liv. add (R. drawable. f9); 40 Liv. add (R. drawable. f10); 41 42 gv_1.setadapter (New imageadapter (); 43 44} 45 46 // adapter 47 48 class imageadapter extends baseadapter49 {50 @ override51 public int getcount () {52 return Liv. size (); 53} 54 55 @ override56 public object getitem (INT position) {57 Return Liv. get (position); 58} 59 60 @ override61 public long getitemid (INT position) {62 Return 0; 63} 64 65 @ override66 public view getview (INT position, view convertview, viewgroup parent) {67 68 // 1. the ID69 integer ivid = Liv. get (position); 70 71 // 2. prepare view72 if (convertview = NULL) 73 {74 convertview = new imageview (testactivity10.this); 75} 76 77 // 3. adapt to 78 // convert 79 imageview = (imageview) convertview; 80 81 imageview. setimageresource (ivid); 82 83 // imageview. setmaxwidth (70); 84 // imageview. setmaxheight (70); 85 86 imageview. setlayoutparams (New gridview. layoutparams (110,110); 87 88 89 return imageview; 90} 91} 92}
. Java

View-baseadapter and simplecursoradapter of listview and Code view of grid View

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.