Example usage for android.database Cursor getColumnCount

List of usage examples for android.database Cursor getColumnCount

Introduction

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

Prototype

int getColumnCount();

Source Link

Document

Return total number of columns

Usage

From source file:fr.unix_experience.owncloud_sms.engine.SmsFetcher.java

public void bufferizeMessagesSinceDate(MailboxID mbID, Long sinceDate) {
    String mbURI = mapMailboxIDToURI(mbID);

    if (_context == null || mbURI == null) {
        return;/* w  w  w.  j  a va2 s .  co  m*/
    }

    Cursor c = new SmsDataProvider(_context).query(mbURI, "date > ?", new String[] { sinceDate.toString() });

    // Reading mailbox
    if (c != null && c.getCount() > 0) {
        c.moveToFirst();
        do {
            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 {
                        // Special case for date, we need to record last without searching
                        if (colName.equals(new String("date"))) {
                            final Long tmpDate = c.getLong(idx);
                            if (tmpDate > _lastMsgDate) {
                                _lastMsgDate = tmpDate;
                            }
                        }
                        entry.put(colName, c.getString(idx));
                    }
                }

                // Mailbox ID is required by server
                entry.put("mbox", mbID.ordinal());

                _jsonDataDump.put(entry);

            } catch (JSONException e) {
                Log.e(TAG, "JSON Exception when reading SMS Mailbox", e);
                c.close();
            }
        } while (c.moveToNext());

        Log.d(TAG, c.getCount() + " messages read from " + mbURI);

        c.close();
    }
}

From source file:at.bitfire.ical4android.AndroidTaskList.java

protected AndroidTask[] queryTasks(String where, String[] whereArgs) throws CalendarStorageException {
    where = (where == null ? "" : "(" + where + ") AND ") + Tasks.LIST_ID + "=?";
    whereArgs = ArrayUtils.add(whereArgs, String.valueOf(id));

    @Cleanup
    Cursor cursor = null;//from w  w  w  . j  a v  a 2s. co m
    try {
        cursor = provider.client.query(syncAdapterURI(provider.tasksUri()), taskBaseInfoColumns(), where,
                whereArgs, null);
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query calendar events", e);
    }

    List<AndroidTask> tasks = new LinkedList<>();
    while (cursor != null && cursor.moveToNext()) {
        ContentValues baseInfo = new ContentValues(cursor.getColumnCount());
        DatabaseUtils.cursorRowToContentValues(cursor, baseInfo);
        tasks.add(taskFactory.newInstance(this, cursor.getLong(0), baseInfo));
    }
    return tasks.toArray(taskFactory.newArray(tasks.size()));
}

From source file:at.bitfire.ical4android.AndroidCalendar.java

protected AndroidEvent[] queryEvents(String where, String[] whereArgs) throws CalendarStorageException {
    where = (where == null ? "" : "(" + where + ") AND ") + Events.CALENDAR_ID + "=?";
    whereArgs = ArrayUtils.add(whereArgs, String.valueOf(id));

    @Cleanup
    Cursor cursor = null;//from w w  w  .j a  va  2  s  . com
    try {
        cursor = provider.query(syncAdapterURI(Events.CONTENT_URI), eventBaseInfoColumns(), where, whereArgs,
                null);
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query calendar events", e);
    }

    List<AndroidEvent> events = new LinkedList<>();
    while (cursor != null && cursor.moveToNext()) {
        ContentValues baseInfo = new ContentValues(cursor.getColumnCount());
        DatabaseUtils.cursorRowToContentValues(cursor, baseInfo);
        events.add(eventFactory.newInstance(this, cursor.getLong(0), baseInfo));
    }
    return events.toArray(eventFactory.newArray(events.size()));
}

From source file:com.dileepindia.cordova.sms.SMSPlugin.java

