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
随机推荐
- mysql-5.7.20 版本的 mysql-group-replication 可用性测试报告
一.喜迎 mysql-5.7.20 事实上mysql-group-replication 功能是在mysql-5.7.17这个版本上引入的,它实现了mysql各个结点间数据强一致性, 这个也成为了我 ...
- Verilog学习笔记
作者:桂. 时间:2017-06-24 11:07:40 链接:http://www.cnblogs.com/xingshansi/p/7039237.html 前言 Verilog是硬件描述语言, ...
- Spring依赖注入构造器注入(通过构造函数注入)
在src目录下建立applicationContext.xml (Spring 管理 bean的配置文件) <?xml version="1.0" encoding=&q ...
- PLSA-概率潜语义分析(二)
PLSA最大化下面函数: 简化后,最大化下面函数: . ------------------------------------------------------------------------ ...
- AFNetworking、ASIHTTPRequest中SSL的使用
首先介绍下AFNetworking中的使用: 2.0要注意个地方:IOS7及其以后,採用AFHTTPSessionManager,IOS7之前採用AFHTTPRequestOperationManag ...
- 怎样设置linux中Tab键的宽度(可永久设置)
和我的上篇文章一样,能够设置当前用户的Tab键宽度.也能够设置全部用户的Tab键宽度 一.仅设置当前用户的Tab键宽度 输入命令:vim ~/.vimrc 然后:set tabstop=4 //我 ...
- 2014年百度之星资格赛第四题Labyrinth
Problem Description 度度熊是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫.该迷宫仅仅能从矩阵左上角第一个方格開始走.仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能 ...
- iOS_20_微博Dock的尾随切换
终于效果图:Dock尾随HomeVC一起切换 要求: 当点击HomeVC里面的微博列表的某一行时候, push到StatusDetail微博详情控制器,而且Dock也一起消失 当点击StatusDet ...
- C++学习笔记16,C++11中的显式的默认构造函数以及显示删除默认构造函数
在早期的C++中.假设须要一些接受一些參数的构造函数,同一时候须要一个不接收不论什么參数的默认构造函数.就必须显示地编写空的默认构造函数.比如: //tc.h class A{ private: in ...
- Rewrite MSIL Code on the Fly with the .NET Framework Profiling API
http://clrprofiler.codeplex.com/ http://blogs.msdn.com/b/davbr/archive/2012/11/19/clrprofiler-4-5-re ...