android ListView示例

来源:互联网 发布:linux tail 最后字节 编辑:程序博客网 时间:2024/05/16 12:30
 

实现列表显示的集中方法

1、通过ListActivity实现

public class ListViewTestActivity extends ListActivity {
    //声明数据数组
 private String students[]={"张三","李四","王五"};
    private ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, students);
        setListAdapter(adapter);

           }
}

 

2、通过xml文件实现

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="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

 </LinearLayout>

显示列表类

public class ListViewTestActivity extends Activity {
    //声明数据数组
 private String students[]={"张三","李四","王五"};
    private ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv=(ListView)findViewById(android.R.id.list);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, students);
        lv.setAdapter(adapter);
           }
}

演示效果:

 

3、如果没有任何数据的情况下:布局文件如下

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
             
               android:text="No data"/>
 </LinearLayout>

 

4、通过SimpleAdapter适配器实现列表

public class ListViewTestActivity extends Activity {
 //声明一个ListView显示信息列表
    private ListView lv;
    //声明一个数据集合
    private ArrayList<HashMap<String, String>> map;
    private HashMap<String, String> hmap;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //实例化集合
        map=new ArrayList<HashMap<String,String>>();
        hmap=new HashMap<String, String>();
        lv=new ListView(this);
        //添加列表数据
        hmap.put("姓名", "时允田");
        map.add(hmap);
        hmap=new HashMap<String, String>();
        hmap.put("姓名", "时允田");
        map.add(hmap);
        hmap=new HashMap<String, String>();
        hmap.put("姓名", "时允田");
        map.add(hmap);
        SimpleAdapter adapter=new SimpleAdapter(this,
          map, android.R.layout.simple_list_item_1, new String[]{"姓名"}, new int[]{android.R.id.text1});
        lv.setAdapter(adapter);
        setContentView(lv);
    }
}

5、利用自定义的布局文件,控制列表显示格式

自定义布局文件

<?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="wrap_content">
 <TextView android:id="@+id/mview1" android:layout_width="100px"
  android:layout_height="wrap_content" />

 <TextView android:id="@+id/mview2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

</LinearLayout>

实现列表类

public class ListViewTestActivity extends Activity {
 //声明一个ListView显示信息列表
    private ListView lv;
    //声明一个数据集合
    private ArrayList<HashMap<String, String>> map;
    private HashMap<String, String> hmap;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //实例化集合
        map=new ArrayList<HashMap<String,String>>();
        hmap=new HashMap<String, String>();
        lv=new ListView(this);
        //添加列表数据
        hmap.put("姓名", "时允田");
        hmap.put("年龄", "26");
        map.add(hmap);
        hmap=new HashMap<String, String>();
        hmap.put("姓名", "时允田");
        hmap.put("年龄", "26");
        map.add(hmap);
        hmap=new HashMap<String, String>();
        hmap.put("姓名", "时允田");
        hmap.put("年龄", "26");
        map.add(hmap);
// 利用自己的layout来进行显示两项
//  SimpleAdapter adapter = new SimpleAdapter(this, data,
//    R.layout.list_item, new String[] { "姓名", "性别" }, new int[] {
//      R.id.mview1, R.id.mview2 });       

        lv.setAdapter(adapter);
        setContentView(lv);
    }
}

 

 

原创粉丝点击