teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

回答を置き換え

2019/12/11 11:22

投稿

jimbe
jimbe

スコア13357

answer CHANGED
@@ -1,18 +1,99 @@
1
- > uuidの非表示の仕方
1
+ ~~> uuidの非表示の仕方~~
2
2
 
3
+ ~~data.put("id",uuid);~~
4
+
5
+ ~~の下に~~
6
+
7
+ ~~data.put("date","");~~
8
+
9
+ ~~を追加し,~~
10
+
11
+ ~~new String[]{"body","id"}, // どの項目を~~
12
+
13
+ ~~を~~
14
+
15
+ ~~new String[]{"body","date"}, // どの項目を~~
16
+
17
+ ~~としては如何でしょうか.~~
18
+
19
+ uuid が表示されている個所に作成日が表示されるようにした修正箇所のみを書き出します.
20
+ 修正前をコメントにしてあるので, 修正箇所の手がかりにしてください.
21
+ SQL の実行に rawQuery/execSQL が使われていたため, 各専用メソッドに置き換えも行っています.
22
+ MemoOpenHelper のバージョンは変えていませんので, 実行時は既存を一度アンインストールしてください.
23
+
24
+ MemoOpenHelper.java
25
+ ```java
26
+ //db.execSQL("CREATE TABLE MEMO_TABLE (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "uuid TEXT, " + "body TEXT)");
27
+ db.execSQL("CREATE TABLE MEMO_TABLE (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "uuid TEXT, " + "body TEXT, " + "datetime INTEGER)");
3
- ```
28
+ ````
29
+
30
+ ListActivity.java
31
+ ```java
32
+ (onCreateメソッド内)
33
+ // rawQueryというSELECT専用メソッドを使用してデータを取得する
34
+ //Cursor c = db.rawQuery("select uuid, body from MEMO_TABLE order by id", null);
35
+ Cursor c = db.query("MEMO_TABLE", new String[]{"uuid","body","datetime"}, null,null,null,null, "id");
36
+
37
+ :
38
+
39
+ String body = c.getString(1);
40
+ long datetime = c.getLong(2); //追加
41
+
42
+ :
43
+
4
44
  data.put("id",uuid);
45
+ data.put("datetime", new SimpleDateFormat("Y-M-d").format(new Date(datetime))); //追加
46
+
47
+ :
48
+
49
+ //new String[]{"body","id"}, // どの項目を
50
+ new String[]{"body","datetime"}, // どの項目を
51
+
52
+ :
53
+
54
+ (onItemClickメソッド内)
55
+ // 選択されたビューを取得 TwoLineListItemを取得した後、text2の値を取得する
56
+ //TwoLineListItem two = (TwoLineListItem)view;
57
+ //TextView idTextView = (TextView)two.findViewById(android.R.id.text2);
58
+ //TextView idTextView = (TextView)two.getText2();
59
+ //String idStr = (String) idTextView.getText();
60
+ String idStr = memoList.get(position).get("id");
61
+
62
+ :
63
+
64
+ (onItemLongClickメソッド内)
65
+ // 選択されたビューを取得 TwoLineListItemを取得した後、text2の値を取得する
66
+ //TwoLineListItem two = (TwoLineListItem)view;
67
+ //TextView idTextView = (TextView)two.getText2();
68
+ //String idStr = (String) idTextView.getText();
69
+ String idStr = memoList.get(position).get("id");
70
+
71
+ :
72
+
73
+ //db.execSQL("DELETE FROM MEMO_TABLE WHERE uuid = '"+ idStr +"'");
74
+ db.delete("MEMO_TABLE", "uuid=?", new String[]{idStr});
5
75
  ```
76
+
77
+ CreateMemoActivity.java
78
+ ```java
79
+ (onCreateメソッド内)
80
+ //Cursor c = db.rawQuery("select body from MEMO_TABLE where uuid = '"+ id +"'", null);
81
+ Cursor c = db.query("MEMO_TABLE", new String[]{"body"}, "uuid=?", new String[]{id},null,null,null);
82
+
83
+ :
84
+
85
+ (onClickメソッド内)
86
+ // INSERT
87
+ //db.execSQL("insert into MEMO_TABLE(uuid, body) VALUES('"+ id +"', '"+ bodyStr +"')");
88
+ ContentValues cv = new ContentValues();
89
+ cv.put("uuid", id);
90
+ cv.put("body", bodyStr);
91
+ cv.put("datetime", System.currentTimeMillis());
92
+ db.insert("MEMO_TABLE", null, cv);
6
- の下に
93
+ else{
94
+ // UPDATE
95
+ //db.execSQL("update MEMO_TABLE set body = '"+ bodyStr +"' where uuid = '"+id+"'");
96
+ ContentValues cv = new ContentValues();
97
+ cv.put("body", bodyStr);
98
+ db.update("MEMO_TABLE", cv, "uuid=?", new String[]{id});
7
- ```
99
+ ```
8
- data.put("date","");
9
- ```
10
- を追加し,
11
- ```
12
- new String[]{"body","id"}, // どの項目を
13
- ```
14
-
15
- ```
16
- new String[]{"body","date"}, // どの項目を
17
- ```
18
- としては如何でしょうか.