Skip to content

Commit

Permalink
Reflections for type boolean, Uri, File.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinChven committed Jun 19, 2017
1 parent 5709f4d commit e036c77
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:design:25.1.1'
testCompile 'junit:junit:4.12'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.sql.Date;
import java.util.List;
Expand Down Expand Up @@ -37,10 +39,18 @@ protected void onCreate(Bundle savedInstanceState) {
DataBaseHelper.addUser(this, user);
}


List<User> users = DataBaseHelper.getUsers(this);
if (users != null) {
for (User user : users) {
Log.i("user", user.toString());

Gson g = new GsonBuilder().setPrettyPrinting().create();
TextView txt = (TextView) findViewById(R.id.data);
try {
String data = g.toJson(users);
txt.setText(data);
} catch (Exception e) {
e.printStackTrace();
txt.setText("data error");
}
}

Expand Down
35 changes: 18 additions & 17 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.github.shinchven.androiddbkits.MainActivity"
tools:showIn="@layout/activity_main">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.github.shinchven.androiddbkits.MainActivity"
tools:showIn="@layout/activity_main">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/data"
android:text="Hello World!"/>
</ScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
Expand Down Expand Up @@ -44,13 +45,17 @@ public static ContentValues objectToContentValues(Object o, String... ignoredFie
Object value = field.get(o);
if (value != null) {
//This part just makes sure the content values can handle the field
if (value instanceof Double || value instanceof Integer || value instanceof String || value instanceof Boolean
if (value instanceof Double || value instanceof Integer || value instanceof String
|| value instanceof Long || value instanceof Float || value instanceof Short) {
values.put(field.getName(), value.toString());
} else if (value instanceof Boolean) {
values.put(field.getName(), ((Boolean) value) ? 1 : 0);
} else if (value instanceof Date) {
values.put(field.getName(), ((Date) value).getTime());
} else if (value instanceof Uri) {
values.put(field.getName(), ((Uri) value).toString());
} else if (value instanceof File) {
values.put(field.getName(), ((File) value).getAbsolutePath());
} else
Log.w(LOG_TAG, "value could not be handled by field: " + field.getName() + ":" + field.getType().getName());
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public static <T> List<T> read(Cursor cursor, Class<T> type, String... ignoredFi
} else if (field.getType() == Float.class || field.getType().getName().equals("float")) {
float data = cursor.getFloat(columnIndex);
field.setFloat(obj, data);
} else if (field.getType() == Short.class || field.getType().getName().equals("short")) {
short data = cursor.getShort(columnIndex);
field.setShort(obj, data);
} else if (field.getType() == Long.class || field.getType().getName().equals("long")) {
long data = cursor.getLong(columnIndex);
field.setLong(obj, data);
Expand Down Expand Up @@ -111,5 +114,4 @@ public static <T> List<T> read(Cursor cursor, Class<T> type, String... ignoredFi
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand Down Expand Up @@ -47,10 +48,10 @@ public static void createTable(Class type, SQLiteDatabase db, String... ignoredF
} else if (field.getType() == String.class) {
sb.append("TEXT");
} else if (field.getType() == Boolean.class || field.getType().getName().equals("boolean")) {
sb.append("boolean");
sb.append("INTEGER");
} else if (field.getType() == Date.class) {
sb.append("LONG");
} else if (field.getType() == Uri.class) {
} else if (field.getType() == Uri.class || field.getType() == File.class) {
sb.append("TEXT");
} else {
Log.i("creating table", "field: " + field.getName() + " not filled");
Expand Down Expand Up @@ -112,7 +113,7 @@ public static void updateTable(Class type, SQLiteDatabase db, String... ignoredF
sb.append(" INTEGER");
} else if (field.getType() == Date.class) {
sb.append(" LONG");
} else if (field.getType() == Uri.class) {
} else if (field.getType() == Uri.class || field.getType() == File.class) {
sb.append(" TEXT");
} else {
Log.i("creating table", "field: " + field.getName() + " not updated");
Expand All @@ -127,7 +128,7 @@ public static void updateTable(Class type, SQLiteDatabase db, String... ignoredF
for (int i = 0; i < columnsSQL.size(); i++) {
try {
db.execSQL(columnsSQL.get(i));
Log.i("sql","table update completed: "+columnsSQL.get(i).toLowerCase());
Log.i("sql", "table update completed: " + columnsSQL.get(i).toLowerCase());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down

0 comments on commit e036c77

Please sign in to comment.