android 28 SimpleAdapter

简介:
监听器返回fasle,则事件还会分发给其他监听器。
SimpleAdapter是BaseAdapter的子类,对适配器进行了简化,数据的格式是List,List的元素必须是Map, public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to) {//Activity是Context的子类,resource是每一行ListViewItem布局,from是Map中头像和名字的所有的键,所有头像的健和所有名字的健,to是ListViewItem(每一行)布局的id值,
开发不灵活。
复制代码

 MainActivity.java

复制代码
package com.sxt.day05_02;

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 {

    ListView mlvGenerals;//<ListView/>
    List<Map<String, Object>> mGenerals;
    SimpleAdapter mAdapter;
    
    static final String KEY_PHOTOID="photoid";
    static final String KEY_NAME="name";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();//初始化数据
        initView();
    }

    private void initView() {
        mlvGenerals=(ListView) findViewById(R.id.lvGeneral);
        mAdapter=new SimpleAdapter(this, mGenerals, 
            R.layout.item_generals, 
            new String[]{KEY_PHOTOID,KEY_NAME}, 
            new int[]{R.id.ivThumb,R.id.tvName});
        mlvGenerals.setAdapter(mAdapter);
    }

    private void initData() {
        int[] resid={
                R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
                R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
                R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
                R.drawable.zhuyuanzhang
        };
        String[] names=getResources().getStringArray(R.array.generals);//R.array.generals是string.xml中定义的数组,
        mGenerals=new ArrayList<Map<String,Object>>();
        HashMap<String, Object> general;
        for (int i = 0; i < names.length; i++) {//List是一个集合,
            general=new HashMap<String, Object>();//集合里面是Map
            general.put(KEY_PHOTOID, resid[i]);//每一个Map里面放了2个键值对
            general.put(KEY_NAME, names[i]);
            mGenerals.add(general);
        }
    }

}
复制代码

activity_main.xml

复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/lvGeneral"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#ccc"
        android:dividerHeight="2dp"/>

</RelativeLayout>
复制代码
item_generals.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="wrap_content"
    android:orientation="horizontal" >
    <ImageView 
        android:id="@+id/ivThumb"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="fitXY"
        android:src="@drawable/baiqi"/>
    <TextView 
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_marginLeft="10dp"
        android:text="白起"
        android:textSize="20sp"
        android:gravity="center_vertical"/>
</LinearLayout>
复制代码



本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4887182.html,如需转载请自行联系原作者

相关文章
|
XML Java Android开发
Android安卓——SimpleAdapter的简单使用
本文是博主对Adapter(适配器)的一些理解,为了加深对Adapter的理解以及记录自己的阶段学习而写,同时也适合初学者阅读,参考本条博客的逻辑进行学习。
102 0
|
Android开发
Android零基础入门第41节:使用SimpleAdapter
原文:Android零基础入门第41节:使用SimpleAdapter    通过ArrayAdapter实现Adapter虽然简单、易用,但ArrayAdapter的功能比较有限,它的每个列表项只能给一个TextView动态填充内容。
1619 0
|
Android开发 数据格式 XML
Android使用SimpleAdapter
SimpleAdapter的使用步骤如下: 声明ListView,并进行初始化操作 准备数据集,一般用list来实现,当然也可以使用数组 为listview适配simpleadapter 如下代码: 声明ListView private ListView mL...
1120 0
|
Android开发 容器
Android——继承SimpleAdapter重写自己的getView
                            在上篇Android——ListView实现简单列表 中,利用SimpleAdapter实现了简单的列表绑定。在本文中,主要是通过重写getView方法,来看下adapter内部绑定数据大致是个什么流程。
1426 0