【Android 学习】之ListView使用大全

简介: 版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u013132758。 https://blog.csdn.net/u013132758/article/details/49204949 Android 学习之ListView使用大全ListView是列表组件,是android中常用的组件,列表显示信息由三个部分组成。
版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u013132758。 https://blog.csdn.net/u013132758/article/details/49204949

Android 学习之ListView使用大全

ListView是列表组件,是android中常用的组件,列表显示信息由三个部分组成。
  • ListView组件。
  • 适配器,用来将用来显示的数据映射到ListView组件中。
  • 列表中要显示的数据。
Listview 的适配器有 ArrayAdapter   SimpleAdapter  SimpleCurdorAdapter。
ArrayAdapter:只显示一行文字。

SimpleAdapter:自定义显示内容。可以有图片、文字,单选框,按钮等。

SimpleCursorAdapter:以列表的形式显示数据库中的内容。例如显示联系人。

1、ArrayAdapter

.java文件
package com.example.leiqi.listview;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * ListView的使用ArrayAdapter
 */
public class L extends AppCompatActivity {
    private LinearLayout mlayout;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_l);

        mlayout = (LinearLayout) this.findViewById(R.id.mlayout);
        listView = new ListView(this);
        //创建ArrayAdapter适配器,android.R.layout.simple_expandable_list_item_1:系统定义好的只显示一行文字。
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,getData());
        listView.setAdapter(adapter);
        mlayout.addView(listView);

    }
/**
 * 获取数据
 * @return List
 */
public List<String> getData(){
    List<String> mlist = new ArrayList<String>();
    mlist.add("数据项1");
    mlist.add("数据项2");
    mlist.add("数据项3");
    return mlist;
}


}

对应的布局文件
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:id="@+id/mlayout"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.leiqi.listview.L">
</LinearLayout>

2、SimpleAdapter

SimpleAdapter是简单且较为灵活的适配器,可以自定义XML显示每一行数据的内容。可以放图片、按钮、单选框等。
本文示例展示一个手机商品的列表,自定义布局文件listviewrow.xml实现列表中的每一行的不具备,在构建SimpleAdapter对象时加载listviewrow.xml布局,并对相应的列表信息赋值。

.java文件
package com.example.leiqi.cehua;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

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

/**
 *
 */
public class L2 extends Activity {


    private LinearLayout mlayout;
    private ListView mlist;
    //创建List对象,用于存放列表项没一行的列表信息
    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_l2);

        mlayout =(LinearLayout) findViewById(R.id.mlayout2);

        mlist = new ListView(this);
        //创建布局参数
        LinearLayout.LayoutParams mlistParams = new LinearLayout.
                LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT);
        //当拖拽列表时现实的颜色默认为黑色,这里设置为白色
        mlist.setCacheColorHint(Color.WHITE);
        //将列表list添加到线性布局mlayout中
        mlayout.addView(mlist,mlistParams);
        /**
         * 构建SimpleAdapter对象,构造函数共有5个参数
         * 第一个参数的含义是上下文Context
         * 第二个参数的含义是布局资源文件这里自定义的列表项布局文件
         * 第三个参数的含义是HashMap中的key信息img,name,money,zhe
         * 第四个参数的含义是listViewrow.xml 文件中的组件id
         * 第五个参数的含义是listViewrow.xml 文件中的组件id
         */
        SimpleAdapter adapter = new SimpleAdapter(this,getdata(),R.layout.listviewrow,
                new String[]{"img","name","mony","Discount"},new int[]{
                R.id.image,R.id.phonename,R.id.phonemony,R.id.phoneDiscount});
        //为列表添加适配器
        mlist.setAdapter(adapter);
        mlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             * 单击触发事件
             * parent:发生单击事件的Adapterview
             * view;被单机的View
             * position:当前单击的行在adapter 的下标
             * id:当前单击的行的id
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(L2.this,
                        "您选择地是"+list.get(position).get("name").toString(),Toast.LENGTH_SHORT).show();
            }
        });

    }
/**
 * 获取列表项显示的数据
 */
public List<Map<String,Object>> getdata()
{
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("img", R.drawable.l1);
    map.put("name", "摩托罗拉(moto)XT711 5G");
    map.put("mony", "2669元");
    map.put("zhe", "9折");
    list.add(map);
    map = new HashMap<String,Object>();
    map.put("img", R.drawable.l2);
    map.put("name", "Iphone 4S 3G");
    map.put("mony", "2669元");
    map.put("zhe", "9折");
    list.add(map);
    map = new HashMap<String,Object>();
    map.put("img",R.drawable.l3);
    map.put("name","三星 11 5G");
    map.put("mony", "2669元");
    map.put("zhe", "9折");
    list.add(map);
return list;
}
}
对应的布局文件
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/mlayout2"
    tools:context="com.example.leiqi.cehua.L2"
    android:orientation="horizontal">


