Example usage for android.database Cursor move

List of usage examples for android.database Cursor move

Introduction

In this page you can find the example usage for android.database Cursor move.

Prototype

boolean move(int offset);

Source Link

Document

Move the cursor by a relative amount, forward or backward, from the current position.

Usage

From source file:org.kontalk.provider.UsersProvider.java

/**
 * Computes counts by the address book index labels and returns it as {@link Bundle} which
 * will be appended to a {@link Cursor} as extras.
 *//*  www . j  a va 2 s .  co  m*/
private Bundle getFastScrollingIndexExtras(Cursor cursor) {
    try {
        LinkedHashMap<String, Counter> groups = new LinkedHashMap<>();
        int count = cursor.getCount();

        for (int i = 0; i < count; i++) {
            cursor.moveToNext();
            String source = cursor.getString(Contact.COLUMN_DISPLAY_NAME);
            // use phone number if we don't have a display name
            if (source == null)
                source = cursor.getString(Contact.COLUMN_NUMBER);
            String label = mLocaleUtils.getLabel(source);
            Counter counter = groups.get(label);
            if (counter == null) {
                counter = new Counter(1);
                groups.put(label, counter);
            } else {
                counter.inc();
            }
        }

        int numLabels = groups.size();
        String labels[] = new String[numLabels];
        int counts[] = new int[numLabels];
        int i = 0;
        for (Map.Entry<String, Counter> entry : groups.entrySet()) {
            labels[i] = entry.getKey();
            counts[i] = entry.getValue().value;
            i++;
        }

        return FastScrollingIndexCache.buildExtraBundle(labels, counts);
    } finally {
        // reset the cursor
        cursor.move(-1);
    }
}

From source file:com.example.mydemos.view.RingtonePickerActivity.java

private int getRingtonePosition(Uri ringtoneUri, int tabName, String were) {

    if (ringtoneUri == null)
        return -1;

    final Cursor cursor = getCursor(tabName, were);
    final int cursorCount = cursor.getCount();
    final String musicId = MediaStore.Audio.Media._ID;

    if (!cursor.moveToFirst()) {
        Log.e("zxw", "getRingtonePosition 1");
        return -1;
    }//from  www  . j a  v a 2s.  co m

    // Only create Uri objects when the actual URI changes
    Uri BaseUri = null;
    Uri currentUri = null;
    String previousUriString = null;
    if (tabName == SYSTEM_TONE) {
        BaseUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
    } else {
        BaseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    }

    for (int i = 0; i < cursorCount; i++) {

        int position = cursor.getInt(cursor.getColumnIndex(musicId));

        currentUri = ContentUris.withAppendedId(BaseUri, position);

        //if( position == mSelectedId) {
        if (ringtoneUri.equals(currentUri)) {
            Log.e("zxw", "getRingtonePosition i" + i);
            cursor.close();
            return i;
        }

        //            String uriString = cursor.getString(URI_COLUMN_INDEX);
        //            if (currentUri == null || !uriString.equals(previousUriString)) {
        //                currentUri = Uri.parse(uriString);
        //            }            
        //            if (ringtoneUri.equals(ContentUris.withAppendedId(currentUri, cursor
        //                    .getLong(ID_COLUMN_INDEX)))) {
        //                Log.e("zxw","getRingtonePosition i" + i);
        //                return i;
        //            }

        cursor.move(1);

        //            previousUriString = uriString;
    }
    cursor.close();
    Log.e("zxw", "getRingtonePosition 2");
    return -1;
}

From source file:com.raspi.chatapp.util.storage.MessageHistory.java

