閱讀903 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android 適配器教程(二)

       上次我寫了一個類似前言的東西,向大家粗略介紹了一下什麼是適配器,並且舉了一個最簡單的例子幫助大家理解,使用了安卓原生的ArrayAdapter現在我們繼續向下學習,同時也繼續在我們上次的Demo項目中繼續添加例子,一步步的向下深入學習。


這次要講解的是另外一個常用的安卓原生適配器—SimpleCursorAdapter:


關於SimpleCursorAdapter,sdk的解釋是這樣的:

      An easyadapter to map columns from a cursor to TextViews or ImageViews defined in anXML file. You can specify which columns you want, which views you want todisplay the columns, and the XML file that defines the appearance of theseviews。


簡單的說就是方便把從遊標得到的數據進行列表顯示,並可以把指定的列映射到對應的TextView中。

 

下麵要寫的程序是從電話簿中把聯係人顯示到類表中獲得一個指向數據庫的Cursor並且定義一個布局文件(當然也可以使用係統自帶的)將數據展示出來!

 

項目開始!


(1)還是先在activity_main.xml裏添加一個button,一會跳轉的時候使用。

(2)然後新建一個類SimpleCursorAdapterDemo繼承自Activity作為我們第二個例子的Activity,並且

@Override 我們的onCreate方法。

(3)新建一個xml文件simplecursoradapterdemo.xml作為我們的布局文件,其中也是包含一個文本域和一個ListView:

代碼如下

simplecursoradapterdemo.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是simplecursoradapter的一個例子" >
    </TextView>

    <ListView
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

(4)回到SimpleCursorAdapterDemo,定義一個Listview暫且叫做lv,並使用剛才的ID,setContentView()為剛才的xml布局,然後為lv設置一個適配器:

SimpleCursorAdapter(Context context,int layout,Cursor c, String[] from, int[] to)

先解釋一下參數:

第一個參數this,第二個layout的參數是ListView中單行的布局, c是你數據的遊標。剛開始其實from和to比較讓人不熟悉,From是你查詢出的數據,to是單行布局中的單獨控件,一對一的關係,用起來非常方便。


再說一下需要用到的Cursor


我們用:

Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null,null);

先獲得一個指向係統通訊錄數據庫的Cursor對象獲得數據來源。


再使用:

startManagingCursor(cursor);

將獲得的Cursor對象交由Activity管理,這樣Cursor的生命周期和Activity便能夠自動同步,省去自己手動管理Cursor。


SimpleCursorAdapter 構造函數前麵3個參數和ArrayAdapter是一樣的,最後兩個參數:一個包含數據庫的列的String型數組,一個包含布局文件中對應組件id的int型數組。其作用是自動的將String型數組所表示的每一列數據映射到布局文件對應id的組件上。上麵的代碼,將NAME列的數據一次映射到布局文件的id為text1的組件上。


注意:需要在AndroidManifest.xml中如權限:

<uses-permissionandroid:name="android.permission.READ_CONTACTS"></uses-permission>


具體代碼如下:

package com.example.adapterdemo;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class SimpleCursorAdapterDemo extends Activity{
	
	private ListView lv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simplecursoradapterdemo);
        lv = (ListView)findViewById(R.id.simplecursoradapterlistview);
        Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor);
         
        ListAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1, 
                cursor,
                new String[]{People.NAME}, 
                new int[]{android.R.id.text1});
         
        lv.setAdapter(listAdapter);
	}
}


最終的效果圖:





這樣第二種常用的安卓原生適配器也就告一段落了,還是本著分成多篇以防止篇幅過長的原則,這一篇就到這裏了,下一講我們繼續學習SimpleAdapter,一種可擴展性很強的適配器,大家不要錯過哦!


源碼會在最後一篇寫完的時候傳上來,我也是邊寫博客,邊寫代碼,我覺得這樣思路比較清晰。


我也是學生,水平有限,還望大家多多指教~

最後更新:2017-04-03 05:39:33

  上一篇:go Cocos2d-x開發實例:使用Lambda 表達式
  下一篇:go poj2528-Mayor&#39;s posters