亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Android 中的 adapter

系統(tǒng) 1821 0

Adapter介紹

?

Adapter 是將數(shù)據(jù)綁定到 UI 界面上的橋接類。 Adapter 負(fù)責(zé)創(chuàng)建顯示每個(gè)項(xiàng)目的子 View 和提供對下層數(shù)據(jù)的訪問。

?

支持 Adapter 綁定的 UI 控件必須擴(kuò)展 AdapterView 抽象類。創(chuàng)建自己的繼承自 AdapterView 的控件和創(chuàng)建新的 Adapter 類來綁定它們是可能的。

?

一些 Android 提供的 Adapter 介紹

?

在多數(shù)情況下,你不需要白手創(chuàng)建自己的 Adapter Android 提供了一系列 Adapter 來將數(shù)據(jù)綁定到 UI Widget 上。

?

因?yàn)? Android 負(fù)責(zé)提供數(shù)據(jù)和選擇用于顯示每個(gè)項(xiàng)目的 View ,所以 Adapter 能快速地修改要綁定的控件的外觀和功能。下面的列表顯示了兩個(gè)最有用和最通用的本地 Adapter

?

? ArrayAdapter

ArrayAdapter 是一個(gè)綁定 View 到一組對象的通用類。默認(rèn)情況下, ArrayAdapter 綁定每個(gè)對象的 toString 值到在 layout 中預(yù)先定義的 TextView 控件上。可變通的,構(gòu)造函數(shù)允許你使用更加復(fù)雜的 layout 或者通過重寫 getView 方法來擴(kuò)展類從而使用 TextView 的替代物(如 ImageView 或嵌套的 layout )。

?

? SimpleCursorAdapter

SimpleCursorAdapter 綁定 View Content Provider 查詢返回的游標(biāo)上。指定一個(gè) XML layout 定義,然后將數(shù)據(jù)集中的每一列的值綁定到 layout 中的一個(gè) View 上。

?

接下來的章節(jié)將深入挖掘這些 Adapter 類的細(xì)節(jié)。例子中,提供了綁定數(shù)據(jù)到 ListView 上,盡管這個(gè)邏輯會(huì)和其他一些 AdapterView 類(如 Spinner Gallery )工作的一樣。

?

使用 Adapter 進(jìn)行數(shù)據(jù)綁定

?

Adapter 應(yīng)用到繼承自 AdapterView 類上,你需要調(diào)用 View setAdapter 方法,傳入一個(gè) Adapter 實(shí)例,如下面的片段所示:

?

ArrayList<String> myStringArray = new ArrayList<String>();

ArrayAdapter<String> myAdapterInstance;

int layoutID = android.R.layout.simple_list_item_1;

myAdapterInstance = new ArrayAdapter<String>(this, layoutID, myStringArray);

myListView.setAdapter(myAdapterInstance);

?

這個(gè)片段顯示了最簡單的情況,將數(shù)組中的字符串綁定到 ListView 中用于顯示每個(gè)項(xiàng)目的簡單 TextView 控件上。

?

接下來的第一個(gè)例子顯示了如何綁定一組復(fù)雜的對象到 ListView 上,使用一個(gè)自定義的 layout 。第二個(gè)例子顯示了如何使用 SimpleCursorAdapter 來綁定查詢結(jié)果到 ListView 中的自定義 layout 上。

?

?

?

?

在android開發(fā)中列表的使用是十分常見的。google對列表的封裝使列表既有顯示傳統(tǒng)文本列表的能力,也有加入了諸如選擇項(xiàng)、復(fù)選項(xiàng)等處理事件的能力。這里寫一些我這幾天對這個(gè)問題的理解。

在android的api中,LIST和adapter都被放在了android.widget包內(nèi)。包內(nèi)的具體結(jié)構(gòu)我這里先不展示了,主要側(cè)重列表和adapter。adapter的作用就是將要在列表內(nèi)顯示的數(shù)據(jù)和列表本身結(jié)合起來。列表本身只完成顯示的作用,其實(shí)他就是繼承自VIEWGROUP類。但是他又有一個(gè)獨(dú)特的函數(shù)就是setAdapter()就是完成了view和adapter的結(jié)合。adapter如同其本身含義,其實(shí)就是一個(gè)適配器,他可以對要顯示的數(shù)據(jù)進(jìn)行統(tǒng)一的封裝,主要是將數(shù)據(jù)變成view提供給list。

我們先來看看adapter的體系:

public interface Adapter----0層(表示繼承體系中的層次)

public interface ExpandableListAdapter---(無所謂層次因?yàn)闆]有其他接口繼承實(shí)現(xiàn)它)

這是adapter的始祖,其他個(gè)性化的adapter均實(shí)現(xiàn)它并加入自己的接口。

public interface ListAdapter----1層

public interface SpinnerAdapter----1層

public interface WrapperListAdapter----2層(實(shí)現(xiàn)ListAdapter)

以上接口層面上的體系已經(jīng)完了。可以看出來作為widget view的橋梁adapter其實(shí)只分為2種:ListAdapter和SpinnerAdapter以及ExpandableListAdapter。也就是說所有widget也就是基于list和spinne與ExpandableList三種view形式的。

