zoukankan      html  css  js  c++  java
  • ListView 及 SimpleAdapter的简单案例

    SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便

    SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

    参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文

    参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致

    参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id

    参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称

    参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应

    下面就举例说明

    listview.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="vertical" >
        
        
        <ListView 
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            
            
        </ListView>
        
    
    </LinearLayout>

    listview_item.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" >
        
        <ImageView 
            android:id="@+id/touxiang"
            android:src="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            />
        <RelativeLayout 
           android:layout_weight="1"
           android:layout_height="wrap_content"
           android:layout_width="0dp"
           android:layout_margin="10dp"
            >
            <TextView 
                android:id="@+id/tv_name"
                android:textSize="20sp"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"            
                android:text="ads"         
                />
            <TextView 
                android:id="@+id/tv_message"
                android:textSize="20sp"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"   
                android:layout_below="@id/tv_name"
                android:text="ads"         
                />
            
            
        </RelativeLayout>
        
    </LinearLayout>

    MainActivity.java

    public class MainActivity extends Activity {
    
        private SimpleAdapter simAdapt;
        private ListView listView;
        private List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listview);
    
            for (int i = 0; i < 20; i++) {
                Map<String, Object> item = new HashMap<String, Object>();
                item.put("touxiang", R.drawable.ic_launcher);
                item.put("name", "某某" + i);
                item.put("message", "今晚我请客吃饭");
                data.add(item);
            }
    
            listView = (ListView) findViewById(R.id.listview1);
    
            simAdapt = new SimpleAdapter(
                    this, 
                    data, 
                    R.layout.listview_item,
                    new String[] { "touxiang", "name", "message" },// 与下面数组元素要一一对应
                    new int[] { R.id.touxiang, R.id.tv_name, R.id.tv_message });
    
            listView.setAdapter(simAdapt);
    
        }
    }

    以上就是ListView 和 SimpleAdapter 的简单使用

  • 相关阅读:
    [PHP] laravel框架注意bootstrap/cache
    [git] git拉取远程分支代码
    Prometheus监控系统(4)pushgateway及自定义脚本
    k8s 学习
    pxc /Galera Cluster详解
    Percona Monitoring and Management
    etcd获取所有key
    kubernetes Pod驱逐迁移drain用法
    kubernetes etcd数据管理
    办公环境下k8s网络互通方案
  • 原文地址:https://www.cnblogs.com/nyzxh/p/6110077.html
Copyright © 2011-2022 走看看