一、摘要
An Adapter object acts as a bridge between an AdapterView
and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View
for each item in the data set.
这是Android官方对Adapter的定义。这个关系可以描述为下面一个关系图,
二、Adapter的继承关系
三,样例
这里附上北航《Android编程宝典》的一个例子,搭配ListView说明Adapter怎么样去扮演这个中间人,为View层提供数据。
1,定义3个layout文件,分别是activity_main.xml,fragment_main.xml,my_listitem.xml。
activity_main.xml
<FrameLayout xmlns: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
<LinearLayout xmlns: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"> <ListView android: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"?> <LinearLayout xmlns: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" > <TextView android:id="@+id/itemTitle" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textSize="30sp" /> <TextView android:id="@+id/itemText" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
2,编写MainActivity,代码如下
package so.byj.example.mytestapp; import java.util.ArrayList; import java.util.HashMap; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.ListView; import android.widget.RatingBar; import android.widget.SimpleAdapter; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.SeekBar; import android.widget.TextView; import android.widget.SeekBar.OnSeekBarChangeListener; import android.os.Build; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } @Override public boolean onCreateOptionsMenu(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 boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View 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); ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); for(int i=0;i<30;i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("ItemTitle", "This is Title....."); map.put("ItemText", "This is text....."); mylist.add(map); } SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist, R.layout.my_listitem, // 这里定义了数据填充的样式 new String[] {"ItemTitle", "ItemText"}, new int[] {R.id.itemTitle,R.id.itemText}); list.setAdapter(mSchedule); return rootView; } } }
如上面黑体部分,先把待填充数据的ListView拿出来,然后实例一个Adapter对ListView进行数据填充。