private JSONObject getJsonFromCursor(Cursor cur) {
    JSONObject json = new JSONObject();

    int nCol = cur.getColumnCount();
    String[] keys = cur.getColumnNames();
    try {// w  ww .  j  av  a 2 s  . c o  m
        for (int j = 0; j < nCol; j++) {
            switch (cur.getType(j)) {
            /*case 0: 
            json.put(keys[j], null);
            break;
            case 1: 
            json.put(keys[j], cur.getInt(j));
            break;
                      
            case 2: 
            json.put(keys[j], cur.getLong(j));
            break;
            case 3: 
            json.put(keys[j], cur.getFloat(j));
            break;
            case 4: 
            json.put(keys[j], cur.getString(j));
            break;
                     
            case 5: 
            json.put(keys[j], cur.getBlob(j));
              */
            case Cursor.FIELD_TYPE_BLOB:
                json.put(keys[j], cur.getBlob(j).toString());
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                json.put(keys[j], cur.getDouble(j));
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                json.put(keys[j], cur.getLong(j));
                break;
            case Cursor.FIELD_TYPE_NULL:
                json.put(keys[j], cur);
                break;
            case Cursor.FIELD_TYPE_STRING:
                json.put(keys[j], cur.getString(j));
                break;

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return json;
}

From source file:com.photowall.oauth.util.BaseHttpClient.java

/**
 * //from  w  w  w  .  j a  v a  2  s .co  m
 * @param mContext
 */
private void setProxy(Context mContext) {
    try {
        ContentValues values = new ContentValues();
        Cursor cur = mContext.getContentResolver().query(Uri.parse("content://telephony/carriers/preferapn"),
                null, null, null, null);
        if (cur != null && cur.getCount() > 0) {
            if (cur.moveToFirst()) {
                int colCount = cur.getColumnCount();
                for (int i = 0; i < colCount; i++) {
                    values.put(cur.getColumnName(i), cur.getString(i));
                }
            }
            cur.close();
        }
        String proxyHost = (String) values.get("proxy");
        if (!TextUtils.isEmpty(proxyHost) && !isWiFiConnected(mContext)) {
            int prot = Integer.parseInt(String.valueOf(values.get("port")));
            delegate.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, prot),
                    new UsernamePasswordCredentials((String) values.get("user"),
                            (String) values.get("password")));
            HttpHost proxy = new HttpHost(proxyHost, prot);
            delegate.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.w(TAG, "set proxy error+++++++++++++++++");
    }
}

From source file:com.triarc.sync.SyncAdapter.java

private void debugRow(Cursor cursor) {
    for (int index = 0; index < cursor.getColumnCount(); index++) {

        Log.d(TAG, cursor.getColumnName(index) + " => " + cursor.getString(index));
    }/*from   w w w .jav a2s  .c  o  m*/
}

From source file:com.nnm.smsviet.Message.java

/**
 * Update {@link Message}.//from  w  w  w  . j  ava 2s. com
 * 
 * @param cursor
 *            {@link Cursor} to read from
 */
public void update(final Cursor cursor) {
    this.read = cursor.getInt(INDEX_READ);
    this.type = cursor.getInt(INDEX_TYPE);
    try {
        if (cursor.getColumnCount() > INDEX_MTYPE) {
            final int t = cursor.getInt(INDEX_MTYPE);
            if (t != 0) {
                this.type = t;
            }
        }
    } catch (IllegalStateException e) {
        Log.e(TAG, "wrong projection?", e);
    }
}

From source file:fr.unix_experience.owncloud_sms.engine.SmsFetcher.java

private void bufferizeMailboxMessages(MailboxID mbID) {
    String mbURI = mapMailboxIDToURI(mbID);

    if (_context == null || mbURI == null) {
        return;/*w w w .  j av  a 2  s . c o  m*/
    }

    if (mbID != MailboxID.INBOX && mbID != MailboxID.SENT && mbID != MailboxID.DRAFTS) {
        Log.e(TAG, "Unhandled MailboxID " + mbID.ordinal());
        return;
    }

    // We generate a ID list for this message box
    String existingIDs = buildExistingMessagesString(mbID);

    Cursor c = null;
    if (existingIDs.length() > 0) {
        c = (new SmsDataProvider(_context)).query(mbURI, "_id NOT IN (" + existingIDs + ")");
    } else {
        c = (new SmsDataProvider(_context)).query(mbURI);
    }

    // Reading mailbox
    if (c != null && c.getCount() > 0) {
        c.moveToFirst();
        do {
            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 {
                        // Special case for date, we need to record last without searching
                        if (colName.equals(new String("date"))) {
                            final Long tmpDate = c.getLong(idx);
                            if (tmpDate > _lastMsgDate) {
                                _lastMsgDate = tmpDate;
                            }
                        }
                        entry.put(colName, c.getString(idx));
                    }
                }

                // Mailbox ID is required by server
                entry.put("mbox", mbID.ordinal());

                _jsonDataDump.put(entry);

            } catch (JSONException e) {
                Log.e(TAG, "JSON Exception when reading SMS Mailbox", e);
                c.close();
            }
        } while (c.moveToNext());

        Log.d(TAG, c.getCount() + " messages read from " + mbURI);

        c.close();
    }
}

From source file:com.nextgis.mobile.fragment.AttributesFragment.java

