List of usage examples for android.database Cursor moveToNext
boolean moveToNext();
From source file:net.naonedbus.manager.impl.FavoriManager.java
/** * Transformer un curseur en liste d'lments * //w w w . j av a 2s. c o m * @param c * un Curseur * @return une liste d'lments */ protected List<Favori> getFromCursorFull(final Cursor c) { final List<Favori> items = new ArrayList<Favori>(); if (c.getCount() > 0) { c.moveToFirst(); while (!c.isAfterLast()) { items.add(getSingleFromCursorFull(c)); c.moveToNext(); } } c.close(); return items; }
From source file:cn.edu.wyu.documentviewer.RootsCache.java
/** * Bring up requested provider and query for all active roots. *///from w ww. j ava2s. c om private Collection<RootInfo> loadRootsForAuthority(ContentResolver resolver, String authority) { if (DEBUG) Log.d(TAG, "Loading roots for " + authority); synchronized (mObservedAuthorities) { if (mObservedAuthorities.add(authority)) { // Watch for any future updates final Uri rootsUri = DocumentsContract.buildRootsUri(authority); mContext.getContentResolver().registerContentObserver(rootsUri, true, mObserver); } } final List<RootInfo> roots = Lists.newArrayList(); final Uri rootsUri = DocumentsContract.buildRootsUri(authority); ContentProviderClient client = null; Cursor cursor = null; try { client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority); cursor = client.query(rootsUri, null, null, null, null); while (cursor.moveToNext()) { final RootInfo root = RootInfo.fromRootsCursor(authority, cursor); roots.add(root); } } catch (Exception e) { Log.w(TAG, "Failed to load some roots from " + authority + ": " + e); } finally { IOUtils.closeQuietly(cursor); ContentProviderClient.releaseQuietly(client); } return roots; }
From source file:com.wojtechnology.sunami.SongManager.java
public void genresFromDB(Set<String> genres) { //Some audio may be explicitly marked as not being music String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; String[] projection = { MediaStore.Audio.Media.DATA }; String[] genresProjection = { MediaStore.Audio.Genres._ID, MediaStore.Audio.Genres.NAME }; Uri uri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI; Cursor projectionCursor = context.getContentResolver().query(uri, genresProjection, null, null, null); if (projectionCursor == null) { return;//from ww w . j av a2 s. c om } while (projectionCursor.moveToNext()) { String genre_id = projectionCursor.getString(0); String genre_name = projectionCursor.getString(1); uri = MediaStore.Audio.Genres.Members.getContentUri("external", Long.parseLong(genre_id)); Cursor cursor = context.getContentResolver().query(uri, projection, selection, null, null); while (cursor.moveToNext()) { FireMixtape song = mSongDict.get(cursor.getString(0)); song.genre = genre_name.toLowerCase(); setActualGenre(song, genres); } cursor.close(); } projectionCursor.close(); }
From source file:com.granita.contacticloudsync.resource.LocalCalendar.java
@SuppressWarnings("Recycle") public void processDirtyExceptions() throws CalendarStorageException { // process deleted exceptions Constants.log.info("Processing deleted exceptions"); try {//from w w w .ja v a 2 s . co m @Cleanup Cursor cursor = provider.query(syncAdapterURI(Events.CONTENT_URI), new String[] { Events._ID, Events.ORIGINAL_ID, LocalEvent.COLUMN_SEQUENCE }, Events.DELETED + "!=0 AND " + Events.ORIGINAL_ID + " IS NOT NULL", null, null); while (cursor != null && cursor.moveToNext()) { Constants.log.debug("Found deleted exception, removing; then re-schuling original event"); long id = cursor.getLong(0), // can't be null (by definition) originalID = cursor.getLong(1); // can't be null (by query) int sequence = cursor.isNull(2) ? 0 : cursor.getInt(2); // get original event's SEQUENCE @Cleanup Cursor cursor2 = provider.query( syncAdapterURI(ContentUris.withAppendedId(Events.CONTENT_URI, originalID)), new String[] { LocalEvent.COLUMN_SEQUENCE }, null, null, null); int originalSequence = cursor.isNull(0) ? 0 : cursor.getInt(0); BatchOperation batch = new BatchOperation(provider); // re-schedule original event and set it to DIRTY batch.enqueue(ContentProviderOperation .newUpdate(syncAdapterURI(ContentUris.withAppendedId(Events.CONTENT_URI, originalID))) .withValue(LocalEvent.COLUMN_SEQUENCE, originalSequence) .withValue(Events.DIRTY, DIRTY_INCREASE_SEQUENCE).build()); // remove exception batch.enqueue(ContentProviderOperation .newDelete(syncAdapterURI(ContentUris.withAppendedId(Events.CONTENT_URI, id))).build()); batch.commit(); } } catch (RemoteException e) { throw new CalendarStorageException("Couldn't process locally modified exception", e); } // process dirty exceptions Constants.log.info("Processing dirty exceptions"); try { @Cleanup Cursor cursor = provider.query(syncAdapterURI(Events.CONTENT_URI), new String[] { Events._ID, Events.ORIGINAL_ID, LocalEvent.COLUMN_SEQUENCE }, Events.DIRTY + "!=0 AND " + Events.ORIGINAL_ID + " IS NOT NULL", null, null); while (cursor != null && cursor.moveToNext()) { Constants.log.debug("Found dirty exception, increasing SEQUENCE to re-schedule"); long id = cursor.getLong(0), // can't be null (by definition) originalID = cursor.getLong(1); // can't be null (by query) int sequence = cursor.isNull(2) ? 0 : cursor.getInt(2); BatchOperation batch = new BatchOperation(provider); // original event to DIRTY batch.enqueue(ContentProviderOperation .newUpdate(syncAdapterURI(ContentUris.withAppendedId(Events.CONTENT_URI, originalID))) .withValue(Events.DIRTY, DIRTY_DONT_INCREASE_SEQUENCE).build()); // increase SEQUENCE and set DIRTY to 0 batch.enqueue(ContentProviderOperation .newUpdate(syncAdapterURI(ContentUris.withAppendedId(Events.CONTENT_URI, id))) .withValue(LocalEvent.COLUMN_SEQUENCE, sequence + 1).withValue(Events.DIRTY, 0).build()); batch.commit(); } } catch (RemoteException e) { throw new CalendarStorageException("Couldn't process locally modified exception", e); } }
From source file:com.github.gw2app.events.Gw2ApiEvents.java
private HashMap<Integer, String> _getMapNamesFromDB() { Log.d("Gw2", "Fetching map names from DB."); SQLiteDatabase db = this.dbhelper.getReadableDatabase(); Cursor cursor = db.query(Gw2DB.MAP_NAMES_TABLE, null, null, null, null, null, null); cursor.moveToFirst();//from w w w . j a v a2 s . co m HashMap<Integer, String> mapNames = new HashMap<Integer, String>(); while (!cursor.isLast()) { Integer id = cursor.getInt(0); String name = cursor.getString(1); mapNames.put(id, name); cursor.moveToNext(); } cursor.close(); return mapNames; }
From source file:com.android.providers.contacts.ContactsSyncAdapter.java
static private void addContactMethodsToContactEntry(ContentResolver cr, long personId, ContactEntry entry) throws ParseException { Cursor c = cr.query(ContactMethods.CONTENT_URI, null, "person=" + personId, null, null); int kindIndex = c.getColumnIndexOrThrow(ContactMethods.KIND); int dataIndex = c.getColumnIndexOrThrow(ContactMethods.DATA); int auxDataIndex = c.getColumnIndexOrThrow(ContactMethods.AUX_DATA); try {//from w ww . j a v a 2s. co m while (c.moveToNext()) { int kind = c.getInt(kindIndex); switch (kind) { case Contacts.KIND_IM: { ImAddress address = new ImAddress(); cursorToContactsElement(address, c, PROVIDER_TYPE_TO_ENTRY_IM); address.setAddress(c.getString(dataIndex)); Object object = ContactMethods.decodeImProtocol(c.getString(auxDataIndex)); if (object == null) { address.setProtocolPredefined(ImAddress.PROTOCOL_NONE); } else if (object instanceof Integer) { address.setProtocolPredefined(PROVIDER_IM_PROTOCOL_TO_ENTRY_PROTOCOL.get((Integer) object)); } else { if (!(object instanceof String)) { throw new IllegalArgumentException("expected an String, " + object); } address.setProtocolPredefined(ImAddress.PROTOCOL_CUSTOM); address.setProtocolCustom((String) object); } entry.addImAddress(address); break; } case Contacts.KIND_POSTAL: { PostalAddress address = new PostalAddress(); cursorToContactsElement(address, c, PROVIDER_TYPE_TO_ENTRY_POSTAL); address.setValue(c.getString(dataIndex)); entry.addPostalAddress(address); break; } case Contacts.KIND_EMAIL: { EmailAddress address = new EmailAddress(); cursorToContactsElement(address, c, PROVIDER_TYPE_TO_ENTRY_EMAIL); address.setAddress(c.getString(dataIndex)); entry.addEmailAddress(address); break; } } } } finally { if (c != null) c.close(); } }
From source file:com.github.gw2app.events.Gw2ApiEvents.java
private HashMap<Integer, String> _getWorldNamesFromDB() { Log.d("Gw2", "Fetching world names from DB."); SQLiteDatabase db = this.dbhelper.getReadableDatabase(); Cursor cursor = db.query(Gw2DB.WORLD_NAMES_TABLE, null, null, null, null, null, null); cursor.moveToFirst();/* w w w. j av a 2 s. c om*/ HashMap<Integer, String> worldNames = new HashMap<Integer, String>(); while (!cursor.isLast()) { Integer id = cursor.getInt(0); String name = cursor.getString(1); worldNames.put(id, name); cursor.moveToNext(); } cursor.close(); return worldNames; }
From source file:com.android.email.provider.EmailMessageCursor.java
public EmailMessageCursor(final Context c, final Cursor cursor, final String htmlColumn, final String textColumn) { super(cursor); mHtmlColumnIndex = cursor.getColumnIndex(htmlColumn); mTextColumnIndex = cursor.getColumnIndex(textColumn); final int cursorSize = cursor.getCount(); mHtmlParts = new SparseArray<String>(cursorSize); mTextParts = new SparseArray<String>(cursorSize); final ContentResolver cr = c.getContentResolver(); while (cursor.moveToNext()) { final int position = cursor.getPosition(); final long messageId = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); try {/*w w w . j a va2 s .c om*/ if (mHtmlColumnIndex != -1) { final Uri htmlUri = Body.getBodyHtmlUriForMessageWithId(messageId); final InputStream in = cr.openInputStream(htmlUri); final String underlyingHtmlString; try { underlyingHtmlString = IOUtils.toString(in); } finally { in.close(); } final String sanitizedHtml = HtmlSanitizer.sanitizeHtml(underlyingHtmlString); mHtmlParts.put(position, sanitizedHtml); } } catch (final IOException e) { LogUtils.v(LogUtils.TAG, e, "Did not find html body for message %d", messageId); } try { if (mTextColumnIndex != -1) { final Uri textUri = Body.getBodyTextUriForMessageWithId(messageId); final InputStream in = cr.openInputStream(textUri); final String underlyingTextString; try { underlyingTextString = IOUtils.toString(in); } finally { in.close(); } mTextParts.put(position, underlyingTextString); } } catch (final IOException e) { LogUtils.v(LogUtils.TAG, e, "Did not find text body for message %d", messageId); } } cursor.moveToPosition(-1); }
From source file:curso.android.DAO.RespostaDAO.java
public static List<Resposta> getRespostaPergunta(Pergunta p) { Cursor cursor = null; try {/*from w ww .j a v a 2 s .com*/ List<Resposta> questions = new ArrayList<Resposta>(); cursor = Const.db.rawQuery("SELECT * FROM resposta WHERE ask_id = " + p.getAsk_id(), null); if (cursor.getCount() > 0) { int idIndex = cursor.getColumnIndex("asw_id"); int askIndex = cursor.getColumnIndex("ask_id"); int userIndex = cursor.getColumnIndex("user_id"); int respostaIndex = cursor.getColumnIndex("asw_resposta"); int nameIndex = cursor.getColumnIndex("user_name"); cursor.moveToFirst(); do { Long id = Long.valueOf(cursor.getInt(idIndex)); Long ask = Long.valueOf(cursor.getString(askIndex)); Long user = Long.valueOf(cursor.getInt(userIndex)); String resposta = cursor.getString(respostaIndex); String user_name = cursor.getString(nameIndex); Resposta answer = new Resposta(); answer.setAsw_id(id); answer.setAsk_id(ask); answer.setUser_id(user); answer.setAsw_resposta(resposta); answer.setUser_name(user_name); questions.add(answer); cursor.moveToNext(); } while (!cursor.isAfterLast()); } return questions; } finally { if (cursor != null) { cursor.close(); } } }
From source file:org.noorganization.instalistsynch.controller.synch.impl.UnitSynch.java
@Override public void indexLocal(int _groupId, Date _lastIndexTime) { String lastIndexTime = ISO8601Utils.format(_lastIndexTime, false, TimeZone.getTimeZone("GMT+0000")); Cursor changeLog = mLocalLogController.getLogsSince(lastIndexTime, eModelType.UNIT); while (changeLog.moveToNext()) { int actionId = changeLog.getInt(changeLog.getColumnIndex(LogInfo.COLUMN.ACTION)); eActionType actionType = eActionType.getTypeById(actionId); String uuid = changeLog.getString(changeLog.getColumnIndex(LogInfo.COLUMN.ITEM_UUID)); if (uuid.contentEquals("-")) continue; List<ModelMapping> existingMappings = mMappingController.get( ModelMapping.COLUMN.CLIENT_SIDE_UUID + " = ? AND " + ModelMapping.COLUMN.GROUP_ID + " = ?", new String[] { uuid, String.valueOf(_groupId) }); ModelMapping existingMapping = (existingMappings.size() == 0 ? null : existingMappings.get(0)); if (actionType == null) { Log.w(TAG, "Changelog contains entry without action."); continue; }//from w ww. java2s .c o m switch (actionType) { case INSERT: { if (existingMapping == null) { ModelMapping newMapping = new ModelMapping(null, _groupId, null, uuid, new Date(Constants.INITIAL_DATE), new Date(), false); mMappingController.insert(newMapping); } break; } case UPDATE: { if (existingMapping == null) { Log.e(TAG, "Changelog contains update, but mapping does not exist. " + "Ignoring."); continue; } try { Date clientDate = ISO8601Utils.parse( changeLog.getString(changeLog.getColumnIndex(LogInfo.COLUMN.ACTION_DATE)), new ParsePosition(0)); existingMapping.setLastClientChange(clientDate); mMappingController.update(existingMapping); } catch (ParseException e) { Log.e(TAG, "Change log contains invalid date: " + e.getMessage()); continue; } break; } case DELETE: { if (existingMapping == null) { Log.e(TAG, "Changelog contains deletion, but mapping does not exist. " + "Ignoring."); continue; } try { Date clientDate = ISO8601Utils.parse( changeLog.getString(changeLog.getColumnIndex(LogInfo.COLUMN.ACTION_DATE)), new ParsePosition(0)); existingMapping.setLastClientChange(clientDate); existingMapping.setDeleted(true); mMappingController.update(existingMapping); } catch (ParseException e) { Log.e(TAG, "Change log contains invalid date: " + e.getMessage()); continue; } break; } } } changeLog.close(); }