Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5291925

Browse files
committed
Bug fix for 4-1
1 parent 76c662c commit 5291925

File tree

27 files changed

+292
-131
lines changed

27 files changed

+292
-131
lines changed

AndroidTutorial4_1Update.md

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,81 +43,44 @@
4343
// 在「FILENAME_COLUMN」下方加入錄音檔案名稱欄位
4444
public static final String CREATE_TABLE =
4545
"CREATE TABLE " + TABLE_NAME + " (" +
46-
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
47-
DATETIME_COLUMN + " INTEGER NOT NULL, " +
48-
COLOR_COLUMN + " INTEGER NOT NULL, " +
49-
TITLE_COLUMN + " TEXT NOT NULL, " +
50-
CONTENT_COLUMN + " TEXT NOT NULL, " +
46+
...
5147
FILENAME_COLUMN + " TEXT, " +
5248
RECFILENAME_COLUMN + " TEXT, " + // 增加錄音檔案名稱
53-
LATITUDE_COLUMN + " REAL, " +
54-
LONGITUDE_COLUMN + " REAL, " +
55-
LASTMODIFY_COLUMN + " INTEGER, " +
56-
ALARMDATETIME_COLUMN + " INTEGER)";
49+
...";
5750

5851
4. 同樣在「ItemDAO.java」,修改「insert」方法:
5952

6053
public Item insert(Item item) {
6154
ContentValues cv = new ContentValues();
62-
63-
cv.put(DATETIME_COLUMN, item.getDatetime());
64-
cv.put(COLOR_COLUMN, item.getColor().parseColor());
65-
cv.put(TITLE_COLUMN, item.getTitle());
66-
cv.put(CONTENT_COLUMN, item.getContent());
55+
...
6756
cv.put(FILENAME_COLUMN, item.getFileName());
6857
// 錄音檔案名稱
6958
cv.put(RECFILENAME_COLUMN, item.getRecFileName());
70-
cv.put(LATITUDE_COLUMN, item.getLatitude());
71-
cv.put(LONGITUDE_COLUMN, item.getLongitude());
72-
cv.put(LASTMODIFY_COLUMN, item.getLastModify());
73-
cv.put(ALARMDATETIME_COLUMN, item.getAlarmDatetime());
74-
long id = db.insert(TABLE_NAME, null, cv);
75-
item.setId(id);
76-
77-
return item;
59+
...
7860
}
7961

8062
5. 同樣在「ItemDAO.java」,修改「update」方法:
8163

8264
public boolean update(Item item) {
8365
ContentValues cv = new ContentValues();
8466

85-
cv.put(DATETIME_COLUMN, item.getDatetime());
86-
cv.put(COLOR_COLUMN, item.getColor().parseColor());
87-
cv.put(TITLE_COLUMN, item.getTitle());
88-
cv.put(CONTENT_COLUMN, item.getContent());
67+
...
8968
cv.put(FILENAME_COLUMN, item.getFileName());
9069
// 錄音檔案名稱
9170
cv.put(RECFILENAME_COLUMN, item.getRecFileName());
92-
cv.put(LATITUDE_COLUMN, item.getLatitude());
93-
cv.put(LONGITUDE_COLUMN, item.getLongitude());
94-
cv.put(LASTMODIFY_COLUMN, item.getLastModify());
95-
cv.put(ALARMDATETIME_COLUMN, item.getAlarmDatetime());
96-
String where = KEY_ID + "=" + item.getId();
97-
98-
return db.update(TABLE_NAME, cv, where, null) > 0;
71+
...
9972
}
10073

10174
6. 同樣在「ItemDAO.java」,修改「getRecord」方法:
10275

10376
public Item getRecord(Cursor cursor) {
104-
Item result = new Item();
105-
106-
result.setId(cursor.getLong(0));
107-
result.setDatetime(cursor.getLong(1));
108-
result.setColor(ItemActivity.getColors(cursor.getInt(2)));
109-
result.setTitle(cursor.getString(3));
110-
result.setContent(cursor.getString(4));
77+
...
11178
result.setFileName(cursor.getString(5));
11279
// 錄音檔案名稱
11380
result.setRecFileName(cursor.getString(6));
114-
// 後續的編號都要修改
81+
// 後續的編號都要加一
11582
result.setLatitude(cursor.getDouble(7));
116-
result.setLongitude(cursor.getDouble(8));
117-
result.setLastModify(cursor.getLong(9));
118-
result.setAlarmDatetime(cursor.getLong(9));
119-
120-
return result;
83+
...
12184
}
12285