private String parseAttributes(String data) throws RuntimeException {
    String selection = Constants.FIELD_ID + " = ?";
    Cursor attributes = mLayer.query(null, selection, new String[] { mItemId + "" }, null, null);
    if (null == attributes || attributes.getCount() == 0)
        return data;

    if (attributes.moveToFirst()) {
        for (int i = 0; i < attributes.getColumnCount(); i++) {
            String column = attributes.getColumnName(i);
            String text, alias;//www.  j  a v  a  2  s. c  o  m

            if (column.startsWith(Constants.FIELD_GEOM_))
                continue;

            if (column.equals(Constants.FIELD_GEOM)) {
                switch (mLayer.getGeometryType()) {
                case GTPoint:
                    try {
                        GeoPoint pt = (GeoPoint) GeoGeometryFactory.fromBlob(attributes.getBlob(i));
                        data += getRow(getString(R.string.coordinates), formatCoordinates(pt));
                    } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    continue;
                case GTMultiPoint:
                    try {
                        GeoMultiPoint mpt = (GeoMultiPoint) GeoGeometryFactory.fromBlob(attributes.getBlob(i));
                        data += getRow(getString(R.string.center),
                                formatCoordinates(mpt.getEnvelope().getCenter()));
                    } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    continue;
                case GTLineString:
                    try {
                        GeoLineString line = (GeoLineString) GeoGeometryFactory.fromBlob(attributes.getBlob(i));
                        data += getRow(getString(R.string.length),
                                LocationUtil.formatLength(getContext(), line.getLength(), 3));
                    } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    continue;
                case GTMultiLineString:
                    try {
                        GeoMultiLineString multiline = (GeoMultiLineString) GeoGeometryFactory
                                .fromBlob(attributes.getBlob(i));
                        data += getRow(getString(R.string.length),
                                LocationUtil.formatLength(getContext(), multiline.getLength(), 3));
                    } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    continue;
                case GTPolygon:
                    try {
                        GeoPolygon polygon = (GeoPolygon) GeoGeometryFactory.fromBlob(attributes.getBlob(i));
                        data += getRow(getString(R.string.perimeter),
                                LocationUtil.formatLength(getContext(), polygon.getPerimeter(), 3));
                        data += getRow(getString(R.string.area),
                                LocationUtil.formatArea(getContext(), polygon.getArea()));
                    } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    continue;
                case GTMultiPolygon:
                    try {
                        GeoMultiPolygon polygon = (GeoMultiPolygon) GeoGeometryFactory
                                .fromBlob(attributes.getBlob(i));
                        data += getRow(getString(R.string.perimeter),
                                LocationUtil.formatLength(getContext(), polygon.getPerimeter(), 3));
                        data += getRow(getString(R.string.area),
                                LocationUtil.formatArea(getContext(), polygon.getArea()));
                    } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    continue;
                default:
                    continue;
                }
            }

            Field field = mLayer.getFieldByName(column);
            int fieldType = field != null ? field.getType() : Constants.NOT_FOUND;
            switch (fieldType) {
            case GeoConstants.FTInteger:
                text = attributes.getInt(i) + "";
                break;
            case GeoConstants.FTReal:
                NumberFormat nf = NumberFormat.getInstance();
                nf.setMaximumFractionDigits(4);
                nf.setGroupingUsed(false);
                text = nf.format(attributes.getDouble(i));
                break;
            case GeoConstants.FTDate:
            case GeoConstants.FTTime:
            case GeoConstants.FTDateTime:
                text = formatDateTime(attributes.getLong(i), fieldType);
                break;
            default:
                text = toString(attributes.getString(i));
                Pattern pattern = Pattern.compile(URL_PATTERN);
                Matcher match = pattern.matcher(text);
                while (match.matches()) {
                    String url = text.substring(match.start(), match.end());
                    text = text.replaceFirst(URL_PATTERN, "<a href = '" + url + "'>" + url + "</a>");
                    match = pattern.matcher(text.substring(match.start() + url.length() * 2 + 17));
                }
                break;
            }

            if (field != null)
                alias = field.getAlias();
            else if (column.equals(Constants.FIELD_ID))
                alias = Constants.FIELD_ID;
            else
                alias = "";

            data += getRow(alias, text);
        }
    }

    attributes.close();
    return data;
}

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

/**
 * Update {@link Message}.// w w  w  . j  a v a  2  s . c o  m
 *
 * @param cursor {@link Cursor} to read from
 */
public void update(final Cursor cursor) {
    read = cursor.getInt(INDEX_READ);
    type = cursor.getInt(INDEX_TYPE);
    try {
        if (cursor.getColumnCount() > INDEX_MTYPE) {
            final int t = cursor.getInt(INDEX_MTYPE);
            if (t != 0) {
                type = t;
            }
        }
    } catch (IllegalStateException e) {
        Log.e(TAG, "wrong projection?", e);
    }
}