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:Main.java

public static Map<String, Integer> getColumnMap(Cursor cursor) {
    Map<String, Integer> retMap = new HashMap<String, Integer>();
    int nCols = cursor.getColumnCount();
    for (int i = 0; i < nCols; i++) {
        retMap.put(cursor.getColumnName(i), i);
    }/*from w  ww  .  j ava 2s .  c  o  m*/
    return retMap;
}

From source file:Main.java

private static String dumpRow(Cursor c) {
    String result = "VALUES(";

    for (int pos = 0; pos < c.getColumnCount(); pos++) {
        switch (c.getType(pos)) {
        case Cursor.FIELD_TYPE_NULL:
            result += "null";
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            result += Integer.toString(c.getInt(pos));
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            result += Float.toString(c.getFloat(pos));
            break;
        case Cursor.FIELD_TYPE_STRING:
            result += DatabaseUtils.sqlEscapeString(c.getString(pos));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            result += "X'";
            for (byte b : c.getBlob(pos)) {
                result += String.format("%02X", b);
            }/* w  ww  .  j  a v a  2  s. com*/
            result += "'";
            break;
        default:
            return "Invalid field type " + c.getType(pos) + " at position " + pos;
        }

        if (pos < c.getColumnCount() - 1) {
            result += ", ";
        }
    }
    return result + ")";
}

From source file:Main.java

public static String logCursor(String prefix, Cursor cr) {
    StringBuilder sb = new StringBuilder().append(prefix + ": ");
    for (int i = 0; i < cr.getColumnCount(); i++) {
        sb.append(cr.getColumnName(i)).append("=");
        switch (cr.getType(i)) {
        case Cursor.FIELD_TYPE_NULL:
            sb.append("NULL");
            break;
        case Cursor.FIELD_TYPE_STRING:
            sb.append("\"").append(cr.getString(i)).append("\"");
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            sb.append(cr.getInt(i));/*  ww  w.  ja v  a 2s. com*/
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            sb.append(cr.getFloat(i));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            sb.append("BIN(").append(cr.getBlob(i).length).append("b)");
            break;
        }
        sb.append(" ");
    }
    return sb.toString();
}

From source file:Main.java

public static String getGSFID(Context context) {
    String result;//  w ww  .j  a  va2  s  .c  om
    final Uri URI = Uri.parse("content://com.google.android.gsf.gservices");
    final String ID_KEY = "android_id";
    String[] params = { ID_KEY };
    Cursor c = context.getContentResolver().query(URI, null, null, params, null);
    if (c == null || !c.moveToFirst() || c.getColumnCount() < 2) {
        return null;
    } else {
        result = Long.toHexString(Long.parseLong(c.getString(1)));
    }
    c.close();
    return result;
}

From source file:Main.java

