Customizing the adapter in Android

Source: Internet
Author: User

Ah hello, haven't updated the blog for a long time, alas alas, really sorry ah! Beg your pardon!



1. The main method of customizing the list Xiang adapter is:

①public int GetCount () =====> This method is used to control the number of list items

②public view GetView (int position, view Convertview, ViewGroup Parent) ====> This method is used to control the page


2, custom adapter in the actual development of the use of very flexible is also very broad, because this method must be mastered. (although there are ready-made adapters, they are not used in real-world development)


3, in the process of customizing the adapter, you need to create a Baseadapter object, extending the object needs to rewrite the following four methods:

①getcount (): The return value of the method controls how many list items the adapter will contain;

②getitem (int position): The return value of the method determines the contents of the list item at position;

③getitemid (int position): The return value of the method determines the ID of the list item at position;

④getview (int position, View Convertview, ViewGroup parent): The return value of the method determines the list item component of position out.


4, the specific implementation code as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Match_parent "Android: layout_height= "Match_parent" ><listview android:id= "@+id/mylist" android:layout_width= "Match_parent" Android : layout_height= "Match_parent"/></LINEARLAYOUT> 
Package Com.example.baseadaptert;import Android.os.bundle;import Android.view.view;import android.view.ViewGroup; Import Android.widget.adapterview;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.textview;import Android.widget.toast;import Android.widget.adapterview.onitemclicklistener;import Android.app.Activity;import    Android.content.context;public class Baseadaptert extends Activity {private ListView myList;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);                MyList = (ListView) Findviewbyid (r.id.mylist);        Final String names[] = new string[]{"Xiaoming", "Xiaohua", "Xiaomi", "Xiaomei"}; Final int images[] = new int[] {r.drawable.libai, R.drawable.nongyu, R.drawable.qingzhao, r.drawable.tiger};//Fina        l int layoutid = R.layout.item; Mylist.setadaptER (new Baseadapter () {@Overridepublic int getcount () {return names.length;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Activity activity = (activity) baseadapter T.this;//linearlayout out = (linearlayout) activity.getlayoutinflater (). Inflate (position, NULL); LinearLayout out = (linearlayout) activity.getlayoutinflater (). Inflate (R.layout.item, null); ImageView IV = (ImageView) Out.findviewbyid (R.id.imageview); Iv.setimageresource (Images[position]); TextView TV = (TextView) Out.findviewbyid (R.id.textview); Tv.settext (Names[position]); return out;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn 0;}                @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;}}); Mylist.setonitemclicklistener (New Onitemclicklistener () {//Set click Listener @overridepublic void Onitemclick (adapterview< ?> Parent, view view, int position, long id) {Toast.maketext (view.getcontext(), names[position], 1). Show ();}}); }}




Customizing the adapter in Android

Related Article

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.