Example usage for android.database Cursor close

List of usage examples for android.database Cursor close

Introduction

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

Prototype

void close();

Source Link

Document

Closes the Cursor, releasing all of its resources and making it completely invalid.

Usage

From source file:com.ppshein.PlanetMyanmarDictionary.common.java

public static String checkBookmark(String notedword, Context context) {
    String returnString = "nofade";
    try {//from   w w  w.java  2s  .c o m

        DatabaseUtil dbUtil = new DatabaseUtil(context);

        dbUtil.open();
        Cursor cursor = dbUtil.fetchBookmark(notedword);
        String getBookmark = cursor.getString(1);
        cursor.close();
        dbUtil.close();
        if (getBookmark.toString() != "") {
            returnString = "fade";
        }
    } catch (Exception e) {
        returnString = "nofade";
    }

    return returnString;
}

From source file:Main.java

/**
 * Method to resolve the display name of a content URI.
 *
 * @param uri the content URI to be resolved.
 * @param contentResolver the content resolver to query.
 * @param columnField the column field to query.
 * @return the display name of the @code uri if present in the database
 *  or an empty string otherwise./*w w w .j a  va  2  s.c  o  m*/
 */
public static String getDisplayName(Uri uri, ContentResolver contentResolver, String columnField) {
    if (contentResolver == null || uri == null)
        return "";
    Cursor cursor = null;
    try {
        cursor = contentResolver.query(uri, null, null, null, null);

        if (cursor != null && cursor.getCount() >= 1) {
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(columnField);
            if (index > -1)
                return cursor.getString(index);
        }
    } catch (NullPointerException e) {
        // Some android models don't handle the provider call correctly.
        // see crbug.com/345393
        return "";
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return "";
}

From source file:com.ppshein.PlanetMyanmarDictionary.common.java

public static String ProcessBookmark(String notedword, Context context) {
    String returnString = "nofade";
    try {//from   w w w .  ja  v  a2  s .  c om

        DatabaseUtil dbUtil = new DatabaseUtil(context);

        dbUtil.open();
        Cursor cursor = dbUtil.fetchBookmark(notedword);
        String getBookmark = cursor.getString(1);
        cursor.close();
        dbUtil.close();
        if (getBookmark.toString() != "") {
            dbUtil.open();
            boolean IsDeleted = dbUtil.deleteBookmark(notedword);
            Log.v("Bookmarks", "Bookmark is deleted " + IsDeleted);
            dbUtil.close();
            returnString = "nofade";
        }
    } catch (Exception e) {
        addBookmark(notedword, context);
        returnString = "fade";
    }
    return returnString;
}

From source file:Main.java

public static long[] getAllSongs(Context context) {
    Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media._ID }, MediaStore.Audio.Media.IS_MUSIC + "=1", null, null);
    try {//from w w  w  . j  a v a2s  .co m
        if (c == null || c.getCount() == 0) {
            return null;
        }
        int len = c.getCount();
        long[] list = new long[len];
        for (int i = 0; i < len; i++) {
            c.moveToNext();
            list[i] = c.getLong(0);
        }

        return list;
    } finally {
        if (c != null) {
            c.close();
        }
    }
}

From source file:Main.java

public static List<String> displayContacts(ContentResolver contentResolver) {

    List<String> contacts = new ArrayList<String>();
    ContentResolver cr = contentResolver;
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    contacts.add(name + " : " + phoneNo);
                }//w  w w.ja v  a2s.c  om
                pCur.close();
            }
        }
    }
    return contacts;
}

From source file:net.ccghe.emocha.model.DBAdapter.java

public static ArrayList<String> getFilePaths() {
    Cursor c = sDB.query(TABLE_DOWNLOADS, new String[] { "path" }, null, null, null, null, null);
    ArrayList<String> result = new ArrayList<String>();

    int numRows = c.getCount();
    for (int i = 0; i < numRows; i++) {
        c.moveToPosition(i);/*w w w .  j  ava  2  s  .  c o  m*/
        result.add(c.getString(DL_COL_PATH));
    }
    c.close();
    return result;
}

From source file:Main.java

/**
 * Get Image orientation from uri/*  www  .  ja  v  a  2  s.  co  m*/
 *
 * @param context  Context of image
 * @param photoUri Uri of image
 * @return
 */
public static int getOrientation(Context context, Uri photoUri) {
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    try {
        if (cursor.moveToFirst()) {
            if (cursor.getInt(0) == ORIENTATION_270)
                return ExifInterface.ORIENTATION_ROTATE_270;
            else if (cursor.getInt(0) == ORIENTATION_180)
                return ExifInterface.ORIENTATION_ROTATE_180;
            else if (cursor.getInt(0) == ORIENTATION_90)
                return ExifInterface.ORIENTATION_ROTATE_90;
        }
    } finally {
        cursor.close();
    }
    return -1;
}

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
    /*//from   w  ww . j  a  va 2s  .c om
     * 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:Main.java

private static String[] convertCursorAsStringArrayWithCloseCursor(Cursor cursor, int colIdx) {
    String[] result = null;/*from   ww  w .j  a v a2  s  .c o m*/
    try {
        int resultCount = cursor.getCount();
        if (resultCount > 0) {
            HashSet<String> phones = new HashSet<String>(resultCount);
            while (cursor.moveToNext()) {
                String phone = cursor.getString(0);
                phones.add(phone);
            }
            result = phones.toArray(new String[phones.size()]);
        }
        Log.d(TAG,
                "ConvertCursor As StringArray : found " + resultCount + " String converted from idx " + colIdx);
    } finally {
        cursor.close();
    }
    return result;
}

From source file:com.xfinity.ceylon_steel.controller.CustomerController.java

public static ArrayList<Customer> getCustomers(Context context) {
    ArrayList<Customer> customers = new ArrayList<Customer>();
    SQLiteDatabaseHelper databaseInstance = SQLiteDatabaseHelper.getDatabaseInstance(context);
    SQLiteDatabase database = databaseInstance.getWritableDatabase();
    Cursor customerCursor = database.rawQuery("select customerId, customerName from tbl_customer", null);
    int customerIdIndex = customerCursor.getColumnIndex("customerId");
    int customerNameIndex = customerCursor.getColumnIndex("customerName");
    for (customerCursor.moveToFirst(); !customerCursor.isAfterLast(); customerCursor.moveToNext()) {
        Customer customer = new Customer(customerCursor.getInt(customerIdIndex),
                customerCursor.getString(customerNameIndex));
        customers.add(customer);/* ww w  . j a v  a  2 s .c om*/
    }
    customerCursor.close();
    databaseInstance.close();
    return customers;
}