12386
7. 同樣在「ItemDAO.java」,修改「sample」方法:
@@ -129,16 +92,13 @@
12992
Item item3 = new Item(0, new Date().getTime(), Colors.GREEN, "一首非常好聽的音樂!", "Hello content", "", "", 0, 0, 0);
13093
Item item4 = new Item(0, new Date().getTime(), Colors.ORANGE, "儲存在資料庫的資料", "Hello content", "", "", 0, 0, 0);
13194

132-
insert(item);
133-
insert(item2);
134-
insert(item3);
135-
insert(item4);
95+
...
13696
}
13797

13898
8. 開啟「MyDBHelper.java」,增加資料庫版本編號:
13999

140100
// 資料庫版本,資料結構改變的時候要更改這個數字,通常是加一
141-
public static final int VERSION = 3;
101+
public static final int VERSION = 2;
142102

143103
9. 開啟「ItemActivity.java」,增加錄音檔案名稱欄位變數:
144104

@@ -186,4 +146,21 @@
186146

187147
}
188148

149+
12. 同樣在「ItemActivity.java」,找到「onActivityResult」方法,修改設定錄音檔案名稱呼叫的方法:
150+
151+
@Override
152+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
153+
if (resultCode == Activity.RESULT_OK) {
154+
switch (requestCode) {
155+
...
156+
case START_RECORD:
157+
// 修改設定錄音檔案名稱
158+
item.setRecFileName(recFileName);
159+
break;
160+
...
161+
}
162+
}
163+
}
164+
165+
189166
完成全部的修改以後執行應用程式,測試同一個記事資料照相與錄音的功能。

examples/0401/MyAndroidTutorial/MyAndroidTutorial.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="java-gradle" name="Java-Gradle">
55
<configuration>
66
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
77
</configuration>
88
</facet>
99
</component>
10-
<component name="NewModuleRootManager" inherit-compiler-output="false">
10+
<component name="NewModuleRootManager" inherit-compiler-output="true">
1111
<output url="file://$MODULE_DIR$/build/classes/main" />
1212
<output-test url="file://$MODULE_DIR$/build/classes/test" />
1313
<exclude-output />

examples/0401/MyAndroidTutorial/app/app.iml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="MyAndroidTutorial" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>
@@ -9,6 +9,7 @@
99
<facet type="android" name="Android">
1010
<configuration>
1111
<option name="SELECTED_BUILD_VARIANT" value="debug" />
12+
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
1213
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
1314
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
1415
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugTest" />
@@ -24,6 +25,7 @@
2425
</component>
2526
<component name="NewModuleRootManager" inherit-compiler-output="false">
2627
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
28+
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
2729
<exclude-output />
2830
<content url="file://$MODULE_DIR$">
2931
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />

examples/0401/MyAndroidTutorial/app/src/main/java/net/macdidi/myandroidtutorial/Item.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
public class Item implements java.io.Serializable {
77

8-
// 編號、日期時間、顏色、標題、內容、檔案名稱、經緯度、修改、已選擇
8+
// 編號、日期時間、顏色、標題、內容、檔案名稱、錄音檔案名稱、經緯度、修改、已選擇
99
private long id;
1010
private long datetime;
1111
private Colors color;
1212
private String title;
1313
private String content;
1414
private String fileName;
15+
private String recFileName;
1516
private double latitude;
1617
private double longitude;
1718
private long lastModify;
@@ -24,14 +25,15 @@ public Item() {
2425
}
2526

2627
public Item(long id, long datetime, Colors color, String title,
27-
String content, String fileName, double latitude, double longitude,
28-
long lastModify) {
28+
String content, String fileName, String recFileName,
29+
double latitude, double longitude, long lastModify) {
2930
this.id = id;
3031
this.datetime = datetime;
3132
this.color = color;
3233
this.title = title;
3334
this.content = content;
3435
this.fileName = fileName;
36+
this.recFileName = recFileName;
3537
this.latitude = latitude;
3638
this.longitude = longitude;
3739
this.lastModify = lastModify;
@@ -100,6 +102,14 @@ public void setFileName(String fileName) {
100102
this.fileName = fileName;
101103
}
102104

105+
public String getRecFileName() {
106+
return recFileName;
107+
}
108+
109+
public void setRecFileName(String recFileName) {
110+
this.recFileName = recFileName;
111+
}
112+
103113
public double getLatitude() {
104114
return latitude;
105115
}

examples/0401/MyAndroidTutorial/app/src/main/java/net/macdidi/myandroidtutorial/ItemActivity.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ItemActivity extends Activity {
3333

3434
// 檔案名稱
3535
private String fileName;
36+
private String recFileName;
3637

3738
// 照片
3839
private ImageView picture;
@@ -99,7 +100,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
99100
break;
100101
case START_RECORD:
101102
// 設定錄音檔案名稱
102-
item.setFileName(fileName);
103+
item.setRecFileName(recFileName);
103104
break;
104105
case START_LOCATION:
105106
break;
@@ -197,7 +198,7 @@ public void clickFunction(View view) {
197198
break;
198199
case R.id.record_sound:
199200
// 錄音檔案名稱
200-
final File recordFile = configFileName("R", ".mp3");
201+
final File recordFile = configRecFileName("R", ".mp3");
201202

202203
// 如果已經有錄音檔,詢問播放或重新錄製
203204
if (recordFile.exists()) {
@@ -268,4 +269,18 @@ private File configFileName(String prefix, String extension) {
268269
prefix + fileName + extension);
269270
}
270271

272+
private File configRecFileName(String prefix, String extension) {
273+
// 如果記事資料已經有檔案名稱
274+
if (item.getRecFileName() != null && item.getRecFileName().length() > 0) {
275+
recFileName = item.getRecFileName();
276+
}
277+
// 產生檔案名稱
278+
else {
279+
recFileName = FileUtil.getUniqueFileName();
280+
}
281+
282+
return new File(FileUtil.getExternalStorageDir(FileUtil.APP_DIR),
283+
prefix + recFileName + extension);
284+
}
285+
271286
}

examples/0401/MyAndroidTutorial/app/src/main/java/net/macdidi/myandroidtutorial/ItemDAO.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ItemDAO {
2323
public static final String TITLE_COLUMN = "title";
2424
public static final String CONTENT_COLUMN = "content";
2525
public static final String FILENAME_COLUMN = "filename";
26+
public static final String RECFILENAME_COLUMN = "recfilename";
2627
public static final String LATITUDE_COLUMN = "latitude";
2728
public static final String LONGITUDE_COLUMN = "longitude";
2829
public static final String LASTMODIFY_COLUMN = "lastmodify";
@@ -36,6 +37,7 @@ public class ItemDAO {
3637
TITLE_COLUMN + " TEXT NOT NULL, " +
3738
CONTENT_COLUMN + " TEXT NOT NULL, " +
3839
FILENAME_COLUMN + " TEXT, " +
40+
RECFILENAME_COLUMN + " TEXT, " +
3941
LATITUDE_COLUMN + " REAL, " +
4042
LONGITUDE_COLUMN + " REAL, " +
4143
LASTMODIFY_COLUMN + " INTEGER)";
@@ -65,6 +67,7 @@ public Item insert(Item item) {
6567
cv.put(TITLE_COLUMN, item.getTitle());
6668
cv.put(CONTENT_COLUMN, item.getContent());
6769
cv.put(FILENAME_COLUMN, item.getFileName());
70+
cv.put(RECFILENAME_COLUMN, item.getRecFileName());
6871
cv.put(LATITUDE_COLUMN, item.getLatitude());
6972
cv.put(LONGITUDE_COLUMN, item.getLongitude());
7073
cv.put(LASTMODIFY_COLUMN, item.getLastModify());
@@ -93,6 +96,7 @@ public boolean update(Item item) {
9396
cv.put(TITLE_COLUMN, item.getTitle());
9497
cv.put(CONTENT_COLUMN, item.getContent());
9598
cv.put(FILENAME_COLUMN, item.getFileName());
99+
cv.put(RECFILENAME_COLUMN, item.getRecFileName());
96100
cv.put(LATITUDE_COLUMN, item.getLatitude());
97101
cv.put(LONGITUDE_COLUMN, item.getLongitude());
98102
cv.put(LASTMODIFY_COLUMN, item.getLastModify());
@@ -160,9 +164,10 @@ public Item getRecord(Cursor cursor) {
160164
result.setTitle(cursor.getString(3));
161165
result.setContent(cursor.getString(4));
162166
result.setFileName(cursor.getString(5));
163-
result.setLatitude(cursor.getDouble(6));
164-
result.setLongitude(cursor.getDouble(7));
165-
result.setLastModify(cursor.getLong(8));
167+
result.setRecFileName(cursor.getString(6));
168+
result.setLatitude(cursor.getDouble(7));
169+
result.setLongitude(cursor.getDouble(8));
170+
result.setLastModify(cursor.getLong(9));
166171

167172
// 回傳結果
168173
return result;
@@ -182,10 +187,10 @@ public int getCount() {
182187

183188
// 建立範例資料
184189
public void sample() {
185-
Item item = new Item(0, new Date().getTime(), Colors.RED, "關於Android Tutorial的事情.", "Hello content", "", 0, 0, 0);
186-
Item item2 = new Item(0, new Date().getTime(), Colors.BLUE, "一隻非常可愛的小狗狗!", "她的名字叫「大熱狗」,又叫\n作「奶嘴」,是一隻非常可愛\n的小狗。", "", 25.04719, 121.516981, 0);
187-
Item item3 = new Item(0, new Date().getTime(), Colors.GREEN, "一首非常好聽的音樂!", "Hello content", "", 0, 0, 0);
188-
Item item4 = new Item(0, new Date().getTime(), Colors.ORANGE, "儲存在資料庫的資料", "Hello content", "", 0, 0, 0);
190+
Item item = new Item(0, new Date().getTime(), Colors.RED, "關於Android Tutorial的事情.", "Hello content", "", "", 0, 0, 0);
191+
Item item2 = new Item(0, new Date().getTime(), Colors.BLUE, "一隻非常可愛的小狗狗!", "她的名字叫「大熱狗」,又叫\n作「奶嘴」,是一隻非常可愛\n的小狗。", "", "", 25.04719, 121.516981, 0);
192+
Item item3 = new Item(0, new Date().getTime(), Colors.GREEN, "一首非常好聽的音樂!", "Hello content", "", "", 0, 0, 0);
193+
Item item4 = new Item(0, new Date().getTime(), Colors.ORANGE, "儲存在資料庫的資料", "Hello content", "", "", 0, 0, 0);
189194

190195
insert(item);
191196
insert(item2);

examples/0401/MyAndroidTutorial/app/src/main/java/net/macdidi/myandroidtutorial/MyDBHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class MyDBHelper extends SQLiteOpenHelper {
1010
// 資料庫名稱
1111
public static final String DATABASE_NAME = "mydata.db";
1212
// 資料庫版本,資料結構改變的時候要更改這個數字,通常是加一
13-
public static final int VERSION = 1;
13+
public static final int VERSION = 2;
1414
// 資料庫物件,固定的欄位變數
1515
private static SQLiteDatabase database;
1616

examples/0402/MyAndroidTutorial/app/src/main/java/net/macdidi/myandroidtutorial/Item.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Item implements java.io.Serializable {
1212
private String title;
1313
private String content;
1414
private String fileName;
15+
private String recFileName;
1516
private double latitude;
1617
private double longitude;
1718
private long lastModify;
@@ -24,14 +25,16 @@ public Item() {
2425
}
2526

2627
public Item(long id, long datetime, Colors color, String title,
27-
String content, String fileName, double latitude, double longitude,
28-
long lastModify) {
28+
String content, String fileName, String recFileName,
29+
double latitude, double longitude, long lastModify) {
2930
this.id = id;
3031
this.datetime = datetime;
3132
this.color = color;
3233
this.title = title;
3334
this.content = content;
3435
this.fileName = fileName;
36+
// 錄音檔案名稱
37+
this.recFileName = recFileName;
3538
this.latitude = latitude;
3639
this.longitude = longitude;
3740
this.lastModify = lastModify;
@@ -100,6 +103,14 @@ public void setFileName(String fileName) {
100103
this.fileName = fileName;
101104
}
102105

106+
public String getRecFileName() {
107+
return recFileName;
108+
}
109+
110+
public void setRecFileName(String recFileName) {
111+
this.recFileName = recFileName;
112+
}
113+
103114
public double getLatitude() {
104115
return latitude;
105116
}

0 commit comments

Comments
 (0)