ListView simple adapter

layout代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.example.my.myapplication.TestActivity4">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_2">

    </ListView>
</LinearLayout>

Activity代码:

package com.example.my.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestActivity4 extends AppCompatActivity {

    ListView lv_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test4);

        lv_2=(ListView)findViewById(R.id.lv_2);

        //1.数据集合 layout文件

        List<Map<String,Object>> lm=new ArrayList<Map<String, Object>>();

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("img",R.drawable.f1);
        map.put("name","美食1");
        map.put("content","美食1的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f2);
        map.put("name","美食2");
        map.put("content","美食2的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f3);
        map.put("name","美食3");
        map.put("content","美食3的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f4);
        map.put("name","美食4");
        map.put("content","美食4的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f5);
        map.put("name","美食5");
        map.put("content","美食5的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f6);
        map.put("name","美食6");
        map.put("content","美食6的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f7);
        map.put("name","美食7");
        map.put("content","美食7的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f8);
        map.put("name","美食8");
        map.put("content","美食8的介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f9);
        map.put("name","美食9");
        map.put("content","美食9的介绍");
        lm.add(map);

        //数组 key的数据
        String [] strings={"img","name","content"};
        int[] ids={R.id.iv_1,R.id.tv_7,R.id.tv_8};
        //2.创建
        SimpleAdapter simpleAdapter =new SimpleAdapter(this,lm,R.layout.simple_adapter,strings,ids);
        lv_2.setAdapter(simpleAdapter);
    }
}

Item布局代码:

<?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="match_parent">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/f1"
        android:id="@+id/iv_1"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="名字=aaa"
            android:id="@+id/tv_7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="内容=aaa"
            android:id="@+id/tv_8"/>
    </LinearLayout>
</LinearLayout>

效果图:

时间: 2024-08-29 09:23:28

ListView simple adapter的相关文章

布局与控件(九)-ListView的Adapter们

第10节 ListView的Adapter 安卓系统为ListView设计了多种Adapter作为它的搭档.每种Adapter不仅为ListView提供数据内容,也会告诉ListView如何展示这些数据-规定好列表项的长相. 这些Adapter都是从Adapter类继承而来的,它们的关系如下: 这里我们选择性的介绍常见的2种Adapter-ArrayAdapter和SimpleAdapter. 10.1 ArrayAdapter ArrayAdapter是最简单的Adapter,我们在前面已经使

ListView和Adapter信息显示

<?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:id="@+id/activity_main" andr

ListView和Adapter数据适配器的简单介绍

ListView 显示大量相同格式数据 常用属性: listSelector            listView每项在选中.按下等不同状态时的Drawable divider                ListView每项间的间隔Drawable dividerHeight        ListView每项间间隔的间隔高度 常用方法: setAdapter()                设置数据适配器 setOnItemClickListener()        设置每项点击事件

ListView和Adapter的配合使用以及Adapter的重写

ListView和Adapter的使用 首先介绍一下ListView是Android开发过程中较为常见的组件之一,它将数据以列表的形式展现出来.一般而言,一个ListView由以下三个元素组成: 1.View,用于展示列表,通常是一个xml所指定的.大家都知道Android的界面基本上是由xml文件负责完成的,所以ListView的界面也理所应当的使用了xml定义.例如在ListView中经常用到的“android.R.layout.simple_list_item”等, 就是Android系统

listview及adapter

http://blog.csdn.net/shaojie519/article/details/6595720 http://blog.csdn.net/liuhe688/article/details/6532519 http://tech.cncms.com/shouji/android/78565.html http://blog.csdn.net/wangjinyu501/article/details/7716785 http://blog.csdn.net/wangkuifeng01

Android ListView 和 Adapter 从本地/网络获取歌曲列表

本文内容 环境 项目结构 演示1:SimpleAdapter 演示2:BaseAdapter 演示3:customlazylist 演示4:customcompletelazylist 本文只给出演示概要,代码太多,贴出来意义不大,自己下载调试一下,点击此处下载. 本文通过四个示例,循序渐进地演示,将歌曲列表加载到 ListView 控件,歌曲列表,包括缩略图.歌手名.歌曲名等信息,或存放在本地,或以 JSON 形式存放在网络. 环境 Windows 2008 R2 64 位 Eclipse A

ListView及Adapter的使用

一.使用ArrayAdapter 其中ArrayAdapter的构造函数有如下几个,其中resource是指每个列表项的布局文件,objects是指列表项的数据源,此处通常指一个数组 ArrayAdapter(Context context, int resource) ArrayAdapter(Context context, int resource, int textViewResourceId) ArrayAdapter(Context context, int resource, T[

第十九讲:ListView与Adapter(一)

天将降大任于是人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为.--<孟子·告子下> 本讲内容:ListView列表组件 与 Adapter适配器的用法 一.ListView列表组件: 作用:ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作(通过绑定监听器). 创建一个ListView需要3个元素. (1)ListView展示每一列的View. (2)填入View的数据或者图片等. (3)连接数据与ListView的适配器. ListVi

第十九讲:ListView与Adapter(二)

会当凌绝顶,一览众山小. -- 杜  甫<望岳> 本讲内容:ListView列表组件 与 Adapter适配器的用法 一.ListView使用SimpleAdapter 很多时候需要在列表中展示一些除了文字以外的东西,比如图片等.这时候可以使用SimpleAdapter.可以通过它 使用simpleAdapter的数据一般都是用HashMap构成的列表,列表的每一节对应ListView的每一行.通过SimpleAdapter的构造函数,将HashMap的每个键的数据映射到布局文件中对应控件上.