Example usage for android.database Cursor isClosed

List of usage examples for android.database Cursor isClosed

Introduction

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

Prototype

boolean isClosed();

Source Link

Document

return true if the cursor is closed

Usage

From source file:Main.java

public static ArrayList<ContentValues> getArea(SQLiteDatabase db, int dqx_dqxx01) {
    ArrayList<ContentValues> list = new ArrayList<ContentValues>();
    ContentValues values = null;//  w w  w.  j a  v a2 s .c om
    Cursor cursor = db.query(TABLE_NAME, new String[] { "DQXX01", "DQXX02", "DQXX05" }, "DQX_DQXX01=?",
            new String[] { "" + dqx_dqxx01 }, null, null, "DQXX01 ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            values = new ContentValues();
            values.put("area_id", cursor.getInt(0));
            values.put("area", cursor.getString(1));
            values.put("full_area", cursor.getString(2));
            list.add(values);
        }
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}

From source file:Main.java

public static ArrayList<ContentValues> getCity(SQLiteDatabase db, int dqx_dqxx01, boolean municipalities) {
    ArrayList<ContentValues> list = new ArrayList<ContentValues>();
    ContentValues values = null;/* w w w.jav  a 2s. com*/
    Cursor cursor = db.query(TABLE_NAME, new String[] { "DQXX01", "DQXX02" }, "DQX_DQXX01=?",
            new String[] { "" + dqx_dqxx01 }, null, null, "DQXX01 ASC");
    if (cursor != null) {
        if (municipalities) {
            cursor.moveToNext();
        }
        while (cursor.moveToNext()) {
            values = new ContentValues();
            values.put("city_id", cursor.getInt(0));
            values.put("city", cursor.getString(1));
            list.add(values);
        }
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}

From source file:com.tonyodev.fetch.Utils.java

static RequestInfo cursorToRequestInfo(Cursor cursor, boolean closeCursor, boolean loggingEnabled) {

    RequestInfo requestInfo = null;

    try {//from   w w w  .ja  v a 2 s  .  co  m

        if (cursor == null || cursor.isClosed() || cursor.getCount() < 1) {
            return requestInfo;
        }

        cursor.moveToFirst();
        requestInfo = createRequestInfo(cursor, loggingEnabled);

        if (closeCursor) {
            cursor.close();
        }

    } catch (Exception e) {

        if (loggingEnabled) {
            e.printStackTrace();
        }
    }

    return requestInfo;
}

From source file:at.flack.receiver.SmsReceiver.java

public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
    if (cursor == null) {
        return null;
    }/*from w  w  w  .j  a v a2  s .  c o  m*/
    String contactName = null;
    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }

    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}

From source file:com.tonyodev.fetch.Utils.java

static List<RequestInfo> cursorToRequestInfoList(Cursor cursor, boolean closeCursor, boolean loggingEnabled) {

    List<RequestInfo> requests = new ArrayList<>();

    try {/*w  w w  .j  av a  2 s.c o  m*/

        if (cursor == null || cursor.isClosed() || cursor.getCount() < 1) {
            return requests;
        }

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            requests.add(createRequestInfo(cursor, loggingEnabled));
            cursor.moveToNext();
        }

        if (closeCursor) {
            cursor.close();
        }

    } catch (Exception e) {

        if (loggingEnabled) {
            e.printStackTrace();
        }
    }

    return requests;
}

From source file:com.tonyodev.fetch.Utils.java

