zoukankan      html  css  js  c++  java
  • Android 之 ListView使用SimpleAdapter展示列表

    SimpleAdapter是用的比较多的一种adapter,它的拓展性很好,可以自己定义布局,也可以设置一些图片等。

    对于SimpleAdapter一般都需要自定义一个xml文件,是一个列表行的布局文件。

    simpleitem.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="2" />
    
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="10"/>
    
        <TextView
            android:id="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="3"/>
    
    </LinearLayout>

    SimpleAdapterDemo.java

    package com.example.phonedemo;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import com.example.phonedemo.util.Utils;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    public class SimpleAdapterDemo extends Activity {
    
        private ListView listView = null;
        private LinearLayout layout = null;
        private List<Map<String, Object>> list = null;
        private SimpleAdapter adapter = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            this.layout = new LinearLayout(this);
            this.layout.setOrientation(LinearLayout.VERTICAL);
            this.listView = new ListView(this);
            list = initList();
            adapter = new SimpleAdapter(this, list, R.layout.simpleitem,
                    new String[] { "id", "name", "age" }, new int[] { R.id.id,
                            R.id.name, R.id.age });
            this.listView.setAdapter(adapter);
            this.layout.addView(this.listView, Utils.match);
            super.addContentView(this.layout, Utils.match);
        }
    
        private List<Map<String, Object>> initList() {
            List<Map<String, Object>> temp = new ArrayList<Map<String, Object>>();
            Map<String, Object> map = null;
            for (int i = 0; i < 20; i++) {
                map = new HashMap<String, Object>();
                map.put("id", i);
                map.put("name", "张三" + i + "号");
                map.put("age", 28);
                map.put("email", "zhangsan@163.com");
                temp.add(map);
            }
            return temp;
        }
    }

     

  • 相关阅读:
    libsvm参数学习和核函数使用(转载)
    libsvm 训练后的模型参数讲解(转)
    Ternary Search Trees 三分搜索树
    分支界定法 branch-and-bound 分析与实现)(转载)
    几种常见的激活函数(转载)
    matlab神经网络实验
    递归神经网络2(转载)
    线性代数和numpy——黑板客老师课程学习
    关于 Intellij IDEA Ultimate Edition 14.1控制台中文乱码 解决
    Intellij IDEA Ultimate Edition 14.1 破解
  • 原文地址:https://www.cnblogs.com/waddell/p/3402974.html
Copyright © 2011-2022 走看看