Android adapter適配器的使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   

ListView之SimpleAdapter

SimpleAdapter的建構函式是:

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

參數context:上下文,比如this。關聯SimpleAdapter啟動並執行視圖上下文

參數data:Map列表,列表要顯示的資料,這部分需要自己實現,如例子中的getData(),類型要與上面的一致,每條項目要與from中指定條目一致

參數resource:ListView單項布局檔案的Id,這個布局就是你自訂的布局了,你想顯示什麼樣子的布局都在這個布局中。這個布局中必須包括了to中定義的控制項id

參數 from:一個被添加到Map上關聯每一個項目列名稱的列表,數組裡面是列名稱

參數 to:是一個int數組,數組裡面的id是自訂布局中各個控制項的id,需要與上面的from對應。

下面是相關的程式碼片段,雖然不是專為做講解用的例子,但SimpleAdapter的用法是完整的。

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.recall);        SysApplication.getInstance().addActivity(this);        listView = (ListView) this.findViewById(R.id.listview);         listView.setAdapter(getAdapter());     }    protected SimpleAdapter getAdapter() {        List<User> users = getUsers();            List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();            for (User u : users)             {                HashMap<String, String> temp = new HashMap<String, String>();                temp.put("name", u.getUsers_Name());                data.add(temp);            }            SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),                    data, R.layout.recall_item, new String[] { "name" },                    new int[] { R.id.name });            Log.i("info", "填充資料完成");            return adapter;            }

一下是對應的:(紅色字型為ListView中顯示)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.