static ArrayList<Bundle> cursorToQueryResultList(Cursor cursor, boolean closeCursor, boolean loggingEnabled) {

    ArrayList<Bundle> requests = new ArrayList<>();

    try {/*w ww .  ja va  2 s .co  m*/

        if (cursor == null || cursor.isClosed()) {
            return requests;
        }

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            long id = cursor.getLong(DatabaseHelper.INDEX_COLUMN_ID);
            int status = cursor.getInt(DatabaseHelper.INDEX_COLUMN_STATUS);
            String url = cursor.getString(DatabaseHelper.INDEX_COLUMN_URL);
            String filePath = cursor.getString(DatabaseHelper.INDEX_COLUMN_FILEPATH);
            int error = cursor.getInt(DatabaseHelper.INDEX_COLUMN_ERROR);
            long fileSize = cursor.getLong(DatabaseHelper.INDEX_COLUMN_FILE_SIZE);
            int priority = cursor.getInt(DatabaseHelper.INDEX_COLUMN_PRIORITY);
            long downloadedBytes = cursor.getLong(DatabaseHelper.INDEX_COLUMN_DOWNLOADED_BYTES);

            String headers = cursor.getString(DatabaseHelper.INDEX_COLUMN_HEADERS);
            ArrayList<Bundle> headersList = headersToBundleList(headers, loggingEnabled);

            int progress = getProgress(downloadedBytes, fileSize);

            Bundle bundle = new Bundle();
            bundle.putLong(FetchService.EXTRA_ID, id);
            bundle.putInt(FetchService.EXTRA_STATUS, status);
            bundle.putString(FetchService.EXTRA_URL, url);
            bundle.putString(FetchService.EXTRA_FILE_PATH, filePath);
            bundle.putInt(FetchService.EXTRA_ERROR, error);
            bundle.putLong(FetchService.EXTRA_DOWNLOADED_BYTES, downloadedBytes);
            bundle.putLong(FetchService.EXTRA_FILE_SIZE, fileSize);
            bundle.putInt(FetchService.EXTRA_PROGRESS, progress);
            bundle.putInt(FetchService.EXTRA_PRIORITY, priority);
            bundle.putParcelableArrayList(FetchService.EXTRA_HEADERS, headersList);

            requests.add(bundle);

            cursor.moveToNext();
        }

        if (closeCursor) {
            cursor.close();
        }

    } catch (Exception e) {

        if (loggingEnabled) {
            e.printStackTrace();
        }
    }

    return requests;
}

From source file:me.myatminsoe.myansms.SmsReceiver.java

/**
 * Get unread MMS./*from  w  ww .  ja va 2  s.  co m*/
 *
 * @param cr   {@link ContentResolver} to query
 * @param text text of the last assumed unread message
 * @return [thread id (-1 if there are more), number of unread messages]
 */
private static int[] getUnreadMMS(final ContentResolver cr, final String text) {
    Cursor cursor = cr.query(URI_MMS, Message.PROJECTION_READ, Message.SELECTION_READ_UNREAD,
            Message.SELECTION_UNREAD, null);
    if (cursor == null || cursor.isClosed() || cursor.getCount() == 0 || !cursor.moveToFirst()) {
        if (MMS_BODY.equals(text)) {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            return new int[] { -1, -1 }; // try again!
        } else {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            return new int[] { 0, 0 };
        }
    }
    int tid = cursor.getInt(Message.INDEX_THREADID);
    long d = cursor.getLong(Message.INDEX_DATE);
    if (d < ConversationListActivity.MIN_DATE) {
        d *= ConversationListActivity.MILLIS;
    }
    if (d > lastUnreadDate) {
        lastUnreadDate = d;
        lastUnreadBody = null;
    }
    while (cursor.moveToNext() && tid > -1) {
        // check if following messages are from the same thread
        if (tid != cursor.getInt(Message.INDEX_THREADID)) {
            tid = -1;
        }
    }
    final int count = cursor.getCount();
    if (!cursor.isClosed()) {
        cursor.close();
    }
    return new int[] { tid, count };
}

From source file:org.opendatakit.common.android.database.DataModelDatabaseHelper.java

/**
 * Accessor to retrieve the database table name given the tableId
 *
 * @param db//from   w  w w .j  a  va2 s .  co m
 * @param tableId
 * @return
 */
