ListView 现在来看是一个可以入GridView一样展示数据的列表,可以定制展示的布局,绑定数据源。

1.使用定义好的样式进行布局,这里面的值就是有一个。

public class ActivityList1 extends Activity {
    ListView listView;
    private String[] data = { "Android应用专业开发社区:eoeAndroid.com", "eoeAndroid出品软件如下:",
            "eoeInstaller", "eoeDouban", "eoeWhere",
            "eoeInfoAssistant", "eoeDakarGame","eoeTrack" }; //数据源
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = new ListView(this);
//        listView.setAdapter(new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, data));
        listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, data));    //使用系统R里面定义的值,呈现为一个单选框的样子
        listView.setItemsCanFocus(true); //允许点击
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); //允许多选
        setContentView(listView); //立即显示这个列表
    }
}

 

2.数据为空的时候显示空数据

private String[] data ={};
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list3);
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data));
    }

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
      <TextView android:id="@id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="对不起,没有数据显示"/>     
</LinearLayout>

 

3.基于Map的数据源的显示,和自定义格式,及点选事件

public class ActivityList2 extends Activity {

    private List<Map<String, Object>> data;
    private ListView listView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PrepareData();
        listView = new ListView(this);
        // 利用系统的layout显示一项
         SimpleAdapter adapter = new SimpleAdapter(this, data,
         android.R.layout.simple_list_item_1, new String[] { "姓名" },
         new int[] { android.R.id.text1 });
         //参数依次含义: 1.Context,2数据源,3显示外观,4,所要呈现的属性名称,5显示的位置
        // 利用系统的layout显示两项
//         SimpleAdapter adapter = new SimpleAdapter(this, data,
//         android.R.layout.simple_list_item_2, new String[] { "姓名","性别" },
//         new int[] { android.R.id.text1 , android.R.id.text2});
        // 利用自己的layout来进行显示两项
//        SimpleAdapter adapter = new SimpleAdapter(this, data,
//                R.layout.list_item, new String[] { "姓名", "性别" }, new int[] {
//                        R.id.mview1, R.id.mview2 });
         //这里第 3个显示外观ID,变成了R的Layout自定义的XML ,而显示的位置 也变成 了其中的Textbox控件
        listView.setAdapter(adapter); //设置数据适配器
        setContentView(listView);      //设置显示视图为ListView
        //声明一个委托
        OnItemClickListener listener = new OnItemClickListener() {
            //参数含义: 1.事件触发的ListView控件,2选中的项目的View,3选中View在ListView的位置,4被选中行ID
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                setTitle(parent.getItemAtPosition(position).toString());
            }
        };
        //设置这个委托到特定事件
        listView.setOnItemClickListener(listener);
    }

    private void PrepareData() {
        data = new ArrayList<Map<String, Object>>();
        Map<String, Object> item;
        item = new HashMap<String, Object>();
        item.put("姓名", "张三小朋友");
        item.put("性别", "男");
        data.add(item);
        item = new HashMap<String, Object>();
        item.put("姓名", "王五同学");
        item.put("性别", "男");
        data.add(item);
        item = new HashMap<String, Object>();
        item.put("姓名", "小李师傅");
        item.put("性别", "女");
        data.add(item);
    }
}

 

4.轨迹球 事件

public class ActivityList4 extends Activity {
    ListView listView;
    private String[] data = { "eoeInstaller", "eoeDouban", "eoeWhere",
            "eoeInfoAssistant", "eoeDakarGame", "eoeTrack" };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = new ListView(this);
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data));
        setContentView(listView);
        OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener(){

            public void onItemSelected(AdapterView<?> parent, View arg1,int position, long arg3) {
                setTitle("您选中的软件是:  "+parent.getItemAtPosition(position).toString());
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                setTitle("");
            }
        };
        listView.setOnItemSelectedListener(itemSelectedListener);
    }
}

posted on 2011-05-10 22:08  冯瑞涛  阅读(2307)  评论(0编辑  收藏  举报