Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import android.database.Cursor;

import android.text.TextUtils;
import android.util.Log;

public class Main {
    /**
     * Print the content of the cursor
     *
     * @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();
    }

    private static String appendColumnSpaces(String value, int columnWidth) {
        StringBuilder builder = new StringBuilder();
        int spaceCount;
        if (value == null) {
            builder.append("null");
            spaceCount = columnWidth - 4;
        } else {
            builder.append(value);
            spaceCount = columnWidth - value.length();
        }
        for (int i = 0; i < spaceCount; i++) {
            builder.append(" ");
        }
        return builder.toString();
    }
}