1. 创建两个XML布局文件main.xml和user.xml。main.xml文件为系统自动创建
      main.xml布局文件代码:
    1. view plain
    2. <?xmlversion="1.0"encoding="utf-8"?>
    3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    4. android:orientation="vertical"
    5. android:layout_width="fill_parent"
    6. android:layout_height="fill_parent">
    7. <LinearLayoutandroid:id="@+id/listLinearLayout"<!-- 设置LinearLayout的ID -->
    8. android:layout_width="fill_parent"<!-- 设置LinearLayout宽度为填满整个屏幕 -->
    9. android:layout_height="wrap_content"<!-- 设置LinearLayout高度适应内部控件的高度 -->
    10. android:orientation="vertical"><!-- 设置LinearLayout的排列方式为纵向排列 -->
    11. <!-- 在LinearLayout里嵌套一个ListView控件 -->
    12. <ListViewandroid:id="@id/android:list"<!-- 设置ListView控件的ID,这个ID为android系统内置ID -->
    13. android:layout_width="fill_parent"<!-- 设置ListView控件的宽度为填满整个屏幕 -->
    14. android:layout_height="wrap_content"<!-- 设置ListView控件的高度为自适应 -->
    15. android:drawSelectorOnTop="false"<!-- 设置ListView控件条目被按下时背景颜色在文字背后,设置成True时背景色会覆盖文字 -->
    16. android:scrollbars="vertical"/><!-- 设置ListView控件滚动条的方向为纵向 -->
    17. </LinearLayout>
    18. </LinearLayout>

    1. ListViw控件中的ID (android:id="@id/android:list") 是Android系统内置的ID,不能改为其他。
      android:drawSelectorOnTop="false" <!-- 当设置为false时条目被按下时背景颜色在文字背后,设置成True时背景色会覆盖文字
      user.xml布局文件代码:
    1. view plain
    2. <?xmlversion="1.0"encoding="utf-8"?>
    3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    4. android:orientation="horizontal"
    5. android:layout_width="fill_parent"
    6. android:layout_height="fill_parent">
    7. <TextViewandroid:id="@+id/user_name"
    8. android:layout_width="180dip"
    9. android:layout_height="wrap_content"
    10. android:singleLine="true"
    11. android:textSize="10pt"
    12. android:paddingTop="2dip"
    13. android:paddingLeft="2dip"
    14. />
    15. <TextViewandroid:id="@+id/user_ip"
    16. android:layout_width="180dip"
    17. android:layout_height="wrap_content"
    18. android:textSize="10pt"
    19. android:singleLine="true"
    20. android:paddingTop="2dip"
    21. android:paddingRight="2dip"
    22. />
    23. </LinearLayout>
    1. 在Java源代码文件中写入如下代码:
    1. view plain
    2. package paj.ListView;
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import android.app.Activity;
    6. import android.app.ListActivity;
    7. import android.os.Bundle;
    8. import android.view.View;
    9. import android.widget.ListView;
    10. import android.widget.SimpleAdapter;
    11. publicclass ListViewMainextends ListActivity{
    12. /** Called when the activity is first created. */
    13. @Override
    14. publicvoid onCreate(Bundle savedInstanceState) {
    15. super.onCreate(savedInstanceState);
    16. setContentView(R.layout.main);
    17. //生成一个ArrayList类型的变量list
    18. ArrayList<HashMap<String, String>> list =new ArrayList<HashMap<String,String>>();
    19. //生成两个HashMap类型的变量map1 , map2
    20. //HashMpa为键值对类型。第一个参数为建,第二个参数为值
    21. HashMap<String, String> map1 =new HashMap<String, String>();
    22. HashMap<String, String> map2 =new HashMap<String, String>();
    23. //把数据填充到map1和map2中。
    24. map1.put("user_name","张三");
    25. map1.put("user_ip","192.168.1.52");
    26. map2.put("user_name","李四");
    27. map2.put("user_ip","192.168.0.1");
    28. //把map1和map2添加到list中
    29. list.add(map1);
    30. list.add(map2);
    31. //生成一个SimpleAdapter类型的变量来填充数据
    32. SimpleAdapter listAdapter =new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name" , "user_ip"},newint[]{R.id.user_name , R.id.user_ip});
    33. //设置显示ListView
    34. setListAdapter(listAdapter);
    35. }
    36. //重写onListItemClick但是ListView条目事件
    37. @Override
    38. protectedvoid onListItemClick(ListView l, View v,int position,long id) {
    39. // TODO Auto-generated method stub
    40. super.onListItemClick(l, v, position, id);
    41. //显示单击条目ID号
    42. System.out.println("id = " + id);
    43. //显示所单击条目的位置数
    44. System.out.println("position = " + position);
    45. }
    46. }


    1. //生成SimpleAdapter对象参数的解释
    1. view plain
    2. SimpleAdapter listAdapter =new SimpleAdapter(
    3. this//this是当前Activity的对象
    4. , list//list为填充数据后的ArrayList类型的list对象
    5. , R.layout.user//这个就是之前创建的第二个布局文件user.xml的id。
    6. ,new String[]{"user_name" , "user_ip"}//这个String数组中的元素就是list对象中的列,list中有几这个数组中就要写几列。
    7. //其中的元素必须是list中列的名。
    8. ,newint[]{R.id.user_name , R.id.user_ip}//这个int型数组中的元素对应着上上一个参数String类型数组中的列名。
    9. //它的值是对应user.xml布局文件中的TextView的id
    10. );
  • 更多相关文章

    1. android 对SD卡文件的I/O操作
    2. Android中在Button控件上显示倒计时
    3. Android SwipeMenuRecyclerView控件的用法
    4. Android 自定义布局控件-圆形RelativeLayout
    5. Android中将布局文件/View添加至窗口过程分析 ---- 从setContent

    随机推荐

    1. 用android studio写一个简单的计算器(没有
    2. android音乐播放器Service的生命周期分析
    3. Android(安卓)8.0 报错 android.os.FileU
    4. Android(安卓)requires compiler complia
    5. android通过php连接mysql数据库!!!!
    6. Android(安卓)DrawerLayout实现抽屉效果
    7. Android在开机时自动启动一个应用程序
    8. Android剪贴板
    9. android实现截屏功能
    10. 为什么在AndroidManifest.xml中Activity