Some records of Android adapter

Source: Internet
Author: User

I. Summary

  An Adapter object acts as a bridge between AdapterView an And the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a for each item in the View data set.

This is the official Android definition of adapter. This relationship can be described as the following diagram,

  

Ii. the inheritance relationship of adapter

Three, examples

Attached here is an example of the "Android programming Treasure" of the Beihang University, paired with a ListView to show how adapter plays the middleman and provides data for the view layer.

1, the definition of 3 layout files, respectively, is activity_main.xml,fragment_main.xml,my_listitem.xml.

Activity_main.xml

<Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/container"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "So.byj.example.mytestapp.MainActivity"Tools:ignore= "Mergerootframe" />

Fragment_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/linearlayout01"android:baselinealigned= "false"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <ListViewAndroid:id= "@+id/mylistview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content">    </ListView>    </LinearLayout>

My_listitem.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/mylistitem"Android:paddingbottom= "3dip"Android:paddingleft= "10dip"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical" >        <TextViewAndroid:id= "@+id/itemtitle"Android:layout_width= "Wrap_content"Android:layout_height= "Fill_parent"android:textsize= "30SP" />        <TextViewAndroid:id= "@+id/itemtext"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/></LinearLayout>

2, write mainactivity, the code is as follows

 PackageSo.byj.example.mytestapp;Importjava.util.ArrayList;ImportJava.util.HashMap;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.support.v7.app.ActionBar;Importandroid.support.v4.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.webkit.WebView.FindListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.AutoCompleteTextView;ImportAndroid.widget.ListView;ImportAndroid.widget.RatingBar;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.RatingBar.OnRatingBarChangeListener;ImportAndroid.widget.SeekBar;ImportAndroid.widget.TextView;ImportAndroid.widget.SeekBar.OnSeekBarChangeListener;ImportAndroid.os.Build; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }    /*** A placeholder fragment containing a simple view. */     Public Static classPlaceholderfragmentextendsFragment { Publicplaceholderfragment () {} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View Rootview= Inflater.inflate (R.layout.fragment_main, container,false); ListView list = (ListView) Rootview.findviewbyid (R.id.mylistview); ArrayListNewArraylist();  for(inti=0;i<30;i++) {HashMap<string, string> map =NewHashmap<string, string>(); Map.put ("Itemtitle", "This is the Title ..."); Map.put ("Itemtext", "This is text ...");            Mylist.add (map); } simpleadapter Mschedule = new  Simpleadapter (getactivity (), MyList,                                                        R.layout.my_listitem,//This defines the style of the data fill                                                          New string[] {"Itemtitle", "Itemtext"},                         new int[] {r.id.itemtitle,r.id.itemtext});                        List.setadapter (Mschedule); returnRootview; }    }}

As in the bold section above, the ListView to be populated will be taken out first, and then an instance of adapter is populated with data from the ListView.

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.