android学习---ListView

ListView

简单示例一

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

代码实现:

MyDataSource.java
package com.leaf.android;

import java.util.ArrayList;
import java.util.List;

public class MyDataSource {

    public MyDataSource() {
        // TODO Auto-generated constructor stub
    }

    public static List<String> getDataSource() {
        List<String> list = new ArrayList<String>();
        list.add("北京");
        list.add("上海");
        list.add("广州");
        list.add("深圳");
        return list;
    }
}

 

MianActivity.java
package com.leaf.android;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MianActivity extends Activity {

    private ListView listView;

    private ArrayAdapter<String> adapter;
    // 表示数据源
    private List<String> data = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        data = MyDataSource.getDataSource();
        adapter = new ArrayAdapter<String>(MianActivity.this,
                android.R.layout.simple_list_item_multiple_choice, data);
        listView = (ListView) this.findViewById(R.id.listview);
        // 给每一个选项添加单选操作
        // listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(adapter);
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                Toast.makeText(MianActivity.this, "click me", 1).show();
                return false;
            }
        });
    }
}

效果:单选   多选

 示例二:

布局文件:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/pname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:text="产品名称"
            android:textSize="15sp" >
        </TextView>

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:text="产品价格"
            android:textSize="15sp" >
        </TextView>

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:text="产品地址"
            android:textSize="15sp" >
        </TextView>
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

 

实现代码

MyDataSource.java

package com.leaf.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyDataSource {

    public MyDataSource() {
        // TODO Auto-generated constructor stub
    }

    public static List<Map<String, String>> getMaps() {
        List<Map<String, String>> listMaps = new ArrayList<Map<String, String>>();
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("pname", "西瓜");
        map1.put("price", "$1.5");
        map1.put("address", "海南");

        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("pname", "香蕉");
        map2.put("price", "$2.5");
        map2.put("address", "海南");

        Map<String, String> map3 = new HashMap<String, String>();
        map3.put("pname", "苹果");
        map3.put("price", "$5.5");
        map3.put("address", "海南");
        listMaps.add(map1);
        listMaps.add(map2);
        listMaps.add(map3);
        return listMaps;

    }
}

Main.java

package com.leaf.android;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Main extends Activity {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ListView) this.findViewById(R.id.listview);
        data = MyDataSource.getMaps();
        adapter = new SimpleAdapter(Main.this, data, R.layout.main,
                new String[] { "pname", "price", "address" }, new int[] {
                        R.id.pname, R.id.price, R.id.address });
        listView.setAdapter(adapter);
    }
}

效果:

posted on 2013-09-04 10:47  leafu  阅读(294)  评论(0编辑  收藏  举报

导航