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.smarthome.deskclock.Alarms.java

/**
 * Return an Alarm object representing the alarm id in the database.
 * Returns null if no alarm exists./*  www. j  a va 2 s  .c o  m*/
 */
public static Alarm getAlarm(ContentResolver contentResolver, int alarmId) {
    Cursor cursor = contentResolver.query(ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, alarmId),
            Alarm.Columns.ALARM_QUERY_COLUMNS, null, null, null);
    Alarm alarm = null;
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            alarm = new Alarm(cursor);
        }
        cursor.close();
    }
    return alarm;
}

From source file:edu.stanford.mobisocial.dungbeetle.Helpers.java

public static Contact forceGetContact(Context context, long contactId) {
    if (contactId == Contact.MY_ID) {
        DBHelper dbh = new DBHelper(context);
        DBIdentityProvider idp = new DBIdentityProvider(dbh);
        try {/*from  w  w w .ja  v a  2  s. com*/
            return idp.contactForUser();
        } finally {
            idp.close();
            dbh.close();
        }
    }
    Cursor c = context.getContentResolver().query(
            Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/contacts"), null, Contact._ID + "=?",
            new String[] { String.valueOf(contactId) }, null);
    if (c == null)
        return null;
    try {

        if (!c.moveToFirst()) {
            return null;
        } else {
            return new Contact(c);
        }
    } finally {
        c.close();
    }
}

From source file:im.delight.android.baselib.Social.java

/**
 * Returns a list of email addresses for the contact with the given lookup ID
 *
 * @param lookupID the lookup ID to get the email addresses for
 * @param context Context instance to get the ContentResolver from
 * @return CharSequence[] containing all email addresses for the given contact
 *//*from ww  w  .  j  a v  a 2 s. c om*/
public static CharSequence[] getContactEmail(String lookupID, Context context) {
    Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.DATA };
    String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    String[] selectionArgs = new String[] { lookupID };
    String sortOrder = null;
    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);
    String email;
    if (result != null) {
        if (result.getCount() > 0) {
            CharSequence[] res = new CharSequence[result.getCount()];
            int i = 0;
            while (result.moveToNext()) {
                email = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                if (email != null) {
                    res[i] = email;
                    i++;
                }
            }
            result.close();
            return res;
        } else {
            result.close();
            return null;
        }
    } else {
        return null;
    }
}

From source file:im.delight.android.baselib.Social.java

/**
 * Returns a list of phone numbers for the contact with the given lookup ID
 *
 * @param lookupID the lookup ID to get the phone numbers for
 * @param context Context instance to get the ContentResolver from
 * @return CharSequence[] containing all phone numbers for the given contact
 *//*from www.  j av a  2  s  .c o  m*/
