在上篇博客,Android-ListView-SimpleCursorAdapter,中介绍了SimpleCurosrAdapter的使用操作(SimpleCursorAdapter是简单便捷Cursor数据处理的适配器,内部都已经封装好了),而CursorAdapter是属于自定义适配器范畴,虽然没有SimpleCursorAdapter那么方便,但灵活性比SimpleCursorAdapter要灵活些

在上篇博客,Android-ListView-(BaseAdapter初步)Android-ListView-(BaseAdapter使用),中介绍了BaseAdapter的使用操作等(BaseAdapter是对很多数据类型进行适配),而CursorAdapter是专门给Cursor数据进行适配的

BaseAdapter是CursorAdapter的父类

BaseAdapter是SimpleCursorAdapter的父类


定义 MyCursorAdapter适配器

package liudeli.cp.client.adapter;

import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView; import liudeli.cp.client.R; public class MyCursorAdapter extends CursorAdapter { // 定义布局加载器
private LayoutInflater layoutInflater; /**
* 构造方法
* @param context 传入上下文
* @param c 传入Cursor游标
* @param flags 传入标记
*/
public MyCursorAdapter(Context context, Cursor c, int flags) { /**
* 主要把这些值传递给父类的构造方法,就会自动的传递到 newView(Context context, Cursor cursor, ViewGroup parent)
* bindView(View view, Context context, Cursor cursor)
*/
super(context, c, flags); layoutInflater = LayoutInflater.from(context);
} /**
* Item布局文件的处理
* @param context 传入上下文
* @param cursor
* @param parent
* @return
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Item布局文件,要显示的Item,在这里处理
View view = layoutInflater.inflate(R.layout.layout_item, null);
return view;
} /**
* 把Cursor获取的数据和布局文件进行绑定
* @param view 此view 是上面 newView方法返回的View
* @param context 上下文
* @param cursor 游标
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
// 获取到布局的控件
TextView tvId = view.findViewById(R.id.tv_id);
TextView tvName = view.findViewById(R.id.tv_name);
TextView tvAge = view.findViewById(R.id.tv_age); // 获取Cursor里面的数据
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age")); // 把数据绑定到控件里面去
tvId.setText(_id + ""); // setText(数据必须是字符串);
tvName.setText(name); // setText(数据必须是字符串);
tvAge.setText(age + ""); // setText(数据必须是字符串);
}
}

Java代码使用CursorAdapter适配器

  /**
* 查询
*/
public void query(View view) {
cursor = contentResolver.query(uri,
new String[]{"_id", "name", "age"},
null, null
, null, null); /**
* 使用CursorAdapter 适配器
*/
CursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.IGNORE_ITEM_VIEW_TYPE); // 给ListView设置适配器
listview.setAdapter(adapter); }

Item布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"> <TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="id"
android:textColor="@android:color/black"
/> <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="name"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
/> <TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="age"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
/> </LinearLayout>

Android-ListView-CursorAdapter的更多相关文章

  1. android ListView 九大重要属性详细分析、

    android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...

  2. Android ListView onItemClick Not Work

    Android ListView onItemClick Not Work ListView item中有Button和RadioButton的时候,它的Item点击事件不起作用,需要设置item的属 ...

  3. 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...

  4. Android ListView 常用技巧

    Android ListView 常用技巧 Android TextView 常用技巧 1.使用ViewHolder提高效率 ViewHolder模式充分利用了ListView的视图缓存机制,避免了每 ...

  5. Android listview addHeaderView 和 addFooterView 详解

    addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 需要注意的是添加布局的时候应该添加从父容器开始添加 ...

  6. Android ListView滑动过程中图片显示重复错乱闪烁问题解决

    最新内容建议直接访问原文:Android ListView滑动过程中图片显示重复错乱闪烁问题解决 主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及L ...

  7. Android --ListView分页

    参考博客:Android ListView分页加载(服务端+android端)Demo 监听OnScrollListener事件 class OnListScrollListener implemen ...

  8. Android ListView ListActivity PreferenceActivity背景变黑的问题ZT

    Android ListView ListActivity PreferenceActivity背景变黑的问题 ListView在滚动时背景会变暗甚至变黑,这个要从Listview的效果说起,默认的L ...

  9. android listview去掉分割线

    1:android listview去掉分割线 1>设置android:divider="@null" 2>android:divider="#0000000 ...

  10. 【转】android ListView 几个重要属性

    android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...

随机推荐

  1. winform频繁刷新导致界面闪烁解决方法

    转自龙心文 原文 winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供 ...

  2. “C# 未在本地计算机上注册microsoft.Jet.OLEDB.12.0”的解决方案

    在进行Access数据库进行操作时,连接字符串为: OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLED ...

  3. 【开发工具】Jenkins+Gitlab实现自动化部署

    我在尝试在容器中安装Jenkins时,初衷是希望使用docker in docker 的模式来实现Jenkins slave容器按需创建.在实现的时候需要在Jenkins 中安装Kubernetes插 ...

  4. centos 和KVM安装

  5. Struts2 的核心、执行原理

    转自: http://www.cnblogs.com/xiadongqing/p/5240615.html 在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样 ...

  6. 【309】◀▶ Windows 相关功能实现

    目录: 共享文件夹失败的解决方法 导 栅 添 1. 共享文件夹失败的解决方法 参考:解决“你没有权限访问,请与网络管理员联系” 参考:WIN7局域网文件共享设置方法 2. 导 在 3. 栅 栅 4. ...

  7. android-tip-关于SpannableString的使用

    如果想单独设置TextView上其中几个字的样式,该怎么办? 答案是使用SpannableString. 使用SpannableString可以为TextView上的某字或某些字设置: 前景色(For ...

  8. Python学习笔记_使用openpyxl操作Excel,在同一个文件里复制某一个sheet

    应用场景:定制一个Excel模板文件,其中定义了一个模板Sheet,以此模板文件里的模板sheet为样例,制作报表,里面有不止一个模板样例Sheet 一.软件环境: 1.OS:Win10 64位 2. ...

  9. ROS Learning-032 (提高篇-010 Launch)Launch 深入研究 --- (启动文件编程)ROS 的 XML语法简介

    ROS 提高篇 之 Launch 深入研究 - 01 - 启动文件的编程 - ROS 的 XML语法简介 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubu ...

  10. 相机IMU融合四部曲(二):误差状态四元数详细解读

    相机IMU融合四部曲(二):误差状态四元数详细解读 极品巧克力 前言 上一篇文章,<D-LG-EKF详细解读>中,讲了理论上的SE3上相机和IMU融合的思想.但是,还没有涉及到实际的操作, ...