I am using db4o lib for storing data.
for example I have this code for storing data (ie News)
public static void insertNewsToDB(Context context, final News news){
final String dbPath = context.getDir("news", Context.MODE_PRIVATE) + "/" + DB4O_NAME;
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
try {
db.store(news);
db.commit();
} finally {
db.close();
}
}
and this getting data from db:
public static List<News> getNewsListFromDB(Context context){
final List<News> news = new ArrayList<News>();
final String dbPath = context.getDir("news", Context.MODE_PRIVATE) + "/" + DB4O_NAME;
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
try {
ObjectSet<News> result = db.queryByExample(News.class);
news.addAll(result);
} finally {
db.close();
}
return news;
}
How could I create method so it's not depend on what type of class I have to pass. More clearly it doesn't depend on second parameter is News,User or something else. And same when I want get news from db.
I need to create general method which is not depend on type of class.
EDIT: I did this but I doesn't work. More clearly trying get list it returns nothing. And I don't get it why what I did wrong
public static void insertToDB(Context context, final Object obj,final String _key) {
final String dbPath = context.getDir(_key, Context.MODE_PRIVATE) + "/" + DB4O_NAME;
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
try {
db.store(obj);
db.commit();
} finally {
db.close();
}
}
public static <T> void insertToDB(Context context, final List<T> newsList,String _key) {
for (T news : newsList) {
insertToDB(context, news,_key);
}
}
public static <T> List<T> getListFromDB(Context context, final java.lang.Class<T> tClass,final String _key) {
final List<T> news = new ArrayList<T>();
final String dbPath = context.getDir(_key, Context.MODE_PRIVATE) + "/" + DB4O_NAME;
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
try {
ObjectSet<T> result = db.queryByExample(tClass);
news.addAll(result);
} finally {
db.close();
}
return news;
}
使用Object作为方法参数类型或者您可以根据需要使用泛型(?或T)
If you need no type information at all, you could simple switch News
to Object
. If you need type information (ie, persisted objects stick to a certain interface), introduce a generic type parameter.
public static <T extends SomeClass> void insertEntityToDB(Context context, final T entity) {
// ...
}
public static <T extends SomeClass> List<T> getEntityListFromDB(Context context) {
// ...
}
You need to implement the concept of Generics in your method calls.
public static void insertNewsToDB(Context context, final News news)
will change to
public static void insertNewsToDB(Context context, final <T> T)
And
public static List<News> getNewsListFromDB(Context context)
will change to
public static List<T> getNewsListFromDB(Context context)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.