在layout xml中添加listview控件
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="480dp"
android:drawSelectorOnTop="true"
android:scrollbars="vertical" />
在MainActivity中
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义数据,一个ArrayList,元素类型为Map<String, Object>
ArrayList<Map<String, Object>> array = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
/** 每个map对应将要映射到指定视图View的数据。这里和user.xml对应,user对应TextView
value对应ImageView
*/
map1 = new HashMap<String, Object>();
map1.put("user", "滨水小区16号楼4单元");
map1.put("value", R.drawable.home02);
map1.put("time", "15:20");
map1.put("machine", "主机1111-2222来电");
array.add(map1);
map1 = new HashMap<String, Object>();
map1.put("user", "滨水小区6号楼3单元");
map1.put("value", R.drawable.home03);
map1.put("time", "15:18");
map1.put("machine", "主机1001-2222来电");
array.add(map1);
map1 = new HashMap<String, Object>();
map1.put("user", "滨水小区23号楼5单元");
map1.put("value", R.drawable.home04);
map1.put("time", "16:20");
map1.put("machine", "主机1111-0000来电");
array.add(map1);
map1 = new HashMap<String, Object>();
map1.put("user", "滨水小区19号楼8单元");
map1.put("value", R.drawable.home05);
map1.put("time", "13:20");
map1.put("machine", "主机1111-1111来电");
array.add(map1);
map1 = new HashMap<String, Object>();
map1.put("user", "滨水小区1号楼16单元");
map1.put("value", R.drawable.home06);
map1.put("time", "12:20");
map1.put("machine", "主机0000-2222来电");
array.add(map1);
map1 = new HashMap<String, Object>();
map1.put("user", "滨水小区3号楼65单元");
map1.put("value", R.drawable.home07);
map1.put("time", "15:45");
map1.put("machine", "主机1111-2222来电");
array.add(map1);
map1 = new HashMap<String, Object>();
map1.put("user", "滨水小区16号楼4单元");
map1.put("value", R.drawable.home02);
map1.put("time", "19:20");
map1.put("machine", "主机1111-2222来电");
array.add(map1);
/** 自定义一个Adapter
* 第一个参数为当前Activity,第二个array为指定需要显示的数据集合
* 第三个参数是将要显示每行数据的View布局,
* 第四个参数是字符数组,对应View布局中的id名称
* 第五个参数是int数组,对应布局文件中的id。
*/
MyAdapter adapter = new MyAdapter(this, array, R.layout.item,
new String[]{"user", "value","time","machine"},
new int[]{R.id.user, R.id.value,R.id.time,R.id.machine});
//设置适配器
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// 响应list点击事件
super.onListItemClick(l, v, position, id);
TextView user = (TextView) v.findViewById(R.id.user);
Toast.makeText(this, "你选择了:" + user.getText(), 1000).show();
System.out.println("debug:" + user.getText());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
*
* @author Administrator
* 继承SimpleAdapter 实现的适配器
*
*/
class MyAdapter extends SimpleAdapter
{
private LayoutInflater layout;
public MyAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View result = super.getView(position, convertView, parent);
//获取LayoutInflater的实例对象
layout = getLayoutInflater();
if (result == null) {
//载入指定布局
layout.inflate(R.layout.item, null);
}
return result;
}
}
}
item xml文件中设置显示的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fffff0">
<ImageView
android:id="@+id/value"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/guard01"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#2D2F31"
android:layout_marginTop="10dp"
android:text="用户"
/>
<TextView
android:id="@+id/machine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主机"
/>
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="时间"
/>
</LinearLayout>