Example usage for android.database Cursor moveToNext

List of usage examples for android.database Cursor moveToNext

Introduction

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

Prototype

boolean moveToNext();

Source Link

Document

Move the cursor to the next row.

Usage

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

/**
 * Calculates directory content size, recursively if necessary.
 * /*w  ww  . java 2  s. c om*/
 * @param resourcePath
 *            the directory resource path to calculate size of
 * @param recursive
 *            the flag indicating recursive calculation
 * @return the resorucePath defined directory size
 */
public static long getDirectorySize(final String resourcePath, final boolean recursive) {
    final String[] projection = new String[] { Nodes.NODE_RESOURCE_PATH, Nodes.NODE_KIND, Nodes.NODE_SIZE };
    final String selection = Nodes.NODE_PARENT_PATH + "=?";
    final String[] selectionArgs = new String[] { resourcePath };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    U1NodeKind kind;
    long size = 0L;
    try {
        if (c.moveToFirst()) {
            do {
                kind = U1NodeKind
                        .valueOf(c.getString(c.getColumnIndex(Nodes.NODE_KIND)).toUpperCase(Locale.US));
                if (U1NodeKind.FILE == kind) {
                    size += c.getLong(c.getColumnIndex(Nodes.NODE_SIZE));
                } else if (U1NodeKind.DIRECTORY == kind && recursive) {
                    final String subDirResourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
                    size += getDirectorySize(subDirResourcePath, true);
                }
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return size;
}

From source file:com.esri.squadleader.model.GeoPackageReader.java

/**
 * Reads the tables in a GeoPackage, makes a layer from each table, and returns a list containing
 * those layers.//from  w w w  .ja  va  2s .  c o m
 *
 * @param gpkgPath       the full path to the .gpkg file.
 * @param sr             the spatial reference to which any raster layers should be projected, typically the
 *                       spatial reference of your map.
 * @param showVectors    if true, this method will include the GeoPackage's vector layers.
 * @param showRasters    if true, this method will include the GeoPackage's raster layer.
 * @param rasterRenderer the renderer to be used for raster layers. One simple option is an RGBRenderer.
 * @param markerRenderer the renderer to be used for point layers.
 * @param lineRenderer   the renderer to be used for polyline layers.
 * @param fillRenderer   the renderer to be used for polygon layers.
 * @return a list of the layers created for all tables in the GeoPackage.
 * @throws IOException if gpkgPath cannot be read. Possible reasons include the file not
 *                     existing, failure to request READ_EXTERNAL_STORAGE or
 *                     WRITE_EXTERNAL_STORAGE permission, or the GeoPackage containing an
 *                     invalid spatial reference.
 */
public List<Layer> readGeoPackageToLayerList(String gpkgPath, SpatialReference sr, boolean showVectors,
        boolean showRasters, RasterRenderer rasterRenderer, Renderer markerRenderer, Renderer lineRenderer,
        Renderer fillRenderer) throws IOException {
    List<Layer> layers = new ArrayList<Layer>();

    if (showRasters) {
        // Check to see if there are any rasters before loading them
        SQLiteDatabase sqliteDb = null;
        Cursor cursor = null;
        try {
            sqliteDb = SQLiteDatabase.openDatabase(gpkgPath, null, SQLiteDatabase.OPEN_READONLY);
            cursor = sqliteDb.rawQuery("SELECT COUNT(*) FROM gpkg_contents WHERE data_type = ?",
                    new String[] { "tiles" });
            if (cursor.moveToNext()) {
                if (0 < cursor.getInt(0)) {
                    cursor.close();
                    sqliteDb.close();
                    FileRasterSource src = new FileRasterSource(gpkgPath);
                    rasterSources.add(src);
                    if (null != sr) {
                        src.project(sr);
                    }
                    RasterLayer rasterLayer = new RasterLayer(src);
                    rasterLayer.setRenderer(rasterRenderer);
                    rasterLayer
                            .setName((gpkgPath.contains("/") ? gpkgPath.substring(gpkgPath.lastIndexOf("/") + 1)
                                    : gpkgPath) + " (raster)");
                    layers.add(rasterLayer);
                }
            }
        } catch (Throwable t) {
            Log.e(TAG, "Could not read raster(s) from GeoPackage", t);
        } finally {
            if (null != cursor) {
                cursor.close();
            }
            if (null != sqliteDb) {
                sqliteDb.close();
            }
        }
    }

    if (showVectors) {
        Geopackage gpkg;
        try {
            gpkg = new Geopackage(gpkgPath);
        } catch (RuntimeException ex) {
            throw new IOException(null != ex.getMessage() && ex.getMessage().contains("unknown wkt")
                    ? "Geopackage " + gpkgPath + " contains an invalid spatial reference."
                    : null, ex);
        }
        geopackages.add(gpkg);
        List<GeopackageFeatureTable> tables = gpkg.getGeopackageFeatureTables();
        if (0 < tables.size()) {
            //First pass: polygons and unknowns
            HashSet<Geometry.Type> types = new HashSet<Geometry.Type>();
            types.add(Geometry.Type.ENVELOPE);
            types.add(Geometry.Type.POLYGON);
            types.add(Geometry.Type.UNKNOWN);
            layers.addAll(getTablesAsLayers(tables, types, fillRenderer));

            //Second pass: lines
            types.clear();
            types.add(Geometry.Type.LINE);
            types.add(Geometry.Type.POLYLINE);
            layers.addAll(getTablesAsLayers(tables, types, lineRenderer));

            //Third pass: points
            types.clear();
            types.add(Geometry.Type.MULTIPOINT);
            types.add(Geometry.Type.POINT);
            layers.addAll(getTablesAsLayers(tables, types, markerRenderer));
        }
    }

    return layers;
}

From source file:com.googlecode.android_scripting.facade.ContactsFacade.java

private String findPhone(String id) {
    String result = null;/* w  ww .  ja va  2 s . c  o  m*/
    if (id == null || id.equals("")) {
        return result;
    }
    try {
        if (Integer.parseInt(id) > 0) {
            Cursor pCur = mContentResolver.query(mPhoneContent, new String[] { mPhoneNumber },
                    mContactId + " = ? and " + mPrimary + "=1", new String[] { id }, null);
            if (pCur != null) {
                pCur.getColumnNames();
                while (pCur.moveToNext()) {
                    result = pCur.getString(0);
                    break;
                }
            }
            pCur.close();
        }
    } catch (Exception e) {
        return null;
    }
    return result;
}

From source file:org.maikelwever.droidpile.SendMailActivity.java

public ArrayList<AutocompleteContact> getContacts() {
    ArrayList<AutocompleteContact> alContacts = new ArrayList<AutocompleteContact>();
    ContentResolver contResv = getApplication().getContentResolver();
    Cursor cursor = contResv.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cursor.moveToFirst()) {
        do {/*  ww  w.j  av a 2s. c om*/
            String name = cursor
                    .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            AutocompleteContact contact = new AutocompleteContact(id, name);

            Cursor emails = contResv.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contact.id, null, null);
            while (emails.moveToNext()) {
                contact.addEmail(
                        emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
                break;
            }
            emails.close();

            alContacts.add(contact);

        } while (cursor.moveToNext());
    }

    cursor.close();
    return alContacts;
}

From source file:com.helpshift.kvstore.SharedPreferencesImpl.java

@Override
public Map<String, ?> getAll() {
    Cursor cursor = null;
    try {//from   w  w  w .ja v a2 s  .  co  m
        String selectAllQuery = "SELECT * FROM " + mPreferenceName;
        cursor = mSqLiteDatabase.rawQuery(selectAllQuery, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                parseSettingCursor(cursor);
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return mMap;
}

From source file:foam.zizim.android.BoskoiService.java

public static BlogData getBlogData(int id) {
    Cursor cursor;
    cursor = BoskoiApplication.mDb.fetchBlogById(id);
    BlogData blogData = null;/*from  ww w.j  av a 2s  . c om*/

    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DATE);
        int descIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DESCRIPTION);

        int linkIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_LINK);

        do {

            blogData = new BlogData();

            blogData.setId(Util.toInt(cursor.getString(idIndex)));
            blogData.setTitle(cursor.getString(titleIndex));
            blogData.setDescription(cursor.getString(descIndex));
            blogData.setLink(cursor.getString(linkIndex));
            blogData.setDate(cursor.getString(dateIndex));

        } while (cursor.moveToNext());

    }
    cursor.close();

    return blogData;
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.services.transformationmanager.database.LocalTransformationDBMS.java

public ArrayList<String> getAllAvalDate() {
    ArrayList<String> list = new ArrayList<String>();
    String q = "SELECT * FROM " + LocalTransformationDB.TABLE_DATE_TO_TRAFFIC + " ORDER BY "
            + LocalTransformationDB.COLUMN_DATE_ID + ";";
    Cursor cursor = database.rawQuery(q, null);
    if (cursor.moveToFirst()) {
        do {/*from w  w w.  jav a 2s . c  o  m*/
            String date = cursor.getString(cursor.getColumnIndex(LocalTransformationDB.COLUMN_DATE_TEXT));
            if (!list.contains(date))
                list.add(date);
        } while (cursor.moveToNext());
    }
    cursor.close();

    Collections.sort(list, new Comparator<String>() {

        @Override
        public int compare(String arg0, String arg1) {
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            int compareResult = 0;
            try {
                Date arg0Date = format.parse(arg0);
                Date arg1Date = format.parse(arg1);
                compareResult = arg0Date.compareTo(arg1Date);
            } catch (ParseException e) {
                e.printStackTrace();
                compareResult = arg0.compareTo(arg1);
            }
            return compareResult;
        }
    });

    for (String s : list) {
        Log.e(TAG, "AvalDate:" + s);
    }
    return list;
}

From source file:com.android.mms.MmsConfig.java

public static int updateAllQuicktexts() {
    Cursor cursor = mContext.getContentResolver().query(Telephony.MmsSms.CONTENT_URI_QUICKTEXT, null, null,
            null, null);/*from   ww  w .ja  v a 2 s . c  o  m*/
    allQuickTextIds.clear();
    allQuickTexts.clear();
    String[] defaultTexts = mContext.getResources().getStringArray(R.array.default_quick_texts);
    int maxId = defaultTexts.length;
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                int qtId = cursor.getInt(0);
                allQuickTextIds.add(qtId);
                String qtText = cursor.getString(1);
                if (qtText != null && !qtText.isEmpty()) {
                    allQuickTexts.add(qtText);
                } else {
                    allQuickTexts.add(defaultTexts[qtId - 1]);
                }
                if (qtId > maxId) {
                    maxId = qtId;
                }
            }
        } finally {
            cursor.close();
        }
    }
    return maxId;
}

