List of usage examples for android.database Cursor moveToNext
boolean moveToNext();
From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java
public int getTodoStatus(ContentResolver contentResolver, Long id) { Cursor c = contentResolver.query(TodoContentProvider.CONTENT_URI, null, TodoContentProvider.COLUMN_ID + "=" + id, null, null); int status = 0; try {/*from w w w . j a v a 2 s.c o m*/ if (c.moveToNext()) { status = c.getInt(c.getColumnIndexOrThrow(TodoContentProvider.COLUMN_STATUS_FLAG)); } else { throw new RuntimeException("Tried to delete a non existent todo"); } } finally { c.close(); } return status; }
From source file:com.pursuer.reader.easyrss.network.SubscriptionDataSyncer.java
private void syncSubscriptionIcons() throws DataSyncerException { final Context context = dataMgr.getContext(); if (!NetworkUtils.checkSyncingNetworkStatus(context, networkConfig)) { return;/* w w w. jav a 2 s.c o m*/ } final ContentResolver resolver = context.getContentResolver(); final Cursor cur = resolver.query(Subscription.CONTENT_URI, new String[] { Subscription._UID, Subscription._ICON, Subscription._URL }, null, null, null); for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) { final String uid = cur.getString(0); final byte[] data = cur.getBlob(1); final String subUrl = cur.getString(2); if (subUrl != null && data == null) { final SubscriptionIconUrl fetchUrl = new SubscriptionIconUrl(isHttpsConnection, subUrl); try { final byte[] iconData = httpGetQueryByte(fetchUrl); final Bitmap icon = BitmapFactory.decodeByteArray(iconData, 0, iconData.length); final int size = icon.getWidth() * icon.getHeight() * 2; final ByteArrayOutputStream output = new ByteArrayOutputStream(size); icon.compress(Bitmap.CompressFormat.PNG, 100, output); output.flush(); output.close(); dataMgr.updateSubscriptionIconByUid(uid, output.toByteArray()); } catch (final IOException exception) { cur.close(); throw new DataSyncerException(exception); } } } cur.close(); }
From source file:fr.unix_experience.owncloud_sms.engine.SmsFetcher.java
public JSONArray getLastMessage(MailboxID mbID) { String mbURI = mapMailboxIDToURI(mbID); if (_context == null || mbURI == null) { return null; }//from w ww . java 2s . c o m // Fetch Sent SMS Message from Built-in Content Provider Cursor c = (new SmsDataProvider(_context)).query(mbURI); c.moveToNext(); // We create a list of strings to store results JSONArray results = new JSONArray(); JSONObject entry = new JSONObject(); try { for (int idx = 0; idx < c.getColumnCount(); idx++) { String colName = c.getColumnName(idx); // Id column is must be an integer if (colName.equals(new String("_id")) || colName.equals(new String("type"))) { entry.put(colName, c.getInt(idx)); // bufferize Id for future use if (colName.equals(new String("_id"))) { } } // Seen and read must be pseudo boolean else if (colName.equals(new String("read")) || colName.equals(new String("seen"))) { entry.put(colName, c.getInt(idx) > 0 ? "true" : "false"); } else { entry.put(colName, c.getString(idx)); } } // Mailbox ID is required by server entry.put("mbox", mbID.ordinal()); results.put(entry); } catch (JSONException e) { Log.e(TAG, "JSON Exception when reading SMS Mailbox", e); c.close(); } c.close(); return results; }
From source file:it.bradipao.berengar.DbTool.java
public static JSONObject table2json(SQLiteDatabase mDB, String sTableName, String sTableSql) { // vars//w w w . j a v a 2s.c o m JSONObject jsonTable = new JSONObject(); JSONArray jsonRows = new JSONArray(); JSONArray jsonColsName = new JSONArray(); JSONArray jsonCols = null; // read table String sqlquery = "select * from " + sTableName; Cursor cur = mDB.rawQuery(sqlquery, null); // iteratew through rows int i = -1; while (cur.moveToNext()) { // at first element store column names if (i == -1) for (i = 0; i < cur.getColumnCount(); i++) { jsonColsName.put(cur.getColumnName(i)); } // get values jsonCols = new JSONArray(); for (i = 0; i < cur.getColumnCount(); i++) { jsonCols.put(cur.getString(i)); } // add values to rows array jsonRows.put(jsonCols); } // final json building try { // table name jsonTable.put("table_name", sTableName); // code for create table if ((sTableSql != null) && (!sTableSql.isEmpty())) jsonTable.put("table_sql", sTableSql); // columns name jsonTable.put("cols_name", jsonColsName); // rows jsonTable.put("rows", jsonRows); } catch (JSONException e) { Log.e(LOGTAG, "error in table2json", e); } // return String return jsonTable; }
From source file:it.ms.theing.loquitur.functions.PhoneDir.java
private String getEmail(String ID) { Cursor emails = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + ID, null, null); String s = ""; if (emails.moveToNext()) { s = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); }/*www.j av a 2 s . c om*/ emails.close(); return s; }
From source file:it.ms.theing.loquitur.functions.PhoneDir.java
private String getPhone(String ID) { Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + ID, null, null); String s = ""; if (phones.moveToNext()) { s = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); }/* www .j a v a 2 s.co m*/ phones.close(); return s; }
From source file:com.googlecode.android_scripting.facade.ContactsFacade.java
@Rpc(description = "Returns a List of all contact IDs.") public List<Integer> contactsGetIds() { List<Integer> result = new ArrayList<Integer>(); String[] columns = { "_id" }; Cursor cursor = mContentResolver.query(CONTACTS_URI, columns, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { result.add(cursor.getInt(0)); }/*from w ww. java 2 s . c o m*/ cursor.close(); } return result; }
From source file:com.clutch.ClutchStats.java
public int getCachedChoice(String name) { int resp = -1; SQLiteDatabase db = getReadableDatabase(); String[] args = { name };/*from ww w . j a v a 2 s . c o m*/ Cursor cur = db.rawQuery("SELECT choice FROM abcache WHERE name = ?", args); cur.moveToFirst(); while (!cur.isAfterLast()) { resp = cur.getInt(0); cur.moveToNext(); } db.close(); return resp; }
From source file:de.escoand.readdaily.DownloadHandler.java
@Override public void onReceive(final Context context, final Intent intent) { final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); final Database db = Database.getInstance(context); final Cursor downloads = db.getDownloads(); LogHandler.log(Log.WARN, "receive starting"); while (downloads.moveToNext()) { final long id = downloads.getLong(downloads.getColumnIndex(Database.COLUMN_ID)); final String name = downloads.getString(downloads.getColumnIndex(Database.COLUMN_SUBSCRIPTION)); final String mime = downloads.getString(downloads.getColumnIndex(Database.COLUMN_TYPE)); final Cursor download = manager.query(new DownloadManager.Query().setFilterById(id)); // download exists if (!download.moveToFirst()) continue; // download finished if (download.getInt( download.getColumnIndex(DownloadManager.COLUMN_STATUS)) != DownloadManager.STATUS_SUCCESSFUL) continue; // import file in background new Thread(new Runnable() { @Override// w ww .j ava 2 s . com public void run() { try { LogHandler.log(Log.WARN, "import starting of " + name); final FileInputStream stream = new ParcelFileDescriptor.AutoCloseInputStream( manager.openDownloadedFile(id)); final String mimeServer = manager.getMimeTypeForDownloadedFile(id); LogHandler.log(Log.INFO, "id: " + String.valueOf(id)); LogHandler.log(Log.INFO, "manager: " + manager.toString()); LogHandler.log(Log.INFO, "stream: " + stream.toString()); LogHandler.log(Log.INFO, "mime: " + mime); LogHandler.log(Log.INFO, "mimeServer: " + mimeServer); switch (mime != null ? mime : (mimeServer != null ? mimeServer : "")) { // register feedback case "application/json": final byte[] buf = new byte[256]; final int len = stream.read(buf); LogHandler.log(Log.WARN, "register feedback: " + new String(buf, 0, len)); break; // csv data case "text/plain": db.importCSV(name, stream); break; // xml data case "application/xml": case "text/xml": db.importXML(name, stream); break; // zipped data case "application/zip": db.importZIP(name, stream); break; // do nothing default: LogHandler.log(new IntentFilter.MalformedMimeTypeException()); break; } stream.close(); LogHandler.log(Log.WARN, "import finished (" + name + ")"); } // file error catch (FileNotFoundException e) { LogHandler.logAndShow(e, context, R.string.message_download_open); } // stream error catch (IOException e) { LogHandler.logAndShow(e, context, R.string.message_download_read); } // xml error catch (XmlPullParserException e) { LogHandler.logAndShow(e, context, R.string.message_download_xml); } // clean finally { manager.remove(id); db.removeDownload(id); LogHandler.log(Log.WARN, "clean finished"); } } }).start(); } downloads.close(); LogHandler.log(Log.WARN, "receiving done"); }
From source file:com.yozio.android.YozioDataStoreImpl.java
public Events getEvents(int limit) { synchronized (this) { JSONArray jsonArray = null;//from w w w . j a va2 s . co m String lastEventId = null; try { Cursor cursor = dbHelper.getReadableDatabase().rawQuery("SELECT _id, " + DATA + " FROM " + EVENTS_TABLE + where() + " ORDER BY _id ASC LIMIT " + limit, null); jsonArray = new JSONArray(); while (cursor.moveToNext()) { if (cursor.isLast()) { lastEventId = cursor.getString(0); } try { jsonArray.put(new JSONObject(cursor.getString(1))); } catch (JSONException e) { // Ignore event. } } cursor.close(); } catch (SQLException e) { Log.e(LOGTAG, "getEvents", e); } finally { dbHelper.close(); } if (jsonArray != null && lastEventId != null) { return new Events(jsonArray, lastEventId); } else { return null; } } }