ListView 搭配SimpleAdapter

  这是SimplerAdapter的构造函数

public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            @LayoutRes int resource, String[] from, @IdRes int[] to) {
        mData = data;
        mResource = mDropDownResource = resource;
        mFrom = from;
        mTo = to;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

from A list of column names that will be added to the Map associated with each

* item.
to The views that should display column in the "from" parameter. These should all be
* TextViews. The first N views in this list are given the values of the first N columns
* in the from parameter.

context:是上下文;

 List<?extends Map<String,?>> data:这里是listview的数据来源,最外层是一个list数据结构list泛型指定的是继承了Map的对象。这里准备使用ArrayList<HashMap<String,String>>;

resource:这是listview的布局文件注意着是@LayoutRes那么就应该是R.layout里的xml文件

from:看注释这里是指定数据源中Map的column的名字,也就是说是指定Map<key,value>中key;是我们添加数据的索引。

to:这是需要显示的TextView(这里必须是TextView)的id,对应的是Map<key,value>中value,将是我们可见的内容。

那么ListView的布局如下:hashmap_item.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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/hash_title"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/hash_aticle"/>
</LinearLayout>

 

 主要代码如下

 private ArrayList<HashMap<String,String>> item;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.my_listview);
        DataInit();
        SimpleAdapter sa = new SimpleAdapter(this,item,R.layout.hashmap_item,new String[]{"title","article"},new int[]{R.id.hash_title,R.id.hash_aticle});
        listView.setAdapter(sa);
    }

    private void DataInit(){
        item = new ArrayList<>(20);
        for(int i=0;i<30;i++){
            HashMap<String,String> hashmap = new HashMap<>();
            hashmap.put("title","title:"+i);
            hashmap.put("article","article:"+i);
            item.add(hashmap);
        }
    }

 activity_main.xml定义如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.skymaster.hs.arrayadapter.MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/my_listview" />
</RelativeLayout>

程序效果图

 

posted @ 2016-09-20 16:10  麦哈顿博士  阅读(694)  评论(0编辑  收藏  举报