編輯:關於Android編程
第一步是配置:
在gradle下配置
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.example.myapplication" minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { /**********配置*********/ sourceSets{ main{ java.srcDirs =['src/main/java','src/main/java-gen'] } /*******************/ } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' /********配置***********/ compile 'de.greenrobot:greendao:2.1.0' compile 'de.greenrobot:greendao-generator:2.1.0' /*******************/ }
第二步創建一個在main目錄下java-gen
第三步創建一個java項目
如下配置下build
package com.example; import de.greenrobot.daogenerator.DaoGenerator; import de.greenrobot.daogenerator.Entity; import de.greenrobot.daogenerator.Schema; public class DaoMaker { public static void main(String[] args) { int version = 1; String defaultPackage = "com.student.entity"; String pathDao="D:\\AndroidStudioProjects\\ApplicationTrunk\\mediamyself\\src\\main\\java-gen"; //生成數據庫的實體類XXentity 對應的是數據庫的表 Schema schema = new Schema(version, defaultPackage); addStudent(schema); schema.setDefaultJavaPackageDao("com.student.dao"); try { new DaoGenerator().generateAll(schema,pathDao); } catch (Exception e){ e.printStackTrace(); } } //創建數據庫的表 private static void addStudent(Schema schema) { Entity entity = schema.addEntity("Student"); entity.addIdProperty();//主鍵是int 類型 entity.addStringProperty("name");//對應的數據庫的列 entity.addStringProperty("address");//對應數據庫的列 entity.addIntProperty("age");//對應數據庫的列 } }
運行下,右鍵,運行之後就會出現在java-gen文件夾下多出一些文件
文件如圖自動生成
在項目中操作數據庫~
package com.example.myapplication.dbmanager; import android.content.Context; import com.student.dao.DaoMaster; import com.student.dao.DaoSession; import de.greenrobot.dao.query.QueryBuilder; /** * 1.創建數據庫 * 2.創建數據庫的表 * 3.包含對數據庫的CRUD * 4.對數據庫的升級 * Created by Administrator on 2016/8/16. */ public class DaoManager { private static final String TAG = DaoManager.class.getSimpleName(); private static final String DB_NAME = "mydb.sqlite";//數據庫名稱 private volatile static DaoManager manager;//多線程訪問 private static DaoMaster.DevOpenHelper helper; private static DaoMaster daoMaster; private DaoSession daoSession; private Context context; /** * 使用單列模式獲得操作數據庫的對象 */ public static DaoManager getInstance(){ DaoManager instance = null; if(manager == null){ synchronized (DaoManager.class) { if (instance == null) { instance = new DaoManager(); manager = instance; } } } return instance; } public void init(Context context) { this.context = context; } /** * 判斷是否存在數據庫,如果沒有數據庫則創建數據庫 * */ public DaoMaster getDaoMaster(){ if (daoMaster == null) { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null); daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; } /** * 完成對數據庫的添加,刪除,修改,查詢的操作,僅僅是一個接口 * */ public DaoSession getDaoSession(){ if (daoSession == null) { if (daoMaster==null){ daoMaster = getDaoMaster(); } daoSession = daoMaster.newSession(); } return daoSession; } /** * 打開輸出日志的操作,默認是關閉的 */ public void setDebug(){ QueryBuilder.LOG_SQL = true; QueryBuilder.LOG_VALUES = true; } /** * 關閉所有的操作,數據庫開啟的時候,使用完必須要關閉 * */ public void closeConnection(){ closeHelper(); closeDaoSession(); } private void closeDaoSession() { if (daoSession != null) { daoSession.clear(); daoSession = null; } } private void closeHelper() { if(helper!=null){ helper.close(); helper = null; } } }
package com.example.myapplication.dbmanager; import android.content.Context; import android.util.Log; import com.student.dao.StudentDao; import com.student.entity.Student; import java.util.List; import de.greenrobot.dao.query.QueryBuilder; /** * 完成對某一張表的具體操作,ORM操作的對象,Student * Created by Administrator on 2016/8/16. */ public class CommonUtils { private static final String TAG = CommonUtils.class.getSimpleName(); private DaoManager manager; public CommonUtils(Context context){ manager = DaoManager.getInstance(); manager.init(context); } /** * 完成對數據庫中student表的插入 操作 */ public boolean insetStudent(Student student){ boolean flag = false; flag = manager.getDaoSession().insert(student) != -1 ? true : false; Log.i(TAG, "insetStudent: "+flag); return flag; } /** * 插入多條記錄,需要開辟新的線程 * */ public boolean insetMultStudent(final Liststudents){ boolean flag = false; try { manager.getDaoSession().runInTx(new Runnable() { @Override public void run() { for (Student s : students) { manager.getDaoSession().insertOrReplace(s); } } }); flag = true; }catch (Exception e){ } return flag; } /** * 完成對student的某一條記錄的修改 * */ public boolean updateStudent(Student student){ boolean flag = false; try{ manager.getDaoSession().update(student); }catch (Exception e){ e.printStackTrace(); } return flag; } /** * 刪除數據 */ public boolean deleteStudent(Student s){ boolean flag = false; try{ //按照指定的id 進行刪除 delete from student where id = ? manager.getDaoSession().delete(s); //managere.getDaosession().deleteAll();//刪除所有記錄 }catch (Exception e){ e.printStackTrace(); } return flag; } /** * 返回多行記錄 * */ public List listAll(){ return manager.getDaoSession().loadAll(Student.class); } /** * 按照主鍵返回單行記錄 * */ public Student listOneStudent(long key){ return manager.getDaoSession().load(Student.class, key); } public void query1(){ //使用native sql 進行查詢操作 List list = manager.getDaoSession().queryRaw(Student.class, "where like ? and _id > ?", new String[]{"%李%", "1002"}); Log.i(TAG, "query1: "+ list); } /** * select * from student where name like ? or name =? or * < <= != in between and * select *from student where age > 23 and address like "江西" * */ public void query2(){ //查詢構建器 QueryBuilder builder = manager.getDaoSession().queryBuilder(Student.class); List list = builder.where(StudentDao.Properties.Age.ge(23)).where(StudentDao.Properties.Address.like("江西")).list(); Log.i(TAG, "query2: " + list); } public void query3(){ //邏輯與 和 邏輯或 是雙目運算符 QueryBuilder builder = manager.getDaoSession().queryBuilder(Student.class); //select * from student where (address = '北京' or age > 50 and name like '%張%') builder.whereOr(StudentDao.Properties.Address.eq("北京"), StudentDao.Properties.Age.eq(50)); builder.whereOr(StudentDao.Properties.Id.ge(2), StudentDao.Properties.Age.ge(10)).limit(3);//取前三條數據 List list = builder.list(); Log.i(TAG, "query3: "+ list); } }
!學習自菜鳥教程-移動端-Android圖片如下 一、底部導航欄實現1、TextView 圖片和文字的變換,在drawable 中新建文件圖片:tav_re
Android LaunchMode詳解越是做的時間越長,基礎知識就忘的越干淨,最近做一個項目中,發現啟動的幾個Activity居然重疊了,我ri~~,再不回憶一下就要退
近來回顧了一下關於Activity的生命周期,參看了相關書籍和官方文檔,也有了不小的收獲,對於以前的認知有了很大程度上的改善,在這裡和大家分享一下。熟悉javaEE的朋友
在activity中有時需要嵌套調用fragment,但嵌套調用往往帶來視圖的顯示與預期的不一樣或是fragment的切換有問題。在使用時要注意幾點: 1、fragm