public static CharSequence[] getContactPhone(String lookupID, Context context) {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
    String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    String[] selectionArgs = new String[] { lookupID };
    String sortOrder = null;
    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);
    String phone;
    if (result != null) {
        if (result.getCount() > 0) {
            CharSequence[] res = new CharSequence[result.getCount()];
            int i = 0;
            while (result.moveToNext()) {
                phone = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                if (phone != null) {
                    res[i] = phone;
                    i++;
                }
            }
            result.close();
            return res;
        } else {
            result.close();
            return null;
        }
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context./*from  w ww. j av  a2 s  .  c  o m*/
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 * @author paulburke
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            if (DEBUG)
                DatabaseUtils.dumpCursor(cursor);

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (IllegalArgumentException ex) {
        Log.i(TAG, "getDataColumn: _data", ex);
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

public static Map<Integer, List> getAreaByPid(int id, File file) {

    String sql = "select area,areaid  from area where cityid= " + id;
    SQLiteDatabase db = null;/*from w  w  w  .  j av  a  2 s.  co  m*/
    Cursor c = null;
    List<String> areaList = null;
    List areaIdList = null;
    Map<Integer, List> areaData = new HashMap<Integer, List>();
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        areaList = new ArrayList<String>();
        areaIdList = new ArrayList<String>();

        while (c.moveToNext()) {
            Map areaMap = new HashMap();
            areaMap.put(c.getString(0), c.getInt(1));
            areaList.add(c.getString(0));
            areaIdList.add(areaMap);
        }
        areaData.put(0, areaList);
        areaData.put(1, areaIdList);
    } catch (Exception e) {
        Log.d("WineStock", "getAreaByPid:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return areaData;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static String getNameFromEmail(Context context, String email) {

    if (context == null) {
        return null;
    }//from   w w w  .ja v a  2 s  . c om

    if (loadContacts(context)) {
        try {
            String name = MainApplication.contacts.getJSONObject(email).getString("name");
            Log.e(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
            return name;
        } catch (JSONException e) {
            Log.e(TAG, "getNameFromEmail - failed: " + email);
        }
    }

    Log.e(TAG, "getNameFromEmail - Name queried: " + email);
    Cursor emailCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            null, ContactsContract.CommonDataKinds.Email.DATA + "='" + email + "'", null, null);
    if (emailCursor == null) {
        return "???";
    }
    if (emailCursor.moveToNext()) {
        String name = emailCursor.getString(
                emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY));
        Log.e(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
        emailCursor.close();
        return name;
    }
    emailCursor.close();
    return email;
}

From source file:Main.java

static String convertUriToPath(Context context, Uri uri) {
    Log.v(TAG, "convertUriToPath : " + uri + " @" + context);

    String path = null;/*w w w .j a v  a  2 s  .c  om*/
    if (null != uri) {
        String scheme = uri.getScheme();
        if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) {
            path = uri.getPath();
        } else if (scheme.equals("http")) {
            path = uri.toString();
        } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
            String[] projection = new String[] { MediaStore.MediaColumns.DATA };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
                    throw new IllegalArgumentException("Given Uri could not be found in media store");
                }
                int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                path = cursor.getString(pathIndex);
            } catch (SQLiteException e) {
                throw new IllegalArgumentException(
                        "Given Uri is not formatted in a way so that it can be found in media store.");
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        } else {
            throw new IllegalArgumentException("Given Uri scheme is not supported");
        }
    }

    Log.v(TAG, "convertUriToPath : >" + path);
    return path;
}

From source file:Main.java

public static Map<Integer, List> getProvince(File file) {

    String sql = "select provinceid ,province from province ";
    SQLiteDatabase db = null;//from   ww  w .  j a  va2 s.c  o  m
    Cursor c = null;
    Map<Integer, List> provinceData = new HashMap<Integer, List>();
    //List provinceList = null;
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        List provinceList1 = new ArrayList();
        List provinceList2 = new ArrayList();
        while (c.moveToNext()) {
            Map provinceMap = new HashMap();
            provinceMap.put(c.getString(1), c.getInt(0));
            provinceList1.add(provinceMap);
            provinceList2.add(c.getString(1));
        }
        provinceData.put(0, provinceList1);
        provinceData.put(1, provinceList2);
    } catch (Exception e) {
        Log.d("WineStock", "getProvince:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return provinceData;
}

From source file:org.zoumbox.mh_dla_notifier.sp.PublicScriptsProxy.java

public static Map<PublicScript, Date> geLastUpdates(Context context, String trollId,
        Set<PublicScript> scripts) {

    MhDlaSQLHelper helper = new MhDlaSQLHelper(context);
    SQLiteDatabase database = helper.getReadableDatabase();

    Map<PublicScript, Date> result = new HashMap<PublicScript, Date>();

    for (PublicScript script : scripts) {

        Cursor cursor = database.rawQuery(SQL_LAST_UPDATE,
                new String[] { trollId, script.name(), MhDlaSQLHelper.STATUS_SUCCESS });
        Date lastUpdate = null;/*  w  w  w .  ja v  a 2s. co  m*/
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            long resultTimestamp = cursor.getLong(0);
            lastUpdate = new Date(resultTimestamp);
        }

        result.put(script, lastUpdate);
        cursor.close();
    }
    database.close();

    String format = "Last updates for troll=%s are: '%s'";
    String message = String.format(format, trollId, result);
    Log.i(TAG, message);

    return result;
}