The Baseadapter of Android development

Source: Internet
Author: User

Baseadapter can be the most flexible to implement the ListView, the inside of the control can also get focus, allow the Click. Examples are as follows:

Main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" ><ListViewAndroid:id= "@+id/mylist"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"/></LinearLayout>

Main method:

 Public classBaseadaptertestextendsactivity{ListView myList; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); MyList=(ListView) Findviewbyid (r.id.mylist); Baseadapter Adapter=NewBaseadapter () {@Override Public intGetCount () {//specify a total of 40 options                return40; } @Override PublicObject GetItem (intposition) {                return NULL; }            //override this method, and the return value of the method will be the ID of the list item@Override Public LongGetitemid (intposition) {                returnposition; }            //override this method, and the view returned by the method will be used as a list box@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {                //Create a LinearLayout and add 2 components to itLinearLayout line =NewLinearLayout (baseadaptertest. This); Line.setorientation (0); ImageView Image=NewImageView (baseadaptertest. This);                Image.setimageresource (R.drawable.ic_launcher); TextView text=NewTextView (baseadaptertest. This); Text.settext ("First" + (position +1) + "list items"); Text.settextsize (20);                Text.settextcolor (color.red);                Line.addview (image);                Line.addview (text); //Returning LinearLayout instances                returnLine ;        }        };    Mylist.setadapter (adapter); }}

The effect is as follows:

The Baseadapter of Android development

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.