zoukankan      html  css  js  c++  java
  • 安卓学习第12课——SimpleAdapter

    SimpleAdapter一点也不简单,他的参数就带了5个,天哪,晕了。。

    今天学的这个适配器, public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

    看这个大概明白,参数分别是第一个:表示访问整个android应用程序接口,基本上所有的组件都需要,一般都写this(改天研究一下),第二个应该是这个List对象名,第三个是list的id,第4,5指的分别是他的list里面的Map存的内容,String类型和其他类型(他的id)。

    <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" >
    <ListView 
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <!-- 定义一个ImageView,用于作为列表项的一部分。 -->
    <ImageView android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingLeft="10dp"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <!-- 定义一个TextView,用于作为列表项的一部分。 -->
    <TextView android:id="@+id/name"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#f0f"
        android:paddingLeft="10dp"
        />
    <!-- 定义一个TextView,用于作为列表项的一部分。 -->
    <TextView android:id="@+id/desc"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:paddingLeft="10dp"
        />
    </LinearLayout>
    </LinearLayout>
    package com.example.simpleadapter;
    
    
    
    import java.util.ArrayList;
    import java.util.HashMap;
    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 MainActivity extends Activity {
    //在xml中创建一个listview,用于显示simpleAdapter的列表项
        private String[] names=new String[]{"虎头","弄玉","李清照","李白"};
        private String[] descs=new String[]{"可爱的小孩","一个擅长音乐的女孩","一个擅长文学的女性","浪漫主义诗人"};
    private int[] imagesIds=new int[]{R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai};    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //创建一个List集合,集合元素师Map
            List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
            for(int i=0;i<names.length;i++){
                Map<String,Object> listItem=new HashMap<String,Object>();
                listItem.put("header", imagesIds[i]);
                listItem.put("personname", names[i]);
                listItem.put("desc", descs[i]);
                listItems.add(listItem);
            }
            //创建一个SimpleAdapter
            SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.simple_item,
                    new String[]{"personname","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});
            ListView list=(ListView) findViewById(R.id.mylist);
            list.setAdapter(simpleAdapter);
        }
    
    }

    我认为这个代码实现的过程是:创建一个listview,实现simpleAdapter列表项,在创建的simpleAdapter里面写下列表项内容。

    备注:这个学习了SimpleAdapter,list,HashMap.

  • 相关阅读:
    2015/8/28 回校正常学习工作
    Asp.net自定义控件开发任我行(3)-Render
    Asp.net自定义控件开发任我行(2)-TagPrefix标签
    Asp.net自定义控件开发任我行(1)-笑傲江湖
    ET采集阿里妈妈淘宝客商品规则
    淘宝API还能用来采集数据吗?taobao.item.get 接口还能用吗?
    淘宝api升级,无法采集淘宝的数据,taobao.item.get 和taobao.taobaoke.items.get都不能用
    用firefox浏览器访问localhost,apache http server 已停止工作
    淘宝客网站怎么批量采集淘宝商品,方维采集淘宝数据思路
    方维购物分享系统怎么样,方维系统安全性检测
  • 原文地址:https://www.cnblogs.com/Yvettey-me/p/3920903.html
Copyright © 2011-2022 走看看