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
随机推荐
- php 利用转转法去除重复数组
w3scool学习地址 http://www.w3school.com.cn/tiy/s.asp?f=demo_php_func_array_flip 利用array键名不能重复的原理,使用两次 ar ...
- MongoDB查询指定字段(field)返回指定字段的方法
使用MongoDB的时候需要只查询指定的字段进行返回,也就是类似mysql里面的 SELECT id,name,age 这样而不是SELECT *.在MongoDB里面映射(projection)声明 ...
- 并发编程之socketserver模块
一.socketserver模块介绍 基于tcp套接字,关键的就是两个循环,一个是链接循环,一个是通信循环 socketserver模块中分两大类:srever类(解决链接问题)和request类(解 ...
- Model層資料驗證
概述 上节我们学习了Model的数据在界面之间的传递,但是很多时候,我们在数据传递的时候为了确保数据的有效性,不得不给Model的相关属性做基本的数据验证. 本节我们就学习如何使用 System.Co ...
- selenium+phantomJS爬虫,适用于登陆限制强,点触验证码等一些场景
selenium是非常出名的自己主动化測试工具,多数场景是測试project师用来做自己主动化測试,可是相同selenium能够作为基本上模拟浏览器的工具,去爬取一些基于http request不能或 ...
- Is "UNION ALL" Always Better Than "UNION"? Watch Out!
无论是教科书还是平常的实践都告诉我们 - “尽量避免用UNION,尽可能用UNION ALL替代”. 原因很简单,UNION会对结果集进行排序去重操作,这是一个很消耗资源的操作. 但是,今天碰到了一个 ...
- C++windows内核编程笔记day13 进程、线程与信号量
Windows进程 进程是一个容器,包括程序运行须要的代码.数据.资源等信息, windows进程的特点: 每一个进程都有自己的ID号 每一个进程都有自己的地址空间.进程之间无法訪问对方的地址空间. ...
- nginx负载均衡的策略
1.轮询(默认) 应用程序轮流来响应请求 2.最少连接(least-conn) 请求被分配到活动连接最少的服务器上 3.ip-hash 通过一个hash函数决定哪个服务器来响应用户的请求( ...
- angular学习笔记(十五)-module里的'服务'
本篇介绍angular中的模块:module 在笔记(二)http://www.cnblogs.com/liulangmao/p/3711047.html里已经讲到过模块,这篇主要讲模块的 '服务' ...
- web 安全问题(一):CSRF 攻击
什么是CSRF CSRF是怎么产生的 CSRF的攻击对象 CSRG的攻击手段 CSRF的防御措施 什么是CSRF 全称是(Cross Site Request Forgery)跨站请求伪造.也就是恶意 ...