由于在實(shí)際使用時(shí),我們需要將數(shù)據(jù)加入到Adapter,而以接口形式呈現(xiàn)的adapter無法保存數(shù)據(jù),于是Adapter就轉(zhuǎn)型為類的模式。

public abstract class BaseAdapter----2層(實(shí)現(xiàn)了ListAdapter和SpinnerAdapter)

以抽象類的形式出現(xiàn)構(gòu)造了類型態(tài)下的頂層抽象,包容了List和Spinner

public class ArrayAdapter----3層

public class SimpleAdapter---3層

public class CursorAdapter----3層(CursorAdapter其后還有子類這里先不探討)

基本體系有了之后,讓我們看看頂層Adapter里有哪些方法(只列舉常用的):

abstract Object getItem(int position)

abstract int getCount()

abstract long getItemId(int position)

abstract int getItemViewType(int position)

abstract View getView(int position,View convertVeiw,ViewGroup parent)

以上是比較重要的方法,ArrayAdapter他們也是重新實(shí)現(xiàn)以上方法的。在實(shí)際的開發(fā)過程中,往往我們要自己做屬于自己的Adapter,以上方法都是需要重新實(shí)現(xiàn)的。

?

?

ArrayAdapter和SimpleCursorAdapter例子

?

?

    使用ArrayAdapter定制 To-Do List

    ?

    這個(gè)例子將擴(kuò)展 To-Do List 工程,以一個(gè) ToDoItem 對象來儲(chǔ)存每一個(gè)項(xiàng)目,包含每個(gè)項(xiàng)目的創(chuàng)建日期。

    ?

    你將擴(kuò)展 ArrayAdapter 類來綁定一組 ToDoItem 對象到 ListView 上,并定制用于顯示每一個(gè) ListView 項(xiàng)目的 layout

    ?

    1. 返回到 To-Do List 工程。創(chuàng)建一個(gè)新的 ToDoItem 類來保存任務(wù)和任務(wù)的創(chuàng)建日期。重寫 toString 方法來返回一個(gè)項(xiàng)目數(shù)據(jù)的概要。

                package com.paad.todolist;
     import java.text.SimpleDateFormat;
     import java.util.Date;
     public class ToDoItem {
     String task; Date created; 
    public String getTask() { return task; } 
    public Date getCreated() { return created; }
     public ToDoItem(String _task) { 
    this(_task, new Date(java.lang.System.currentTimeMillis())); 
    } 
    public ToDoItem(String _task, Date _created) {
     task = _task; created = _created; 
    }
     @Override public String toString() { 
    SimpleDateFormat sdf = new SimpleDateFormat(“dd/MM/yy”); 
    String dateString = sdf.format(created); 
    return “(“ + dateString + “) “ + task; 
    } 
    } 
              

    ?

    ?

    ?

    2. 打開 ToDoList Activity ,修改 ArrayList ArrayAdapter 變量的類型,儲(chǔ)存 ToDoItem 對象而不是字符串。然后,你將修改 onCreate 方法來更新相應(yīng)的變量初始化。你還需要更新 onKeyListener 處理函數(shù)來支持 ToDoItem 對象。

    ?

    ?

private ArrayList<ToDoItem> todoItems; private ListView myListView; private EditText myEditText; private ArrayAdapter<ToDoItem> aa; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); // Inflate your view setContentView(R.layout.main); // Get references to UI widgets myListView = (ListView)findViewById(R.id.myListView); myEditText = (EditText)findViewById(R.id.myEditText); todoItems = new ArrayList<ToDoItem>(); int resID = R.layout.todolist_item; aa = new ArrayAdapter<ToDoItem>(this, resID, todoItems); myListView.setAdapter(aa); myEditText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { ToDoItem newItem; newItem = new ToDoItem(myEditText.getText().toString()); todoItems.add(0, newItem); myEditText.setText(“”); aa.notifyDataSetChanged(); cancelAdd(); return true; } return false; } }); registerForContextMenu(myListView); }
    

?

3. 如果你運(yùn)行 Activity ,它將顯示每個(gè) to-do 項(xiàng)目,如圖 5-3 所示。

?

Android 中的 adapter

5-3

?

4. 現(xiàn)在,你可以創(chuàng)建一個(gè)自定義的 layout 來顯示每一個(gè) to-do 項(xiàng)目。修改在第 4 章中創(chuàng)建的自定義 layout ,包含另外一個(gè) TextView ,它將用于顯示每個(gè) to-do 項(xiàng)目的創(chuàng)建日期。

<?xml version=”1.0” encoding=”utf-8”?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:background=”@color/notepad_paper”> <TextView android:id=”@+id/rowDate” android:layout_width=”wrap_content” android:layout_height=”fill_parent” android:padding=”10dp” android:scrollbars=”vertical” android:fadingEdge=”vertical” android:textColor=”@color/notepad_text” android:layout_alignParentRight=”true” /> <TextView android:id=”@+id/row” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:padding=”10dp” android:scrollbars=”vertical” android:fadingEdge=”vertical” android:textColor=”@color/notepad_text” android:layout_alignParentLeft=”@+id/rowDate” /> </RelativeLayout>
    

