(c)helejava
在個人上篇博客裏,工程構建仍是Eclipse模式,可是這篇博客寫出來時工程已經改成Android Studio模式,主要是Gradle工具的應用,特別是第三方庫的引用,不用將庫放進libs文件夾下,只須要進行Gradle依賴配置,十分方便。android
加入了選考科目數據庫。選考科目只有優秀、良好、及格、不及格的相關評級,不用像必考科目那樣經過內插算出分數。首先是經過選考科目界面進入查詢、添加、修改、刪除相關邏輯層操做,而後是邏輯層轉化爲對數據層的操做,再次是數據庫表的字段的選擇以及數據庫的建立,最後是數據庫在首次安裝的覆蓋寫入。git
開發工具沒有變,依然是AIDE,只是我把文件結構稍作調整。如圖:是工程根目錄,
是app目錄,
是src目錄,也就是個人工程的主要部分。其中java目錄裝的是各類*.java文件,res裝和是layout、menu、string、style等文件。工程根目錄裏的build.gradle文件以及settings.gradle文件則能夠重新建立的gradle工程直接拷貝。 須要着重強調的是app目錄裏的build.gradle文件的書寫:sql
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.2.0" defaultConfig { applicationId "mtn.plaust.pe" minSdkVersion 14 targetSdkVersion 22 versionCode 5 versionName "3.0.1" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:support-v4:22.2.0' compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' }
主要是dependencies的書寫,這裏引用了三個庫。數據庫
新建了一個PEStandard類做爲數據模型:android-studio
public class PEStandard implements Serializable { private String name, other; private int positive, ageCode, type, male; private Double[] standard = new Double[3]; private final static String[] STANDESC = {"優秀","良好","及格","不及格"}; public PEStandard(String name, int ageCode, int type, int male, double p1, double p2, double p3, String other) { this.name = name; //名稱 this.ageCode = ageCode; //年齡編碼 this.type = type; //類型 this.male = male; //1表明男性,0表明女性 standard[0] = p1; //優秀 standard[1] = p2; //良好 standard[2] = p3; //及格 this.other = other; //備註,用於輸入的記錄 if (p1 > p2) { //標記正序 positive = 1; } else { positive = -1; } } ... ... /*給定成績p,得出評定*/ public String judge(double p) { if (p == 0) return ""; int i; for (i = 0; i < 3 && positive * p < positive * standard[i]; i++) {} return STANDESC[i]; } }
三個文件:CustomActivity.java、CustomAddActivity.java、CustomEditActivity.java,不須要贅述。數據結構
public class PEStandardSqliteHelper extends SQLiteOpenHelper { private final static String TBNAME = "t_pestandard"; //固定數據表名稱 private static SQLiteDatabase pesDB = null; private PEStandardSqliteHelper(Context context) { //打開數據庫pe.db(版本號1) super(context,"pe.db",null,1); Log.i("數據庫操做","打開"); } public static SQLiteDatabase getInstance(Context context) { if (pesDB == null || !pesDB.isOpen()) { //懶漢模式 pesDB = new PEStandardSqliteHelper(context).getReadableDatabase(); } return pesDB; } @Override public void onCreate(SQLiteDatabase db) { //首次使用則自動調用此函數建立數據表 String sql = "create table " + TBNAME + " (name text not null, ageCode integer not null, type integer not null, male integer not null, st1 real not null, st2 real not null, st3 real not null, other text)"; db.execSQL(sql); Log.i("數據庫操做", "建立表"); } public static List query(int ageCode, int type, int male) { Log.i("數據庫操做", "查詢"); //TODO:數據庫查詢 List list = new ArrayList<PEStandard>(); Cursor c = pesDB.rawQuery("select * from " + TBNAME +" where ageCode=? and type=? and male=?" ,new String[] {String.valueOf(ageCode), String.valueOf(type), String.valueOf(male)}); c.moveToFirst(); while (!c.isAfterLast()) { list.add(new PEStandard(c.getString(0),c.getInt(1),c.getInt(2),c.getInt(3),c.getDouble(4),c.getDouble(5),c.getDouble(6),c.getString(7))); c.moveToNext(); } c.close(); return list; } public static boolean save(PEStandard p) { //TODO:數據庫存儲 Log.i("數據庫操做","save " + p.getName()); ContentValues values = new ContentValues(); values.put("name",p.getName()); values.put("ageCode",p.getAgeCode()); values.put("type",p.getType()); values.put("male",p.getMale()); values.put("st1",p.getStandard()[0]); values.put("st2",p.getStandard()[1]); values.put("st3",p.getStandard()[2]); values.put("other",p.getOther()); if (pesDB.insert(TBNAME,null,values) == -1) return false; return true; } public static boolean edit(PEStandard p) { //TODO:數據庫修改 Log.i("數據庫操做","edit " + p.getName()); ContentValues values = new ContentValues(); values.put("st1",p.getStandard()[0]); //獲取優秀標準 values.put("st2",p.getStandard()[1]); //獲取良好標準 values.put("st3",p.getStandard()[2]); //獲取及格標準 values.put("other",p.getOther()); //備註 if (pesDB.update(TBNAME,values,"name=? and ageCode=? and type=? and male=?", new String[]{p.getName(),String.valueOf(p.getAgeCode()),String.valueOf(p.getType()),String.valueOf(p.getMale())}) > 0) return true; return false; } public static boolean delete(String name, int ageCode, int type, int male) { //TODO:數據庫刪除 Log.i("數據庫操做","delete " + name); if (pesDB.delete(TBNAME,"name=? and ageCode=? and type=? and male=?",new String[]{name,String.valueOf(ageCode),String.valueOf(type),String.valueOf(male)}) > 0) return true; return false; } @Override protected void finalize() throws Throwable { pesDB.close(); Log.i("數據庫操做","關閉"); super.finalize(); } }
public class DBManager { private static final int BUFFER_SIZE = 1024; public static void initial(Context context) { try { BufferedInputStream bufferIn = new BufferedInputStream(context.getResources().openRawResource(R.raw.pe_db)); //從提早放入raw文件夾的pe_db讀取數據庫 byte[] temp = new byte[BUFFER_SIZE]; File file = new File(context.getCacheDir().getParentFile().getAbsolutePath()+"/databases"); if (!file.exists()) file.mkdir(); file = new File(file.getAbsolutePath()+"/pe.db"); BufferedOutputStream bufferOut = new BufferedOutputStream(new FileOutputStream(file)); int c = 0; while ((c = bufferIn.read(temp)) > 0) { bufferOut.write(temp,0,c); } bufferIn.close(); bufferOut.close(); Log.i("數據庫操做","初始化"); } catch (FileNotFoundException ex) { Log.e("FileOperation",ex.toString()); } catch (IOException ex) { Log.e("FileOperation",ex.toString()); } } }
至於如何保證首次安裝調用該類,其實跟首次登錄提示幫助同樣,要利用SharedPreference類。app
<!--app_bar.xml--> <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:fitsSystemWindows="true"> <android.support.v7.widget.Toolbar android:id="@+id/tb" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentScrim="?attr/colorPrimary" app:title="@string/app_name"/> </android.support.design.widget.AppBarLayout>
<!--main.xml--> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" android:id="@+id/draw_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!--內容區--> <android.support.design.widget.CoordinatorLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/app_bar" layout="@layout/app_bar"/> ... ...
<!--main.xml--> ... ... <!--左側導航欄--> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/navigation_header" app:menu="@menu/options"/> ... ...
<!--options.xml--> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/xinbing" android:icon="@android:drawable/ic_menu_myplaces" android:title="@string/xinbing"/> <item android:id="@+id/navigation_about" android:icon="@android:drawable/ic_menu_info_details" android:title="@string/about"/> <item android:id="@+id/navigation_help" android:icon="@android:drawable/ic_menu_help" android:title="@string/help"/> <item android:id="@+id/navigation_exit" android:icon="@android:drawable/ic_menu_set_as" android:title="@string/exit"/> </group> </menu>
而後是在main.xml裏添加相應的事件響應代碼。eclipse
[core] repositoryformatversion = 0 filemode = false logallrefupdates = true autocrlf = false [branch "master"] remote = master merge = refs/heads/master [remote "master"] url = git@git.oschina.net:hele_two/PE fetch = refs/heads/master [remote "origin"] url = git@git.oschina.net:hele_two/PE