Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import android.content.ContentValues;

public class Main {

    public static ContentValues objectToContentValues(Object object) {
        if (object == null) {
            throw new IllegalArgumentException("please check your argument");
        }
        ContentValues contentValues = new ContentValues();
        try {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                String fieldName = field.getName();
                Type type = field.getType();
                field.setAccessible(true);
                if (type.equals(String.class)) {
                    contentValues.put(fieldName, field.get(object).toString());
                } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
                    contentValues.put(fieldName, field.getInt(object));
                } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
                    contentValues.put(fieldName, field.getFloat(object));
                } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
                    contentValues.put(fieldName, field.getLong(object));
                } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
                    contentValues.put(fieldName, field.getDouble(object));
                } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
                    contentValues.put(fieldName, field.getBoolean(object));
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException e) {
            e.printStackTrace();
        }
        return contentValues;
    }
}