//MainActivity 里面做判断
private App app;
private DbManager db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 判断是否有主题存储
if(savedInstanceState != null){
theme = savedInstanceState.getInt("theme");
setTheme(theme);
}
x.view().inject(this);
//获取到数据库
app = (App) getApplication();
db = x.getDb(app.getDaoConfig());
inflater = getLayoutInflater();
initview();
try {
List<DataBean> list = db.findAll(DataBean.class);
if (list!=null&&list.size()>0){
mlist.addAll(list);
myadapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this,"数据库",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this,"网络",Toast.LENGTH_LONG).show();
loadData();
}
} catch (DbException e) {
e.printStackTrace();
}
}
//APP类里
public class App extends Application{
private DbManager.DaoConfig daoConfig;
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
//创建数据库
daoConfig = new DbManager.DaoConfig()
//数据库名字
.setDbName("yw.db")
//数据集的版本
.setDbVersion(1)
//是否开启事物
.setAllowTransaction(true)
//创建表的监听
.setTableCreateListener(new DbManager.TableCreateListener() {
@Override
public void onTableCreated(DbManager db, TableEntity<?> table) {
Toast.makeText(App.this, table.getName(), Toast.LENGTH_SHORT).show();
}
})
//数据库打开的监听
.setDbOpenListener(new DbManager.DbOpenListener() {
@Override
public void onDbOpened(DbManager db) {
//提高访问数据库的效率
db.getDatabase().enableWriteAheadLogging();
}
});
}
public DbManager.DaoConfig getDaoConfig() {
return daoConfig;
}
}