public class TryChatPopActivity extends Activity { ListView itemlist = null; List<Map<String, Object>> list; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); itemlist = (ListView) findViewById(R.id.chatlist); refreshListItems(); } private void refreshListItems() { list = buildListForSimpleAdapter(); //实例适配器 SimpleAdapter chat = new SimpleAdapter(this, list, R.layout.chata, new String[] {"chatportrait","chatinfo"}, new int[] {R.id.imgPortraitA,R.id.txvInfo}); itemlist.setAdapter(chat); itemlist.setSelection(0); } //用来实例化列表容器的函数 private List<Map<String, Object>> buildListForSimpleAdapter() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(2); ImageView imgA=(ImageView)findViewById(R.id.imgPortraitA); //向列表容器中添加数据(每列中包括一个头像和聊天信息) Map<String, Object> map = new HashMap<String, Object>(); map.put("chatportrait",imgA); map.put("chatinfo", "嗨~"); list.add(map); map = new HashMap<String, Object>(); map.put("chatportrait",imgA); map.put("chatinfo", "嗨~\n你好!"); list.add(map); map = new HashMap<String, Object>(); map.put("chatportrait",imgA); map.put("chatinfo", "嗨~\n你好!\n我是小魏~"); list.add(map); return list; } }
Android开发教程:ListView使用SimpleAdapter适配器
内容版权声明:除非注明,否则皆为本站原创文章。