public MessageArrayContent[] getMessages(String buddyId, int amount, int offset, boolean reverse) {
    //Log.d("DATABASE", "Getting messages");
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    String[] columns = new String[] { MessageHistoryContract.MessageEntry.COLUMN_NAME_BUDDY_ID,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TYPE,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_CONTENT,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_URL,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_STATUS,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TIMESTAMP,
            MessageHistoryContract.MessageEntry._ID,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_OTHERS_ID };
    Cursor messages = db.query(buddyId, columns, null, null, null, null,
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TIMESTAMP + " DESC", offset + "," + amount);

    if (reverse)/*from w  ww  . j a v a 2 s .c o m*/
        messages.moveToFirst();
    else
        messages.moveToLast();
    int messageCount = messages.getCount();
    MessageArrayContent[] result = new MessageArrayContent[messageCount];
    int i = 0;
    if (messages.getCount() > 0)
        do {
            String from = messages.getString(0);
            SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCES, 0);
            String me = preferences.getString(Constants.USERNAME, "");
            String type = messages.getString(1);
            String content = messages.getString(2);
            String url = messages.getString(3);
            int progress = 0;
            String status = messages.getString(4);
            long time = messages.getLong(5);
            long _ID = messages.getLong(6);
            long othersId = messages.getLong(7);
            switch (type) {
            case (MessageHistory.TYPE_TEXT):
                result[i] = new TextMessage(!me.equals(from), content, time, status, _ID, othersId);
                //            if (((TextMessage) result[i]).left)
                //              updateMessageStatus(from, _ID, STATUS_READ);
                break;
            case (MessageHistory.TYPE_IMAGE):
                try {
                    JSONArray contentJSON = new JSONArray(content);
                    result[i] = new ImageMessage(!me.equals(from), //left
                            contentJSON.getString(0), //File
                            contentJSON.getString(1), //description
                            url, //url
                            progress, //progress
                            time, //timeStamp
                            status, //status
                            _ID, //_ID
                            buddyId, //buddyID
                            othersId); //othersId
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            i++;
        } while (messages.move((reverse) ? 1 : -1));
    db.close();
    messages.close();
    return result;
}

From source file:com.raspi.chatapp.util.storage.MessageHistory.java

public ChatEntry[] getChats() {
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor chats = db.query(MessageHistoryContract.ChatEntry.TABLE_NAME_ALL_CHATS,
            new String[] { MessageHistoryContract.ChatEntry.COLUMN_NAME_BUDDY_ID,
                    MessageHistoryContract.ChatEntry.COLUMN_NAME_NAME },
            null, null, null, null, MessageHistoryContract.ChatEntry.COLUMN_NAME_NAME);
    int chatCount = chats.getCount();
    ChatEntry[] resultChats = new ChatEntry[chatCount];
    int i = 0;//ww  w.  j a v a2 s .  c  o  m
    chats.moveToFirst();
    if (chats.getCount() > 0)
        do {
            String buddyId = chats.getString(0);
            String name = chats.getString(1);
            MessageArrayContent mac = getLastMessage(buddyId);

            if (mac instanceof TextMessage) {
                TextMessage msg = (TextMessage) mac;

                String lastMessageDate;
                Date msgTime = new Date(msg.time);
                Calendar startOfDay = Calendar.getInstance();
                startOfDay.set(Calendar.HOUR_OF_DAY, 0);
                startOfDay.set(Calendar.MINUTE, 0);
                startOfDay.set(Calendar.SECOND, 0);
                startOfDay.set(Calendar.MILLISECOND, 0);
                long diff = startOfDay.getTimeInMillis() - msgTime.getTime();
                if (diff <= 0)
                    lastMessageDate = String.format(context.getResources().getString(R.string.time), msgTime);
                else if (diff > 1000 * 60 * 60 * 24)
                    lastMessageDate = String.format(context.getResources().getString(R.string.date), msgTime);
                else
                    lastMessageDate = context.getResources().getString(R.string.last_message_yesterday);

                resultChats[i] = new ChatEntry(buddyId, name, MessageHistory.TYPE_TEXT, msg.status,
                        lastMessageDate, ((msg.left) ? name + ": " : "") + msg.message, !msg.left);
            } else if (mac instanceof ImageMessage) {
                ImageMessage msg = (ImageMessage) mac;

                String lastMessageDate;
                Date msgTime = new Date(msg.time);
                Calendar startOfDay = Calendar.getInstance();
                startOfDay.set(Calendar.HOUR_OF_DAY, 0);
                startOfDay.set(Calendar.MINUTE, 0);
                startOfDay.set(Calendar.SECOND, 0);
                startOfDay.set(Calendar.MILLISECOND, 0);
                long diff = startOfDay.getTimeInMillis() - msgTime.getTime();
                if (diff <= 0)
                    lastMessageDate = String.format(context.getResources().getString(R.string.time), msgTime);
                else if (diff > 1000 * 60 * 60 * 24)
                    lastMessageDate = String.format(context.getResources().getString(R.string.date), msgTime);
                else
                    lastMessageDate = context.getResources().getString(R.string.last_message_yesterday);
                msg.description += "".equals(msg.description) ? context.getResources().getString(R.string.image)
                        : "";

                resultChats[i] = new ChatEntry(buddyId, name, MessageHistory.TYPE_IMAGE, msg.status,
                        lastMessageDate, msg.description, !msg.left);
            }
            i++;
        } while (chats.move(1));
    chats.close();
    db.close();
    return resultChats;
}