Android 利用cursor来进行排序(转至http://blog.csdn.net/yangzongquan/article/details/6547860)
主要思路是:override move系列的方法,让cursor以自己想要的顺序来移动,从而达到对cursor排序的目的。比如数组A0里有 4(0),3(1),1(2),2(3),括号内为位置,排序后用数据记录A1:1(2),2(3),3(1),4(0)。要访问第一个元素,则访问 A1[0]得到1(2),根据(2)找到在A0中的实际位置2,即1(2)。参考了下系统的CursorWrapper和AbstractCursor代 码实现,另外有时间可以顺带了解下MatrixCursor。
package com.xx.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import android.database.Cursor;
import android.database.CursorWrapper;
import com.xx.test.SortCursor.SortEntry;
public class SortCursor extends CursorWrapper implements Comparator<SortEntry>{ public SortCursor(Cursor cursor) {
super(cursor);
} Cursor mCursor;
ArrayList<SortEntry> sortList = new ArrayList<SortEntry>();
int mPos = 0;
public static class SortEntry {
public String key;
public int order;
} public int compare(SortEntry entry1, SortEntry entry2) {
return entry1.key.compareTo(entry2.key);
} public SortCursor(Cursor cursor, String columnName) {
super(cursor); mCursor = cursor;
if (mCursor != null && mCursor.getCount() > 0) {
int i = 0;
int column = cursor.getColumnIndexOrThrow(columnName);
for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext(), i++) {
SortEntry sortKey = new SortEntry();
sortKey.key = cursor.getString(column);
sortKey.order = i;
sortList.add(sortKey);
}
}
Collections.sort(sortList, this);
}
public boolean moveToPosition(int position) {
if (position >= 0 && position < sortList.size()) {
mPos = position;
int order = sortList.get(position).order;
return mCursor.moveToPosition(order);
}
if (position < 0) {
mPos = -1;
}
if (position >= sortList.size()) {
mPos = sortList.size();
}
return mCursor.moveToPosition(position);
}
public boolean moveToFirst() {
return moveToPosition(0);
}
public boolean moveToLast() {
return moveToPosition(getCount() - 1);
}
public boolean moveToNext() {
return moveToPosition(mPos + 1);
}
public boolean moveToPrevious() {
return moveToPosition(mPos - 1);
}
public boolean move(int offset) {
return moveToPosition(mPos + offset);
}
public int getPosition() {
return mPos;
}
}
Android 利用cursor来进行排序(转至http://blog.csdn.net/yangzongquan/article/details/6547860)的更多相关文章
- Android 设计模式之观察者模式(转载自:“http://blog.csdn.net/fangchongbory/article/details/7774044”)
/* * 观察者模式 * 定义对象间的一种一个(Subject)对多(Observer)的依赖关系,当一个对象的状态发送改变时,所以依赖于它的 * 对象都得到通知并被自动更新 * * 当然, ...
- 快速掌握 Android Studio 中 Gradle 的使用方法 [转http://blog.csdn.net/feelang/article/details/41783317]
Gradle是可以用于Android开发的新一代的 Build System, 也是 Android Studio默认的build工具. Gradle脚本是基于一种JVM语言 -- Groovy,再加 ...
- [Android Pro] https://blog.csdn.net/gaugamela/article/details/79143309
原文地址:https://blog.csdn.net/gaugamela/article/details/79143309 最近遇到这样一个问题: 第三方的SDK除了Jar包外,还提供了对应的so文件 ...
- android gradle,groovy--https://blog.csdn.net/hebbely/article/details/79074460
android grale,groovyhttps://blog.csdn.net/hebbely/article/details/79074460 Gradle编译时报错:gradle:peer n ...
- 排序算法 ----(转载::http://blog.csdn.net/hguisu/article/details/7776068)
1.插入排序—直接插入排序(Straight Insertion Sort) 基本思想: 将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表.即:先将序列的第1个记录看成是一个有序 ...
- Android 学习路线图(转载自https://blog.csdn.net/lixuce1234/article/details/77947405)
程序设计 一.java (a)基本语法(如继承.异常.引用.泛型等) Java核心技术 卷I(适合入门) 进阶 Effective Java中文版(如何写好的Java代码) Java解惑 (介绍烂Ja ...
- Android APK反编译详解(附图) (转至 http://blog.csdn.net/ithomer/article/details/6727581)
本文Android反编译教程,测试环境: Win7 Ultimate x64 Ubuntu 12.04 x86_x64 反编译工具包 下载 (2012-10-10更新) 一.Apk反编译得到Java源 ...
- 几种排序算法的比较转自http://blog.csdn.net/keenweiwei/article/details/3697452
1冒泡排序: 已知一组无需数据a[1],a[2],a[3],a[4],a[5][a[n],将其按升序排列,首先找出这组数据中最大值,将a[1]与a[2]比较,若a[1]大,则交换两者的值,否则不变,在 ...
- android 蓝牙 http://blog.csdn.net/u012843100/article/details/52384219
http://blog.csdn.net/u012843100/article/details/52384219
随机推荐
- 解决电脑需要切换IP带来的MySQL连接问题
直接上代码: import socket #获取本机电脑名 myname = socket.getfqdn(socket.gethostname( )) #获取本机ip myip = socket.g ...
- MYSQL 更改数据库data存储目录 创建用户 创建权限 设置远程访问的权限.
一. 怎么更改数据库data存储目录: 1. 安装MYSQL. 2. 切换到 C:\Program Files\MySQL\MySQL Server 5.6 3. 新建my.ini. 加入如下配置: ...
- Opening Default document on IIS (HTML With WebAPI)
Question: I've a deployed ASP.NET Web API with a website on the same folder that consume it. When I ...
- Vuejs搜索下拉框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AngularJS 使用 UI Router 实现表单向导
Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...
- Xilinx ISE Design Suite 14.7 ISim 简单仿真
1.创建完项目(以Xilinx ISE Design Suite 14.7开发流程的例子 led例子 为例),编译通过,我们就可以对这个项目进行仿真: 2.然后切换到simulation,然 ...
- Linux Awk使用案例总结(nginx日志统计,文件对比合并等)
知识点: 1)数组 数组是用来存储一系列值的变量,可通过索引来访问数组的值. Awk中数组称为关联数组,因为它的下标(索引)可以是数字也可以是字符串. 下标通常称为键,数组元素的键和值存储在Awk程序 ...
- find 命令查找文件大小为xx的文件
K:字节 G:gb 查找当前目录及子目录下大于1G的文件: # find ./ -size +1G -exec ls -lh {} \; 查找当前目录及子目录下大于1G小于20G的文件: # find ...
- jvisualvm工具使用
VisualVM 是Netbeans的profile子项目,已在JDK6.0 update 7 中自带(java启动时不需要特定参数,监控工具在bin/jvisualvm.exe). https:// ...
- angular学习笔记(二十六)-$http(4)-设置请求超时
本篇主要讲解$http(config)的config中的timeout项: $http({ timeout: number }) 数值,从发出请求开始计算,等待的毫秒数,超过这个数还没有响应,则返回错 ...