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 e036c77 commit 8d72a27
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -94,15 +94,15 @@ public static long addUser(Context context, User user) {
}

public static @Nullable
List<User> getUsers(Context context) {
List<User> getUsers(Context context, Class type) {
List<User> users = null;

DataBaseHelper helper = new DataBaseHelper(context);
SQLiteDatabase db = null;
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()) {

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
Expand All @@ -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<User> users = DataBaseHelper.getUsers(this);
List<User> users = DataBaseHelper.getUsers(this, User.class);
if (users != null) {

Gson g = new GsonBuilder().setPrettyPrinting().create();
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/github/shinchven/androiddbkits/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.shinchven.androiddbkits;

import android.net.Uri;

import java.io.File;
import java.util.Date;

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
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 @@ -91,6 +92,10 @@ public static <T> List<T> read(Cursor cursor, Class<T> 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");
}
Expand Down

0 comments on commit 8d72a27

Please sign in to comment.