??

5. 創(chuàng)建一個(gè)新的類( ToDoItemAdapter ),使用指定的 ToDoItem 變量來擴(kuò)展一個(gè) ArrayAdapter 。重寫 getView 方法來將 ToDoItem 對象中的 task date 屬性指定給第 4 步創(chuàng)建的 layout 中的 View

?

      
    
    ?import java.text.SimpleDateFormat; import android.content.Context; import java.util.*; import android.view.*; import android.widget.*; public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> { int resource; public ToDoItemAdapter(Context _context,int _resource, List<ToDoItem> _items) { super(_context, _resource, _items); resource = _resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout todoView; ToDoItem item = getItem(position); String taskString = item.getTask(); Date createdDate = item.getCreated(); SimpleDateFormat sdf = new SimpleDateFormat(“dd/MM/yy”); String dateString = sdf.format(createdDate); if (convertView == null) { todoView = new LinearLayout(getContext()); String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater vi; vi = (LayoutInflater)getContext().getSystemService(inflater); vi.inflate(resource, todoView, true); } else { todoView = (LinearLayout) convertView; } TextView dateView = (TextView)todoView.findViewById (R.id.rowDate); TextView taskView = (TextView)todoView.findViewById(R.id.row); dateView.setText(dateString); taskView.setText(taskString); return todoView; } } 
 
    
     ? 
 
    
    

6. 最后,用 ToDoItemAdapter 替換 ArrayAdapter 的定義。

?

private ToDoItemAdapter aa;

?

onCreate 中,使用 new ToDoItemAdapter 來替換 ArrayAdapter<String> 的實(shí)例化。

?

aa = new ToDoItemAdapter(this, resID, todoItems);

?

7. 如果你運(yùn)行 Activity ,它看起來如圖 5-4 的截圖。

?

Android 中的 adapter

5-4

?

使用SimpleCursorAdapter

?

SimpleCursorAdapter 允許你綁定一個(gè)游標(biāo)的列到 ListView 上,并使用自定義的 layout 顯示每個(gè)項(xiàng)目。

?

SimpleCursorAdapter 的創(chuàng)建,需要傳入當(dāng)前的上下文、一個(gè) layout 資源,一個(gè)游標(biāo)和兩個(gè)數(shù)組:一個(gè)包含使用的列的名字,另一個(gè)(相同大小)數(shù)組包含 View 中的資源 ID ,用于顯示相應(yīng)列的數(shù)據(jù)值。

?

下面的框架代碼顯示了如何構(gòu)造一個(gè) SimpleCursorAdapter 來顯示聯(lián)系人信息:

? String uriString = “content://contacts/people/”; Cursor myCursor = managedQuery(Uri.parse(uriString), null, null, null, null); String[] fromColumns = new String[] { People.NUMBER, eople.NAME }; int[] toLayoutIDs = new int[] { R.id.nameTextView, R.id.numberTextView }; SimpleCursorAdapter myAdapter; myAdapter = new SimpleCursorAdapter(this,R.layout.simplecursorlayout,myCursor,fromColumns,toLayoutIDs)

    
      

?

?

SimpleCursorAdapter 在本章前面的創(chuàng)建選擇聯(lián)系人的例子中使用過。你將在第 6 章學(xué)習(xí)到更多關(guān)于 Content Provider Cursor 的內(nèi)容,那里你也將找到更多 SimpleCursorAdapter 的例子。

Android 中的 adapter


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久婷婷激情综合中文字幕 | 久久久久久综合一区中文字幕 | 国产精品v | 国产在线91 | 热99精品视频 | 色偷偷91久久综合噜噜噜 | 亚洲视频欧洲视频 | 亚洲视频一区二区在线观看 | 国产成人刺激视频在线观看 | 久久国产精品亚洲 | 久久久久久九 | 久久青草精品一区二区三区 | 奇米影视亚洲色图 | 奇米影视狠狠干 | 波多野结衣在线一区二区 | 在线观看免费毛片 | 一本色道久久88加勒比—综合 | 5388国产亚洲欧美在线观看 | 亚洲精品九色在线网站 | 国产美女久久久久久久久久久 | 欧美亚洲天堂 | 久久久久依人综合影院 | 欧美整片在线观看 | 一道精品视频一区二区三区图片 | 在线免费黄色网址 | 精品欧美一区手机在线观看 | 欧美又乱又伦观看 | 亚洲情欲 | 深夜在线免费观看 | 97免费在线观看 | 九九这里只有精品 | 亚洲12色吧 | 久久精品国产亚洲a | 男人的天堂在线免费视频 | 国产区一区二区三区 | 欧美操穴视频 | 尤物免费视频 | 亚洲国产欧美精品 | 一级bbbbbbbbb毛片 | 亚洲毛片大全 | 97高清国语自产拍 |