public static String getDbTableName(SQLiteDatabase db, String tableId) {
    Cursor c = null;
    try {
        c = db.query(TABLE_DEFS_TABLE_NAME, new String[] { TableDefinitionsColumns.DB_TABLE_NAME },
                TableDefinitionsColumns.TABLE_ID + "=?", new String[] { tableId }, null, null, null);

        if (c.moveToFirst()) {
            int idx = c.getColumnIndex(TableDefinitionsColumns.DB_TABLE_NAME);
            return c.getString(idx);
        }
    } finally {
        if (c != null && !c.isClosed()) {
            c.close();
        }
    }
    return null;
}

From source file:de.ub0r.android.smsdroid.SmsReceiver.java

/**
 * Get unread MMS.//from  w w w.j  a  v a 2  s  .  co m
 *
 * @param cr   {@link ContentResolver} to query
 * @param text text of the last assumed unread message
 * @return [thread id (-1 if there are more), number of unread messages]
 */
private static int[] getUnreadMMS(final ContentResolver cr, final String text) {
    Log.d(TAG, "getUnreadMMS(cr, ", text, ")");
    Cursor cursor = cr.query(URI_MMS, Message.PROJECTION_READ, Message.SELECTION_READ_UNREAD,
            Message.SELECTION_UNREAD, null);
    if (cursor == null || cursor.isClosed() || cursor.getCount() == 0 || !cursor.moveToFirst()) {
        if (MMS_BODY.equals(text)) {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            return new int[] { -1, -1 }; // try again!
        } else {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            return new int[] { 0, 0 };
        }
    }
    int tid = cursor.getInt(Message.INDEX_THREADID);
    long d = cursor.getLong(Message.INDEX_DATE);
    if (d < ConversationListActivity.MIN_DATE) {
        d *= ConversationListActivity.MILLIS;
    }
    if (d > lastUnreadDate) {
        lastUnreadDate = d;
        lastUnreadBody = null;
    }
    while (cursor.moveToNext() && tid > -1) {
        // check if following messages are from the same thread
        if (tid != cursor.getInt(Message.INDEX_THREADID)) {
            tid = -1;
        }
    }
    final int count = cursor.getCount();
    if (!cursor.isClosed()) {
        cursor.close();
    }
    return new int[] { tid, count };
}

From source file:me.myatminsoe.myansms.SmsReceiver.java

/**
 * Get unread SMS.//from w  w  w  .  jav a2 s . c o  m
 *
 * @param cr   {@link ContentResolver} to query
 * @param text text of the last assumed unread message
 * @return [thread id (-1 if there are more), number of unread messages (-1 if text does not
 * match newest message)]
 */
private static int[] getUnreadSMS(final ContentResolver cr, final String text) {
    Cursor cursor = cr.query(URI_SMS, Message.PROJECTION, Message.SELECTION_READ_UNREAD,
            Message.SELECTION_UNREAD, SORT);

    //Cursor cursor = cr.query(URI_SMS, null, null, null, null);

    if (cursor == null || cursor.isClosed() || cursor.getCount() == 0 || !cursor.moveToFirst()) {
        if (text != null) { // try again!
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            return new int[] { -1, -1 };
        } else {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            return new int[] { 0, 0 };
        }
    }
    final String t = cursor.getString(Message.INDEX_BODY);
    if (text != null && (t == null || !t.startsWith(text))) {
        if (!cursor.isClosed()) {
            cursor.close();
        }
        return new int[] { -1, -1 }; // try again!
    }
    final long d = cursor.getLong(Message.INDEX_DATE);
    if (d > lastUnreadDate) {
        lastUnreadDate = d;
        lastUnreadBody = t;
    }
    int tid = cursor.getInt(Message.INDEX_THREADID);
    while (cursor.moveToNext() && tid > -1) {
        // check if following messages are from the same thread
        if (tid != cursor.getInt(Message.INDEX_THREADID)) {
            tid = -1;
        }
    }
    final int count = cursor.getCount();
    if (!cursor.isClosed()) {
        cursor.close();
    }
    return new int[] { tid, count };
}