歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:ListView使用SimpleAdapter適配器

Android開發教程:ListView使用SimpleAdapter適配器

日期:2017/3/1 10:41:33   编辑:Linux編程

最近在做一個聊天的小應用,我負責聊天窗口部分,弄了個簡單的有點丑的“汽泡短信”聊天模式~先附上最終效果圖:


以下是摸索的過程,與大家分享:

從聊天的模式可以看出整個窗口應該是一個ListActivity,其中每一行用聊天的內容填充ListView。

ListView可以使用最基本的ArrayAdapter填充,但是每一行只能填充文本。我們的聊天內容除了文本,還希望有個頭像(當然後期還可以再添聊天時間、用戶名之類的~),首相想到的是使用SimpleAdapter。

本文源碼下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/1月/1日/Android開發教程:ListView使用SimpleAdapter適配器源碼/

這是第一個Demo的代碼:

  1. public class TryChatPopActivity extends Activity {
  2. ListView itemlist = null;
  3. List<Map<String, Object>> list;
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. itemlist = (ListView) findViewById(R.id.chatlist);
  8. refreshListItems();
  9. }
  10. private void refreshListItems() {
  11. list = buildListForSimpleAdapter();
  12. //實例適配器
  13. SimpleAdapter chat = new SimpleAdapter(this, list, R.layout.chata,
  14. new String[] {"chatportrait","chatinfo"}, new int[] {R.id.imgPortraitA,R.id.txvInfo});
  15. itemlist.setAdapter(chat);
  16. itemlist.setSelection(0);
  17. }
  18. //用來實例化列表容器的函數
  19. private List<Map<String, Object>> buildListForSimpleAdapter() {
  20. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(2);
  21. ImageView imgA=(ImageView)findViewById(R.id.imgPortraitA);
  22. //向列表容器中添加數據(每列中包括一個頭像和聊天信息)
  23. Map<String, Object> map = new HashMap<String, Object>();
  24. map.put("chatportrait",imgA);
  25. map.put("chatinfo", "嗨~");
  26. list.add(map);
  27. map = new HashMap<String, Object>();
  28. map.put("chatportrait",imgA);
  29. map.put("chatinfo", "嗨~\n你好!");
  30. list.add(map);
  31. map = new HashMap<String, Object>();
  32. map.put("chatportrait",imgA);
  33. map.put("chatinfo", "嗨~\n你好!\n我是小魏~");
  34. list.add(map);
  35. return list;
  36. }
  37. }

其中 chata 布局文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal"
  7. android:paddingTop="5px"
  8. >
  9. <ImageView
  10. android:layout_width="42px"
  11. android:layout_height="42px"
  12. android:layout_gravity="bottom"
  13. android:id="@+id/imgPortraitA"
  14. android:background="@drawable/portraita"
  15. />
  16. <TextView android:id="@+id/txvInfo"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:paddingTop="5px"
  20. android:paddingBottom="5px"
  21. android:paddingLeft="5px"
  22. android:textColor="@android:color/black"
  23. android:textSize="18dip"
  24. android:background="@drawable/chatbg"></TextView>
  25. </LinearLayout>

這裡最關鍵是嘗試定義和使用SimpleAdapter適配器:

  1. SimpleAdapter chat = new SimpleAdapter(this, list, R.layout.chata,
  2. new String[] {"chatportrait","chatinfo"}, new int[] {R.id.imgPortraitA,R.id.txvInfo});

其中第一個參數是context,即當前的Activity;第二個參數是要去填充ListView每一行內容的list;第三個參數resource是ListView每一行填充的布局文件。

Copyright © Linux教程網 All Rights Reserved