Example usage for android.database Cursor moveToNext

List of usage examples for android.database Cursor moveToNext

Introduction

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

Prototype

boolean moveToNext();

Source Link

Document

Move the cursor to the next row.

Usage

From source file:Main.java

public static String getFirstCNLetterByContactId(Context context, long contactId) {
    String result = "";
    String[] projection = {/*from  w  ww.ja  va2s.co  m*/
            // Data._ID,
            // Data.DISPLAY_NAME,
            // Data.CONTACT_ID,
            Data.DATA12, };
    String where = Data.CONTACT_ID + "=?";
    final String sortOrder = null;
    Cursor cursor = context.getContentResolver().query(PHONE_CONTACTS_URI, projection, where,
            new String[] { String.valueOf(contactId) }, sortOrder);

    if (cursor != null) {
        try {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String nameLetters = cursor.getString(cursor.getColumnIndexOrThrow("data12"));
                    if (null != nameLetters && !"".equals(nameLetters)) {
                        result = nameLetters.substring(0, 1).toUpperCase();
                        break;
                    }
                }
            }

        } finally {
            cursor.close();
        }
    }

    return result;
}

From source file:Main.java

/**
 * Reads the data from the {@code column} of the content's {@code queryUri} and returns it as an
 * Array./* w ww  .  j a  va2  s.  c o m*/
 */
static private Set<String> getColumnContentAsArray(Context context, Uri queryUri, String column) {
    Cursor cursor = context.getContentResolver().query(queryUri, new String[] { column }, null, null, null);
    Set<String> columnValues = new HashSet<>();
    try {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                columnValues.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return columnValues;
}

From source file:Main.java

public static LinkedList<Pair<Integer, String>> retrieveIntegerStringPairFromCursor(Cursor cursor,
        String integerColumnName, String stringColumnName) {
    LinkedList<Pair<Integer, String>> result = new LinkedList<Pair<Integer, String>>();

    if (null == cursor || 0 == cursor.getCount()) {
        return result;
    }/*from w  w  w .j  ava2s.  c om*/

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            Integer integerVal = cursor.getInt(cursor.getColumnIndex(integerColumnName));
            String stringVal = cursor.getString(cursor.getColumnIndexOrThrow(stringColumnName));
            result.add(new Pair(integerVal, stringVal));
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}

From source file:Main.java

public static int getMessageTypeByThreadAndBody(Context context, long threadId, String body) {
    int type = 1;
    final String[] projection = new String[] { " sms.type " + " from sms " + " where sms.thread_id = "
            + threadId + " and sms.body = '" + body + "' --" };
    // Create cursor
    /*//ww  w.  ja v a2 s  .  co m
     * Cursor cursor = context.getContentResolver().query( SMS_CONTENT_URI,
     * projection, null, null, null);
     */
    Cursor cursor = context.getContentResolver().query(SMS_CONTENT_URI, new String[] { "type" },
            "sms.thread_id = ? and sms.body = ?", new String[] { String.valueOf(threadId), body }, null);

    if (cursor != null) {
        try {
            if (cursor.getCount() > 0) {

                while (cursor.moveToNext()) {
                    type = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
                    return type;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
    }

    return type;
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that converts a file reference to a content uri reference
 *
 * @param cr A content resolver//  w  w w  .  j  a  v  a  2  s  .com
 * @param path The path to search
 * @param volume The volume
 * @return Uri The content uri or null if file not exists in the media database
 */
private static Uri fileToContentUri(ContentResolver cr, String path, String volume) {
    final String[] projection = { BaseColumns._ID, MediaStore.Files.FileColumns.MEDIA_TYPE };
    final String where = MediaColumns.DATA + " = ?";
    Uri baseUri = MediaStore.Files.getContentUri(volume);
    Cursor c = cr.query(baseUri, projection, where, new String[] { path }, null);
    try {
        if (c != null && c.moveToNext()) {
            int type = c.getInt(c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MEDIA_TYPE));
            if (type != 0) {
                // Do not force to use content uri for no media files
                long id = c.getLong(c.getColumnIndexOrThrow(BaseColumns._ID));
                return Uri.withAppendedPath(baseUri, String.valueOf(id));
            }
        }
    } finally {
        IOUtils.closeQuietly(c);
    }
    return null;
}

From source file:io.trigger.forge.android.modules.contprov.API.java

private static JSONArray extractNotesFromCursor(Cursor c) throws JSONException {
    JSONArray toRet = new JSONArray();

    int[] cIndices = new int[] { c.getColumnIndex(MyContentDescriptor.Categories.Cols.key_2_catname),
            c.getColumnIndex(MyContentDescriptor.Categories.Cols.key_3_catstatus) };

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {//Extract note from cursor 
        JSONObject o = new JSONObject();
        o.put("name", c.getString(cIndices[0]));
        o.put("status", c.getString(cIndices[1]));
        //c.getString(cIndices[SERVER_ID]),
        toRet.put(o);// w w  w.j av a2 s .co m
    }

    return toRet;
}

From source file:Main.java

public static String checkNull(Context context, int lastImageId, File fileCapture) {
    final String[] imageColumns = { Images.Media._ID, Images.Media.DATA };
    final String imageOrderBy = Images.Media._ID + " DESC";
    final String imageWhere = Images.Media._ID + ">?";
    final String[] imageArguments = { Integer.toString(lastImageId) };

    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, imageWhere,
            imageArguments, imageOrderBy);
    if (cursor == null)
        return null;

    String newpath = null;//from   ww w .j  ava 2  s.c om
    if (cursor.getCount() >= 2) {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID));
            String data = cursor.getString(cursor.getColumnIndex(Images.Media.DATA));
            if (data.equals(fileCapture.getPath())) {
                int rows = contentResolver.delete(Images.Media.EXTERNAL_CONTENT_URI, Images.Media._ID + "=?",
                        new String[] { Long.toString(id) });
                boolean ok = fileCapture.delete();

            } else {
                newpath = data;
            }
        }
    } else {
        newpath = fileCapture.getPath();
        Log.e("MediaUtils", "Not found duplicate.");
    }

    cursor.close();
    return newpath;
}