From source file:com.crankworks.cycletracks.TripUploader.java

@Override
protected Boolean doInBackground(Long... tripid) {
    // First, send the trip user asked for:
    Boolean result = uploadOneTrip(tripid[0]);

    // Then, automatically try and send previously-completed trips
    // that were not sent successfully.
    Vector<Long> unsentTrips = new Vector<Long>();

    mDb.openReadOnly();/*from  ww w .j a  v  a2 s . c  o m*/
    Cursor cur = mDb.fetchUnsentTrips();
    if (cur != null && cur.getCount() > 0) {
        //pd.setMessage("Sent. You have previously unsent trips; submitting those now.");
        while (!cur.isAfterLast()) {
            unsentTrips.add(new Long(cur.getLong(0)));
            cur.moveToNext();
        }
        cur.close();
    }
    mDb.close();

    for (Long trip : unsentTrips) {
        result &= uploadOneTrip(trip);
    }
    return result;
}

From source file:com.wso2.mobile.mdm.api.TrackCallSMS.java

/**
 * Returns a JSONArray of call detail objects Ex: [{number:"0112345666", type:"INCOMING", date:"dd/MM/yyyy hh:mm:ss.SSS", duration:"90"}]
 *//*from   w ww .ja  v  a  2  s .  c  o m*/
public JSONArray getCallDetails() {
    JSONArray jsonArray = null;
    try {
        Cursor managedCursor = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        jsonArray = new JSONArray();

        while (managedCursor.moveToNext()) {
            JSONObject jsonObj = new JSONObject();
            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            Date callDayTime = new Date(Long.valueOf(callDate));
            String callDuration = managedCursor.getString(duration);
            String dir = null;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            }
            jsonObj.put("number", phNumber);
            jsonObj.put("type", dir);
            jsonObj.put("date", callDayTime);
            jsonObj.put("duration", callDuration);
            jsonArray.add(jsonObj);
        }

        managedCursor.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return jsonArray;
}