zoukankan      html  css  js  c++  java
  • 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;
        }
    }
     
  • 相关阅读:
    vue 路由嵌套 (子路由跳转报错或者失效解决方法)
    vue-cli3主题色系统
    vue项目哀悼日
    el-upload 文件上传显示进度
    课表
    uni-app 开发钉钉小程序
    Linux/Centos/Ubuntu crontab备份数据库
    【转】鲁迅为厦大题写校名,为啥4个字错了3个?专家:鲁迅故意写错的。
    计数单位资料汇总(个十百千兆京...,分厘毫丝忽微纤沙尘埃...)
    【转】如意算盘:中国的第五大发明
  • 原文地址:https://www.cnblogs.com/kobe8/p/3747573.html
Copyright © 2011-2022 走看看