ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习。现在这里大概总结一下。

基于数组的ListView:使用android:entries属性可以指定列表项数组,这种方式最简洁方便,但内容只能是文本,可定制的内容少之又少。

使用ArrayAdapter创建ListView:也仅限于将数组或集合里的元素包装成列表项,比直接使用数组好在,可以指定列表项的列表项组件。例如:使用TextView作为列表项组件,不过也只能使用TextView。

例子:

1.列表项布局文件arraylist_item.xml:是的,没错,布局文件只包含一个TextView,这也是行的。

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"/>

2.主界面布局文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.zjlyyq.test.MainActivity"> <ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>

3.MainActivity:

package com.example.zjlyyq.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.arraylist_item,names);
listView.setAdapter(adapter);
}
}

使用SimpleAdapter创建ListView:SimpleAdapter可以满足更强的定制,每个列表项对应一个布局文件,将一个Map里的元素对应在布局文件里的各组件,例如:

1.列表项布局文件simpleadapter_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_weight="0.15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"/>
<LinearLayout
android:layout_weight="0.85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/talk"/>
</LinearLayout>
</LinearLayout>

2.MainActivity:

package com.example.zjlyyq.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
int[] headers = new int[]{R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane};
String[] talks = new String[]{"晚上吃了吗?","吃了","你呢?","还没?","哦哦"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
List<Map<String,Object>> itemslist = new ArrayList<Map<String,Object>>();
for(int i = 0;i < 5;i ++){
Map<String,Object> evetyItem = new HashMap<String,Object>();
evetyItem.put("header",headers[i]);
evetyItem.put("name",names[i]);
evetyItem.put("talk",talks[i]);
itemslist.add(evetyItem);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemslist,R.layout.simpleadapter_item,
new String[]{"header","name","talk"},
new int[]{R.id.header,R.id.username,R.id.talk}); listView.setAdapter(simpleAdapter);
}
}

最新文章

  1. jq size()与length的区别
  2. php中文乱码问题
  3. IE7浏览器下CSS属性选择器二三事
  4. 如何优雅的写C++代码(一)
  5. poj 3237 Tree 树链剖分
  6. 【周期串】NYOJ-1121 周期串
  7. http://codepen.io/zhou-yg/pen/NqgPmg 在线编辑器
  8. 前端自动化构建工具-yoman浅谈
  9. 【Java学习笔记之十一】Java中常用的8大排序算法详解总结
  10. Sublime Text3 快捷键汇总及设置快捷键配置环境变量
  11. 06 intent flag三种属性
  12. 用samba来创建windows下的文件共享
  13. Spring的诞生
  14. AngularJS集合数据遍历显示
  15. Apache 和 Tomcat联系和区别
  16. springboot读取配置注解@ConfiguratioinProperties和@Value的区别
  17. CentOS 7 安装Redis
  18. 分布式监控系统Zabbix-添加windows监控主机
  19. JVM 内部原理(四)— 基本概念之 JVM 结构
  20. [Laravel] 10 - WEB API : wrapper

热门文章

  1. linux第二天
  2. 剑指Offer 数组中只出现一次的数字
  3. c#延迟加载
  4. IntelliJ IDEA WEB项目的部署配置
  5. windows下nginx安装、配置与使用(转载)
  6. mysql优化(初学)
  7. 正则-RegExp()构造函数
  8. Awesome
  9. Linux面试知识点总结
  10. Storm Windowing storm滑动窗口简介