public static String[] singleRecordToArray(Cursor cursor) {
    String[] result = null;/*from  w ww. j a va2s . c om*/
    try {
        if (cursor.moveToNext()) {
            result = new String[cursor.getColumnCount()];
            fillArray(cursor, result);
        }
    } finally {
        closeQuietly(cursor);
    }
    return result;
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, Context ctx) {
    Log.d("thong", "Uri: " + contentUri.toString());

    try {/*from ww  w .j  a va  2  s  . co m*/
        String realpath = "";

        String[] proj = { MediaStore.Images.Media.DATA };

        //Cursor cursor = ((Activity) ctx).managedQuery(contentUri, proj, null, null, null);

        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);

        Log.d("thong", "Column count: " + cursor.getColumnCount());

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        realpath = cursor.getString(column_index);

        cursor.close();

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

From source file:Main.java

static public void dumpCursor(Cursor cursor) {
    final String LOG_TAG = "dumpCursor";
    if (cursor != null) {
        Log.d(LOG_TAG, "cursor " + cursor.getCount());
        for (int i = 0; i < cursor.getColumnCount(); i++)
            Log.d(LOG_TAG, "col " + i + " " + cursor.getColumnName(i));

        cursor.moveToFirst();/*from w ww.  jav  a2s .com*/
        String[] a = new String[cursor.getColumnCount()];
        while (!cursor.isAfterLast()) {
            for (int i = 0; i < a.length; i++)
                a[i] = cursor.getString(i);
            Log.d(LOG_TAG, Arrays.toString(a));
            cursor.moveToNext();
        }

        cursor.moveToFirst();
    }
}

From source file:Main.java

public static List<String[]> multiRecordToArray(Cursor cursor) {
    ArrayList<String[]> result = new ArrayList<String[]>();
    try {//from ww  w.j  a  v  a 2  s . co  m
        while (cursor.moveToNext()) {
            String[] record = new String[cursor.getColumnCount()];
            fillArray(cursor, record);
            result.add(record);
        }
    } finally {
        closeQuietly(cursor);
    }
    return result;
}

From source file:Main.java

/**
 * Print the content of the cursor//from  w  w  w  .  j  a v  a2s  .c o  m
 *
 * @param cursor, The cursor, which content needs to be printed
 * @param logTag, The log tag
 */
public static void printCursorContent(Cursor cursor, String logTag) {
    if (cursor == null) {
        Log.d(logTag, "Cursor is NULL!");
        return;
    }
    final int columnSpace = 2;
    ArrayList<Integer> columnWidth = new ArrayList<Integer>();
    for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
        String value = cursor.getColumnName(columnIndex);
        int maxWidth = value.length();
        if (cursor.moveToFirst()) {
            do {
                try {
                    value = cursor.getString(columnIndex);
                } catch (Exception e) {
                    value = "BLOB";
                    Log.d(logTag, "Get value from " + cursor.getColumnName(columnIndex) + " failed. Caused by "
                            + e.getLocalizedMessage());
                }
                if (!TextUtils.isEmpty(value) && value.length() > maxWidth) {
                    maxWidth = value.length();
                }
            } while (cursor.moveToNext());
        }
        columnWidth.add(maxWidth + columnSpace);
    }
    ArrayList<ArrayList<String>> tableContent = new ArrayList<ArrayList<String>>();
    for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
        ArrayList<String> columnContent = new ArrayList<String>();
        String value = cursor.getColumnName(columnIndex);
        columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex)));
        if (cursor.moveToFirst()) {
            do {
                try {
                    value = cursor.getString(columnIndex);
                } catch (Exception e) {
                    value = "BLOB";
                }
                columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex)));
            } while (cursor.moveToNext());
        }
        tableContent.add(columnContent);
    }
    // Including the header
    int maxRowIndex = cursor.getCount() + 1;
    for (int rowIndex = 0; rowIndex < maxRowIndex; rowIndex++) {
        StringBuilder rowValues = new StringBuilder();
        for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
            ArrayList<String> columnValues = tableContent.get(columnIndex);
            rowValues.append(columnValues.get(rowIndex));
        }
        Log.d(logTag, rowValues.toString());
    }
    // set the cursor back the first item
    cursor.moveToFirst();
}

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

public static AndroidTaskList[] find(Account account, TaskProvider provider, AndroidTaskListFactory factory,
        String where, String whereArgs[]) throws CalendarStorageException {
    List<AndroidTaskList> taskLists = new LinkedList<>();
    try {/* w  w  w .j a va 2s . c  o m*/
        @Cleanup
        Cursor cursor = provider.client.query(syncAdapterURI(provider.taskListsUri(), account), null, null,
                null, null);
        while (cursor != null && cursor.moveToNext()) {
            ContentValues values = new ContentValues(cursor.getColumnCount());
            DatabaseUtils.cursorRowToContentValues(cursor, values);
            AndroidTaskList taskList = factory.newInstance(account, provider, values.getAsLong(TaskLists._ID));
            taskList.populate(values);
            taskLists.add(taskList);
        }
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query task list by ID", e);
    }
    return taskLists.toArray(factory.newArray(taskLists.size()));
}