</LinearLayout>

listviewrow.xml 在layout目录下、
<?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="fill_parent"
    android:background="@drawable/trippoilistviewbg" >
    <!--用来显示图片-->
    <ImageView
        android:id="@+id/image"
        android:layout_width="68dp"
        android:layout_height="65dp" android:layout_margin="10dp" />
     <!--手机价格,名称,打折信息-->
        <LinearLayout
            android:orientation="vertical"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/phonename"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/phonemony"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/phoneDiscount"
                android:textColor="#ffFF0000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
</LinearLayout>

列表按下、获取焦点、选中时的效果res/drawable/trippoilistviewbg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--设置按下时的效果-->
    <item android:state_pressed="true" android:drawable="@color/beijin"></item>
    <!--设置选中时的效果-->
    <item android:state_selected="true" android:drawable="@color/beijin1"></item>
    <!--设置选中时的效果-->
    <item android:state_focused="true" android:drawable="@color/beijin2"></item>
</selector>
                     

3.SimpleCursorAdapter

下面的例子是将通讯录中的内容显示在列表中,示例代码如下:
.java 文件
<span style="color:#330000;">package com.example.leiqi.cehua;

import android.app.Activity;
import android.database.Cursor;
import android.provider.Contacts;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 *
 */
public class L3 extends Activity {
private ListView listView;
    private LinearLayout mlayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_l3);
    mlayout = (LinearLayout) findViewById(R.id.l3);

        listView = new ListView(this);
        Cursor cursor = getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);
        startManagingCursor(cursor);
        ListAdapter listAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_expandable_list_item_1,
                cursor,new String[]{Contacts.People.NAME},
                new int[]{ android.R.id.text1});
        listView.setAdapter(listAdapter);
        mlayout.addView(listView);
    }


}
</span>
对应的布局文件
<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:id="@+id/l3"
    tools:context="com.example.leiqi.cehua.L3">



</LinearLayout>

源码下载地址
相关文章
|
9月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
175 0
|
9月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
119 2
|
5月前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
第一个安卓项目 | 中国象棋demo学习
|
9月前
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
4月前
|
Web App开发 编解码 视频直播
视频直播技术干货(十二):从入门到放弃,快速学习Android端直播技术
本文详细介绍了Android端直播技术的全貌,涵盖了从实时音视频采集、编码、传输到解码与播放的各个环节。文章还探讨了直播中音视频同步、编解码器选择、传输协议以及直播延迟优化等关键问题。希望本文能为你提供有关Andriod端直播技术的深入理解和实践指导。
90 0
|
5月前
|
Android开发
Android学习 —— 测试init.rc中的条件触发的处理顺序
Android学习 —— 测试init.rc中的条件触发的处理顺序
|
6月前
|
搜索推荐 Android开发
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
本文分享了不同价位电脑配置对于编译AOSP安卓系统源代码的影响,提供了从6000元到更高价位的电脑配置实例,并比较了它们的编译时间,以供学习AOSP源代码时电脑配置选择的参考。
389 0
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
|
8月前
|
API Android开发 开发者
`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView
【6月更文挑战第26天】`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView。它提供高效的数据视图复用,优化的布局管理,支持多种布局(如线性、网格),并解耦数据、适配器和视图。RecyclerView的灵活性、性能(如局部刷新和动画支持)和扩展性使其成为现代Android开发的首选,特别是在处理大规模数据集时。
100 2
|
8月前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
288 2
|
9月前
|
存储 定位技术 开发工具
Android 开发前的设计,Android之内存泄漏调试学习与总结
Android 开发前的设计,Android之内存泄漏调试学习与总结

热门文章

最新文章

  • 1
    如何修复 Android 和 Windows 不支持视频编解码器的问题?
  • 2
    Android历史版本与APK文件结构
  • 3
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 4
    【04】flutter补打包流程的签名过程-APP安卓调试配置-结构化项目目录-完善注册相关页面-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程
  • 5
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 6
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 7
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 8
    Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
  • 9
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 1
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    24
  • 2
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    31
  • 3
    Android历史版本与APK文件结构
    119
  • 4
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    27
  • 5
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    23
  • 6
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
    55
  • 7
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    36
  • 8
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
    71
  • 9
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    116
  • 10
    Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
    29