Android ListView 控件学习

1.创建Activity

package com.coolbi;import java.util.ArrayList;import java.util.HashMap;import android.app.ListActivity;import android.os.Bundle;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;public class ListViewTest extends ListActivity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();HashMap<String,String> map1 = new HashMap<String,String>();HashMap<String,String> map2 = new HashMap<String,String>();HashMap<String,String> map3 = new HashMap<String,String>();map1.put("userName","张三");map1.put("userStatus","大学");map1.put("sex","男");map2.put("userName","李四");map2.put("userStatus", "研究生");map2.put("sex","女");map3.put("userName","至死不悟");map3.put("userStatus", "博士");map3.put("sex","男");list.add(map1);list.add(map2);list.add(map3);SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.listview,new String[]{"userName","userStatus","sex"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3});setListAdapter(adapter);}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id){super.onListItemClick(l, v, position, id);System.out.println("position="+position+",id="+id);//this.showDialog(0);}}

2.Main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="fill_parent"><ListViewandroid:id="@id/android:list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_marginRight="30dip"/> </LinearLayout></LinearLayout>

3,显示ListView内容的样式文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView android:id="@+id/textView1"    android:layout_width="100dip"     android:layout_height="30dip"     android:paddingTop="5dip"    android:layout_marginRight="30dip"    /><TextView  android:id="@+id/textView2"    android:layout_width="100dip"     android:layout_height="30dip"     android:paddingTop="5dip"    /><TextViewandroid:id="@+id/textView3"android:layout_width="100dip"android:layout_height="30dip"     android:paddingTop="5dip" /></LinearLayout>

更多相关文章

  1. Android开发UI之自定义控件的皮肤
  2. android 各种常用,不易记的dialog,等控件整理
  3. Android仿iOS左右滑动开关控件(Android4.0以上适用)
  4. Android系统自带样式Theme总结
  5. react native 修改Picker样式Android
  6. Android动态生成按钮样式
  7. 【063】◀▶ Android (I) - 控件和布局
  8. Android--ListView滚动条样式
  9. Android控件及API说明(二)

随机推荐

  1. android获取指定路径下目录文件
  2. Android(安卓)Handler(七)
  3. Android(安卓)Opencore OpenMAX学习
  4. android获取位置权限,手机状态权限,存储权
  5. Android中四种OnClick事件的写法
  6. android隐藏底部虚拟键Navigation Bar实
  7. android如何实现渐变色?
  8. android ActionBar的使用
  9. android recent键值
  10. Android开发需要配置的环境