From source file:de.escoand.readdaily.DownloadHandler.java

public static float downloadProgress(final Context context, final String name) {
    Cursor cursor = Database.getInstance(context).getDownloads();
    long id = 0;//www  .  j a v  a 2  s . c  om
    float progress;

    // get download id
    while (cursor.moveToNext())
        if (cursor.getString(cursor.getColumnIndex(Database.COLUMN_SUBSCRIPTION)).equals(name)) {
            id = cursor.getLong(cursor.getColumnIndex(Database.COLUMN_ID));
            break;
        }
    cursor.close();
    if (id <= 0)
        return SUBSCRIPTION_DOWNLOAD_UNKNOWN;

    // get download
    cursor = ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE))
            .query(new DownloadManager.Query().setFilterById(id));
    if (!cursor.moveToFirst()) {
        Database.getInstance(context).removeDownload(id);
        return DOWNLOAD_ID_UNKNOWN;
    }

    // get progress
    progress = cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
            / cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

    cursor.close();
    return progress;
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that converts a content uri to a file system path
 *
 * @param cr The content resolver/*from w w  w  . ja v a2s . c  o  m*/
 * @param id The media database id
 * @param volume The volume
 * @return File The file reference
 */
private static File mediaIdToFile(ContentResolver cr, long id, String volume) {
    final String[] projection = { MediaColumns.DATA };
    final String where = MediaColumns._ID + " = ?";
    Uri baseUri = MediaStore.Files.getContentUri(volume);
    Cursor c = cr.query(baseUri, projection, where, new String[] { String.valueOf(id) }, null);
    try {
        if (c != null && c.moveToNext()) {
            return new File(c.getString(c.getColumnIndexOrThrow(MediaColumns.DATA)));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return null;
}

From source file:at.bitfire.ical4android.AndroidTaskList.java

public static AndroidTaskList[] find(Account account, TaskProvider provider, AndroidTaskListFactory factory,
        String where, String whereArgs[]) throws CalendarStorageException {
    List<AndroidTaskList> taskLists = new LinkedList<>();
    try {/*from w  ww  . j  a  va  2 s . c o  m*/
        @Cleanup
        Cursor cursor = provider.client.query(syncAdapterURI(provider.taskListsUri(), account), null, null,
                null, null);
        while (cursor != null && cursor.moveToNext()) {
            ContentValues values = new ContentValues(cursor.getColumnCount());
            DatabaseUtils.cursorRowToContentValues(cursor, values);
            AndroidTaskList taskList = factory.newInstance(account, provider, values.getAsLong(TaskLists._ID));
            taskList.populate(values);
            taskLists.add(taskList);
        }
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query task list by ID", e);
    }
    return taskLists.toArray(factory.newArray(taskLists.size()));
}