Android 13th Lesson--baseadapter

Source: Internet
Author: User

Baseadapter It takes four methods to create such an object. int GetCount (); Object getItem (int position); Long Getitemid (int position); View GetView (int position, view Convertview, ViewGroup parent), (1) The number of items in the list (2) returns the list contents of the value (3) Gets the ID of the list item at postion (4) The component in the list item

 PackageCom.example.baseadapter;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {ListView mylist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); MyList=(ListView) Findviewbyid (r.id.mylist); Baseadapter Adapter=NewBaseadapter () {@Override Public intGetCount () {return40; } @Override PublicObject GetItem (intposition) {                return NULL; } @Override Public LongGetitemid (intposition) {                returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {                //Create a LinearLayout and add two components to itLinearLayout line=NewLinearLayout (mainactivity. This); Line.setorientation (0); ImageView Image=NewImageView (mainactivity. This);                Image.setimageresource (R.drawable.ic_launcher); TextView TextView=NewTextView (mainactivity. This); Textview.settext ("First" + (position+1) + "list items"); Textview.settextsize (20);                Textview.settextcolor (color.red);                Line.addview (image);                                Line.addview (TextView); //Returning LinearLayout instances                returnLine ;        }                                };    Mylist.setadapter (adapter); }    }
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">   <ListViewAndroid:id= "@+id/mylist"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"     /></LinearLayout>

Baseadapter is implemented by writing its own default method to implement the ListView

So his part of the layout is not written in XML, but in the activity.

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.