diff --git a/app/src/main/java/com/github/shinchven/androiddbkits/DataBaseHelper.java b/app/src/main/java/com/github/shinchven/androiddbkits/DataBaseHelper.java index 6de143f..431edb5 100644 --- a/app/src/main/java/com/github/shinchven/androiddbkits/DataBaseHelper.java +++ b/app/src/main/java/com/github/shinchven/androiddbkits/DataBaseHelper.java @@ -24,7 +24,7 @@ public class DataBaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "db.sqlite"; - private static final int DB_VERSION = 8; + private static final int DB_VERSION = 12; public DataBaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); @@ -77,7 +77,7 @@ public static long addUser(Context context, User user) { long result = 0; try { - result = db.insert("user", "_id", values); + result = db.insert(user.getClass().getSimpleName(), "_id", values); } catch (Exception e) { e.printStackTrace(); @@ -94,7 +94,7 @@ public static long addUser(Context context, User user) { } public static @Nullable - List getUsers(Context context) { + List getUsers(Context context, Class type) { List users = null; DataBaseHelper helper = new DataBaseHelper(context); @@ -102,7 +102,7 @@ List getUsers(Context context) { Cursor cursor = null; try { db = helper.getReadableDatabase(); - cursor = db.query("user", null, null, null, null, null, null); + cursor = db.query(type.getSimpleName(), null, null, null, null, null, null); users = CursorReader.read(cursor, User.class, new String[]{}); while (cursor.moveToNext()) { diff --git a/app/src/main/java/com/github/shinchven/androiddbkits/MainActivity.java b/app/src/main/java/com/github/shinchven/androiddbkits/MainActivity.java index b42bc88..bfedd78 100644 --- a/app/src/main/java/com/github/shinchven/androiddbkits/MainActivity.java +++ b/app/src/main/java/com/github/shinchven/androiddbkits/MainActivity.java @@ -1,6 +1,8 @@ package com.github.shinchven.androiddbkits; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; @@ -26,7 +28,7 @@ protected void onCreate(Bundle savedInstanceState) { setSupportActionBar(toolbar); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 2; i++) { User user = new User(); user.setAge(15); user.setBirthday(new Date(System.currentTimeMillis())); @@ -36,11 +38,14 @@ protected void onCreate(Bundle savedInstanceState) { user.setdValue(new Random().nextDouble()); user.setfValue(new Random().nextFloat()); user.setlValue(System.currentTimeMillis()); + user.setLastName("hello"); + user.setAvatar(getExternalFilesDir(Environment.DIRECTORY_DCIM)); + user.setContentUri(Uri.parse("content://com.github.shinchven.db/2/3")); DataBaseHelper.addUser(this, user); } - List users = DataBaseHelper.getUsers(this); + List users = DataBaseHelper.getUsers(this, User.class); if (users != null) { Gson g = new GsonBuilder().setPrettyPrinting().create(); diff --git a/app/src/main/java/com/github/shinchven/androiddbkits/User.java b/app/src/main/java/com/github/shinchven/androiddbkits/User.java index 09d295c..4c993b5 100644 --- a/app/src/main/java/com/github/shinchven/androiddbkits/User.java +++ b/app/src/main/java/com/github/shinchven/androiddbkits/User.java @@ -1,5 +1,8 @@ package com.github.shinchven.androiddbkits; +import android.net.Uri; + +import java.io.File; import java.util.Date; /** @@ -16,6 +19,9 @@ public class User { private float fValue; private double dValue; private long lValue; + private File avatar; + private String lastName; + private Uri contentUri; public int get_id() { return _id; @@ -88,4 +94,28 @@ public long getlValue() { public void setlValue(long lValue) { this.lValue = lValue; } + + public File getAvatar() { + return avatar; + } + + public void setAvatar(File avatar) { + this.avatar = avatar; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Uri getContentUri() { + return contentUri; + } + + public void setContentUri(Uri contentUri) { + this.contentUri = contentUri; + } } diff --git a/dbkits/src/main/java/com/github/shinchven/dbkits/CursorReader.java b/dbkits/src/main/java/com/github/shinchven/dbkits/CursorReader.java index 1487489..ccdb328 100644 --- a/dbkits/src/main/java/com/github/shinchven/dbkits/CursorReader.java +++ b/dbkits/src/main/java/com/github/shinchven/dbkits/CursorReader.java @@ -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; @@ -91,6 +92,10 @@ public static List read(Cursor cursor, Class type, String... ignoredFi String uriString = cursor.getString(columnIndex); Uri uri = Uri.parse(uriString); field.set(obj, uri); + } else if (field.getType() == File.class) { + String filePath = cursor.getString(columnIndex); + File file = new File(filePath); + field.set(obj, file); } else { Log.i(TAG, "field: " + field.getName() + " not filled"); }