android基础知识08:android MatrixCursor源码解析

本文探讨了Android MatrixCursor的内部工作原理,包括数据存储结构、构造函数和获取列值的方法。MatrixCursor的数据行数由rowCount表示,列数由columnCount给出,数据以特定格式存储在data数组中。通过RowBuilder类,可以方便地访问和操作矩阵游标中的数据,提供类似于SQLite Content Provider的操作接口。
摘要由CSDN通过智能技术生成
        在上一篇文章中,我们对content provider基础进行了详细的介绍。该文中介绍的content provider都是基于sqlite的,但实际上content provider是可以基于其他存储格式的。本文将开始介绍基于xml的content provider。
        第一步,我们先介绍一个重要的部分Cursor。基于xml的content provider所使用的Cursor与基于sqlite的是不同的。基于xml的Cursor需要继承MatrixCursor类。本文先介绍MatrixCursor类。
       MatrixCursor类位于android.database包中。其中定义了四个变量,用来存储相关的值,其介绍如下:
private final String[] columnNames; 存放列名
private Object[] data;                      
存放数据值。这里,我通常认为应该用一个表(二维数组来存放数据值),但实际上却是用一个一维数组来模拟二维数组。所以对于一个二维数组:

其存放在data中的格式为

private int rowCount = 0;

MatrixCursor中存放的数据行数。因此很容易理解data数组的长度必须大于等于(columnCount*rowCount );

private final int columnCount;

列数=columnNames长度

        其构造函数为:

    /**
     * Constructs a new cursor with the given initial capacity.
     *
     * @param columnNames names of the columns, the ordering of which
     *  determines column ordering elsewhere in this cursor
     * @param initialCapacity in rows
     */
    public MatrixCursor(String[] columnNames, int initialCapacity) {
        this.columnNames = columnNames;
        this.columnCount = columnNames.length;

        if (initialCapacity < 1) {
            initialCapacity = 1;
        }

        this.data = new Object[columnCount * initialCapacity];
    }

    /**
     * Constructs a new cursor.
     *
     * @param columnNames names of the columns, the ordering of which
     *  determines column ordering elsewhere in this cursor
     */
    public MatrixCursor(String[] columnNames) {
        this(columnNames, 16);
    }
       其中的initialCapacity,就是指存放数据值的数组的容量。

       在前面

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值