Android Samples
Revision | bab087385d8b9ce5112255b863a320f5e0f3b756 (tree) |
---|---|
Zeit | 2014-06-13 06:09:51 |
Autor | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
Added ui-pattern-listview-detail-editor module
@@ -94,6 +94,7 @@ | ||
94 | 94 | <module>listview-single-choice-checkable-layout</module> |
95 | 95 | <module>listview-single-choice-custom-layout</module> |
96 | 96 | |
97 | + | |
97 | 98 | <module>markdown-viewer</module> |
98 | 99 | |
99 | 100 | <module>navigation-drawer-hello</module> |
@@ -119,6 +120,8 @@ | ||
119 | 120 | <module>service-hello</module> |
120 | 121 | <module>service-start-at-boottime</module> |
121 | 122 | |
123 | + <module>ui-pattern-listview-detail-editor</module> | |
124 | + | |
122 | 125 | <module>view-composite-hello</module> |
123 | 126 | <module>view-custom-drawing-animation</module> |
124 | 127 | <module>view-custom-drawing-hello</module> |
@@ -0,0 +1,40 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<manifest | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + package="com.example.hello.android.ui_pattern_listview_detail_editor" | |
5 | + android:versionCode="1" | |
6 | + android:versionName="1.0" | |
7 | +> | |
8 | + <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="19" /> | |
9 | + | |
10 | + <application android:label="@string/app_name"> | |
11 | + <activity | |
12 | + android:name=".UserListActivity" | |
13 | + android:label="@string/app_name" | |
14 | + > | |
15 | + <intent-filter> | |
16 | + <action android:name="android.intent.action.MAIN" /> | |
17 | + <category android:name="android.intent.category.LAUNCHER" /> | |
18 | + </intent-filter> | |
19 | + </activity> | |
20 | + | |
21 | + <activity | |
22 | + android:name=".UserDetailActivity" | |
23 | + android:label="@string/user_detail" | |
24 | + /> | |
25 | + | |
26 | + <activity | |
27 | + android:name=".EditNameActivity" | |
28 | + android:label="@string/edit_name" | |
29 | + /> | |
30 | + <activity | |
31 | + android:name=".EditEmailActivity" | |
32 | + android:label="@string/edit_email" | |
33 | + /> | |
34 | + <activity | |
35 | + android:name=".EditAgeActivity" | |
36 | + android:label="@string/edit_age" | |
37 | + /> | |
38 | + | |
39 | + </application> | |
40 | +</manifest> |
@@ -0,0 +1,46 @@ | ||
1 | + | |
2 | +all : package | |
3 | + | |
4 | +#################### build | |
5 | + | |
6 | +compile : | |
7 | + mvn $@ | |
8 | + | |
9 | +gen : | |
10 | + mvn android:generate-sources | |
11 | + | |
12 | +package : | |
13 | + mvn $@ | |
14 | + | |
15 | +release : | |
16 | + mvn clean -Prelease package | |
17 | + | |
18 | +install : | |
19 | + mvn $@ | |
20 | + | |
21 | +deploy : | |
22 | + mvn package android:deploy | |
23 | + | |
24 | +undeploy : | |
25 | + mvn android:undeploy | |
26 | + | |
27 | +rebuild : | |
28 | + mvn clean package | |
29 | + | |
30 | +redeploy : | |
31 | + mvn clean package android:deploy | |
32 | + | |
33 | +#################### emulator | |
34 | + | |
35 | +start : | |
36 | + mvn android:emulator-start | |
37 | + | |
38 | +stop : | |
39 | + mvn android:emulator-stop | |
40 | + | |
41 | +#################### | |
42 | +dist : | |
43 | + mvn assembly:assembly | |
44 | + | |
45 | +clean : | |
46 | + mvn $@ |
@@ -0,0 +1,22 @@ | ||
1 | +! ui-pattern-listview-detail-editor | |
2 | + | |
3 | +よくあるデータの一覧と詳細画面の詳細画面の方を ListView を使って | |
4 | +作ってみるサンプル。 PreferenceActivity の画面のような使い勝手で | |
5 | +詳細画面の各項目を編集できる。 | |
6 | + | |
7 | + | |
8 | +!! ビルドターゲット | |
9 | + | |
10 | +http://maven-android-plugin-m2site.googlecode.com/svn/plugin-info.html | |
11 | + | |
12 | +* mvn compile - アプリケーションのコンパイル | |
13 | +* mvn package - apk の作成 | |
14 | +* mvn clean - ビルドファイルの削除 | |
15 | +* mvn install - maven の local リポジトリにインストール | |
16 | + | |
17 | +* mvn android:deploy - エミュレータやデバイスにインストール | |
18 | +* mvn android:generate-sources | |
19 | + | |
20 | +* mvn install - アプリケーションのインストール | |
21 | +* mvn uninstall - アプリケーションのアンインストール | |
22 | + |
@@ -0,0 +1,142 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<project | |
3 | + xmlns="http://maven.apache.org/POM/4.0.0" | |
4 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
5 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" | |
6 | +> | |
7 | + <modelVersion>4.0.0</modelVersion> | |
8 | + <groupId>com.example.hello</groupId> | |
9 | + <artifactId>ui-pattern-listview-detail-editor</artifactId> | |
10 | + <packaging>apk</packaging> | |
11 | + <version>1.0.0</version> | |
12 | + <name>UI ListView Detail Editor</name> | |
13 | + <url>http://www.example.com/</url> | |
14 | + | |
15 | + <parent> | |
16 | + <groupId>com.example.hello</groupId> | |
17 | + <artifactId>android-samples</artifactId> | |
18 | + <version>1.0.0</version> | |
19 | + </parent> | |
20 | + | |
21 | + <dependencies> | |
22 | + | |
23 | + <dependency> | |
24 | + <groupId>android</groupId> | |
25 | + <artifactId>android</artifactId> | |
26 | + <version>1.5_r4</version> | |
27 | + <!-- | |
28 | + <version>1.5_r3</version> | |
29 | + <version>1.5_r4</version> | |
30 | + <version>1.6_r2</version> | |
31 | + <version>2.1.2</version> | |
32 | + <version>2.1_r1</version> | |
33 | + <version>2.2.1</version> | |
34 | + <version>2.3.1</version> | |
35 | + <version>2.3.3</version> | |
36 | + <version>4.0.1.2</version> | |
37 | + --> | |
38 | + <scope>provided</scope> | |
39 | + </dependency> | |
40 | + | |
41 | + <dependency> | |
42 | + <groupId>junit</groupId> | |
43 | + <artifactId>junit</artifactId> | |
44 | + <scope>test</scope> | |
45 | + </dependency> | |
46 | + </dependencies> | |
47 | + | |
48 | + <build> | |
49 | + <sourceDirectory>src</sourceDirectory> | |
50 | + | |
51 | + <plugins> | |
52 | + <plugin> | |
53 | + <groupId>com.jayway.maven.plugins.android.generation2</groupId> | |
54 | + <artifactId>android-maven-plugin</artifactId> | |
55 | + <configuration> | |
56 | + <sdk> | |
57 | + <!--<path>${env.ANDROID_HOME}</path>--> | |
58 | + <platform>11</platform> <!-- 11 or later for showAsAction attribute in user_list_activity_option_menu.xml --> | |
59 | + </sdk> | |
60 | + </configuration> | |
61 | + <extensions>true</extensions> | |
62 | + </plugin> | |
63 | + | |
64 | + <!-- mvn compile --> | |
65 | + <plugin> | |
66 | + <groupId>org.apache.maven.plugins</groupId> | |
67 | + <artifactId>maven-compiler-plugin</artifactId> | |
68 | + <configuration> | |
69 | + <source>1.5</source> | |
70 | + <target>1.5</target> | |
71 | + <encoding>UTF-8</encoding> | |
72 | + </configuration> | |
73 | + </plugin> | |
74 | + | |
75 | + <!-- mvn assembly:assembly --> | |
76 | + <plugin> | |
77 | + <groupId>org.apache.maven.plugins</groupId> | |
78 | + <artifactId>maven-assembly-plugin</artifactId> | |
79 | + <configuration> | |
80 | + <descriptorRefs> | |
81 | + <!-- | |
82 | + <descriptorRef>jar-with-dependencies</descriptorRef> | |
83 | + <descriptorRef>bin</descriptorRef> | |
84 | + <descriptorRef>src</descriptorRef> | |
85 | + --> | |
86 | + <descriptorRef>project</descriptorRef> | |
87 | + </descriptorRefs> | |
88 | + </configuration> | |
89 | + </plugin> | |
90 | + | |
91 | + <!-- mvn resources:resources --> | |
92 | + <plugin> | |
93 | + <groupId>org.apache.maven.plugins</groupId> | |
94 | + <artifactId>maven-resources-plugin</artifactId> | |
95 | + <configuration> | |
96 | + <encoding>UTF-8</encoding> | |
97 | + </configuration> | |
98 | + </plugin> | |
99 | + | |
100 | + <!-- mvn site --> | |
101 | + <plugin> | |
102 | + <groupId>org.apache.maven.plugins</groupId> | |
103 | + <artifactId>maven-site-plugin</artifactId> | |
104 | + <configuration> | |
105 | + <locales>en</locales> | |
106 | + <inputEncoding>UTF-8</inputEncoding> | |
107 | + <outputEncoding>UTF-8</outputEncoding> | |
108 | + | |
109 | + <reportPlugins> | |
110 | + <plugin> | |
111 | + <groupId>org.apache.maven.plugins</groupId> | |
112 | + <artifactId>maven-project-info-reports-plugin</artifactId> | |
113 | + </plugin> | |
114 | + | |
115 | + <!-- mvn javadoc:javadoc --> | |
116 | + <plugin> | |
117 | + <groupId>org.apache.maven.plugins</groupId> | |
118 | + <artifactId>maven-javadoc-plugin</artifactId> | |
119 | + <configuration> | |
120 | + <charset>utf-8</charset> | |
121 | + </configuration> | |
122 | + </plugin> | |
123 | + </reportPlugins> | |
124 | + </configuration> | |
125 | + </plugin> | |
126 | + | |
127 | + </plugins> | |
128 | + | |
129 | + <!-- mvn resources:resources --> | |
130 | + <resources> | |
131 | + <resource> | |
132 | + <directory>res</directory> | |
133 | + <filtering>false</filtering> | |
134 | + <includes> | |
135 | + <include>**/*.properties</include> | |
136 | + </includes> | |
137 | + </resource> | |
138 | + </resources> | |
139 | + </build> | |
140 | + | |
141 | + | |
142 | +</project> |
@@ -0,0 +1,28 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:layout_width="fill_parent" | |
5 | + android:layout_height="wrap_content" | |
6 | + android:padding="4dip" | |
7 | + android:orientation="horizontal" | |
8 | +> | |
9 | + <EditText | |
10 | + android:id="@+id/age_edittext" | |
11 | + android:layout_width="wrap_content" | |
12 | + android:layout_height="wrap_content" | |
13 | + android:layout_weight="1" | |
14 | + android:layout_margin="2dip" | |
15 | + android:layout_gravity="center_vertical" | |
16 | + android:inputType="number" | |
17 | + android:hint="@string/user_age" | |
18 | + /> | |
19 | + <Button | |
20 | + android:id="@+id/ok_button" | |
21 | + android:layout_width="wrap_content" | |
22 | + android:layout_height="wrap_content" | |
23 | + android:layout_weight="0" | |
24 | + android:layout_margin="2dip" | |
25 | + android:layout_gravity="center_vertical" | |
26 | + android:text="@android:string/ok" | |
27 | + /> | |
28 | +</LinearLayout> | |
\ No newline at end of file |
@@ -0,0 +1,28 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:layout_width="fill_parent" | |
5 | + android:layout_height="wrap_content" | |
6 | + android:padding="4dip" | |
7 | + android:orientation="horizontal" | |
8 | +> | |
9 | + <EditText | |
10 | + android:id="@+id/email_edittext" | |
11 | + android:layout_width="wrap_content" | |
12 | + android:layout_height="wrap_content" | |
13 | + android:layout_weight="1" | |
14 | + android:layout_margin="2dip" | |
15 | + android:layout_gravity="center_vertical" | |
16 | + android:inputType="textEmailAddress" | |
17 | + android:hint="@string/user_email" | |
18 | + /> | |
19 | + <Button | |
20 | + android:id="@+id/ok_button" | |
21 | + android:layout_width="wrap_content" | |
22 | + android:layout_height="wrap_content" | |
23 | + android:layout_weight="0" | |
24 | + android:layout_margin="2dip" | |
25 | + android:layout_gravity="center_vertical" | |
26 | + android:text="@android:string/ok" | |
27 | + /> | |
28 | +</LinearLayout> | |
\ No newline at end of file |
@@ -0,0 +1,28 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:layout_width="fill_parent" | |
5 | + android:layout_height="wrap_content" | |
6 | + android:padding="4dip" | |
7 | + android:orientation="horizontal" | |
8 | +> | |
9 | + <EditText | |
10 | + android:id="@+id/name_edittext" | |
11 | + android:layout_width="wrap_content" | |
12 | + android:layout_height="wrap_content" | |
13 | + android:layout_weight="1" | |
14 | + android:layout_margin="2dip" | |
15 | + android:layout_gravity="center_vertical" | |
16 | + android:inputType="textPersonName" | |
17 | + android:hint="@string/user_name" | |
18 | + /> | |
19 | + <Button | |
20 | + android:id="@+id/ok_button" | |
21 | + android:layout_width="wrap_content" | |
22 | + android:layout_height="wrap_content" | |
23 | + android:layout_weight="0" | |
24 | + android:layout_margin="2dip" | |
25 | + android:layout_gravity="center_vertical" | |
26 | + android:text="@android:string/ok" | |
27 | + /> | |
28 | +</LinearLayout> | |
\ No newline at end of file |
@@ -0,0 +1,21 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<FrameLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:layout_width="fill_parent" | |
5 | + android:layout_height="fill_parent" | |
6 | + android:padding="2dip" | |
7 | +> | |
8 | + <ListView | |
9 | + android:id="@android:id/list" | |
10 | + android:layout_width="fill_parent" | |
11 | + android:layout_height="fill_parent" | |
12 | + android:drawSelectorOnTop="false" | |
13 | + /> | |
14 | + <TextView | |
15 | + android:id="@android:id/empty" | |
16 | + android:layout_width="wrap_content" | |
17 | + android:layout_height="wrap_content" | |
18 | + android:layout_gravity="center" | |
19 | + android:text="@string/no_data" | |
20 | + /> | |
21 | +</FrameLayout> | |
\ No newline at end of file |
@@ -0,0 +1,79 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<FrameLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:layout_width="fill_parent" | |
5 | + android:layout_height="wrap_content" | |
6 | + android:padding="4dip" | |
7 | +> | |
8 | + <LinearLayout | |
9 | + android:id="@+id/user_name_container" | |
10 | + android:layout_width="fill_parent" | |
11 | + android:layout_height="wrap_content" | |
12 | + android:orientation="vertical" | |
13 | + > | |
14 | + <TextView | |
15 | + android:layout_width="fill_parent" | |
16 | + android:layout_height="wrap_content" | |
17 | + android:text="@string/user_name" | |
18 | + android:background="#6666" | |
19 | + /> | |
20 | + <TextView | |
21 | + android:id="@+id/user_name_textview" | |
22 | + android:layout_width="fill_parent" | |
23 | + android:layout_height="wrap_content" | |
24 | + android:layout_margin="4dip" | |
25 | + android:paddingLeft="16dip" | |
26 | + android:hint="@string/user_name" | |
27 | + android:textSize="22sp" | |
28 | + android:textColor="?android:attr/textColorPrimary" | |
29 | + /> | |
30 | + </LinearLayout> | |
31 | + | |
32 | + <LinearLayout | |
33 | + android:id="@+id/user_email_container" | |
34 | + android:layout_width="fill_parent" | |
35 | + android:layout_height="wrap_content" | |
36 | + android:orientation="vertical" | |
37 | + > | |
38 | + <TextView | |
39 | + android:layout_width="fill_parent" | |
40 | + android:layout_height="wrap_content" | |
41 | + android:text="@string/user_email" | |
42 | + android:background="#6666" | |
43 | + /> | |
44 | + <TextView | |
45 | + android:id="@+id/user_email_textview" | |
46 | + android:layout_width="fill_parent" | |
47 | + android:layout_height="wrap_content" | |
48 | + android:layout_margin="4dip" | |
49 | + android:paddingLeft="16dip" | |
50 | + android:hint="@string/user_email" | |
51 | + android:textSize="22sp" | |
52 | + android:textColor="?android:attr/textColorPrimary" | |
53 | + /> | |
54 | + </LinearLayout> | |
55 | + | |
56 | + <LinearLayout | |
57 | + android:id="@+id/user_age_container" | |
58 | + android:layout_width="fill_parent" | |
59 | + android:layout_height="wrap_content" | |
60 | + android:orientation="vertical" | |
61 | + > | |
62 | + <TextView | |
63 | + android:layout_width="fill_parent" | |
64 | + android:layout_height="wrap_content" | |
65 | + android:text="@string/user_age" | |
66 | + android:background="#6666" | |
67 | + /> | |
68 | + <TextView | |
69 | + android:id="@+id/user_age_textview" | |
70 | + android:layout_width="fill_parent" | |
71 | + android:layout_height="wrap_content" | |
72 | + android:layout_margin="4dip" | |
73 | + android:paddingLeft="16dip" | |
74 | + android:hint="@string/user_age" | |
75 | + android:textSize="22sp" | |
76 | + android:textColor="?android:attr/textColorPrimary" | |
77 | + /> | |
78 | + </LinearLayout> | |
79 | +</FrameLayout> | |
\ No newline at end of file |
@@ -0,0 +1,21 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<FrameLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:layout_width="fill_parent" | |
5 | + android:layout_height="fill_parent" | |
6 | + android:padding="4dip" | |
7 | +> | |
8 | + <ListView | |
9 | + android:id="@android:id/list" | |
10 | + android:layout_width="fill_parent" | |
11 | + android:layout_height="fill_parent" | |
12 | + android:drawSelectorOnTop="false" | |
13 | + /> | |
14 | + <TextView | |
15 | + android:id="@android:id/empty" | |
16 | + android:layout_width="wrap_content" | |
17 | + android:layout_height="wrap_content" | |
18 | + android:layout_gravity="center" | |
19 | + android:text="@string/no_data" | |
20 | + /> | |
21 | +</FrameLayout> | |
\ No newline at end of file |
@@ -0,0 +1,19 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<menu xmlns:android="http://schemas.android.com/apk/res/android"> | |
3 | + <!-- | |
4 | + showAsAction | |
5 | + - ifRoom | |
6 | + - always | |
7 | + - never | |
8 | + - withText | |
9 | + - collapseActionView (API Level 14) | |
10 | + --> | |
11 | + <item | |
12 | + android:id="@+id/add_user_menuitem" | |
13 | + android:title="@string/add" | |
14 | + android:icon="@android:drawable/ic_menu_add" | |
15 | + android:numericShortcut="1" | |
16 | + android:alphabeticShortcut="a" | |
17 | + android:showAsAction="ifRoom|withText" | |
18 | + /> | |
19 | +</menu> | |
\ No newline at end of file |
@@ -0,0 +1,31 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <array name="names"> | |
4 | + <item>Alice</item> | |
5 | + <item>Bob</item> | |
6 | + <item>Charlie</item> | |
7 | + <item>David</item> | |
8 | + <item>Eve</item> | |
9 | + <item>Frank</item> | |
10 | + <item>George</item> | |
11 | + <item>Harvey</item> | |
12 | + <item>Ivan</item> | |
13 | + <item>Justin</item> | |
14 | + <item>Kyle</item> | |
15 | + <item>Linda</item> | |
16 | + <item>Matilda</item> | |
17 | + <item>Natalia</item> | |
18 | + <item>Oscar</item> | |
19 | + <item>Pat</item> | |
20 | + <item>Quincy</item> | |
21 | + <item>Rachel</item> | |
22 | + <item>Steave</item> | |
23 | + <item>Trent</item> | |
24 | + <item>Urien</item> | |
25 | + <item>Victor</item> | |
26 | + <item>Walter</item> | |
27 | + <item>Xenia</item> | |
28 | + <item>Yuriya</item> | |
29 | + <item>Zoe</item> | |
30 | + </array> | |
31 | +</resources> |
@@ -0,0 +1,18 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <string name="app_name">ListView DetailEditor</string> | |
4 | + <string name="no_data">NO DATA</string> | |
5 | + <string name="add">Add</string> | |
6 | + | |
7 | + <string name="user_list">User List</string> | |
8 | + <string name="user_detail">User Detail</string> | |
9 | + | |
10 | + <string name="user_name">Name</string> | |
11 | + <string name="user_email">E-Mail</string> | |
12 | + <string name="user_age">Age</string> | |
13 | + | |
14 | + <string name="edit_name">Edit Name</string> | |
15 | + <string name="edit_email">Edit E-Mail</string> | |
16 | + <string name="edit_age">Edit Age</string> | |
17 | + | |
18 | +</resources> |
@@ -0,0 +1,81 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor; | |
25 | + | |
26 | +import android.app.Activity; | |
27 | +import android.content.Intent; | |
28 | +import android.os.Bundle; | |
29 | +import android.view.View; | |
30 | +import android.view.View.OnClickListener; | |
31 | +import android.view.WindowManager; | |
32 | +import android.widget.Button; | |
33 | +import android.widget.EditText; | |
34 | + | |
35 | +public class EditAgeActivity extends Activity | |
36 | + implements OnClickListener | |
37 | +{ | |
38 | + | |
39 | + public static final String EXTRA_USER_AGE = EditAgeActivity.class.getPackage().getName() + ".EXTRA_USER_AGE"; | |
40 | + private EditText ageEditText; | |
41 | + private Button okButton; | |
42 | + | |
43 | + @Override | |
44 | + protected void onCreate(Bundle savedInstanceState) | |
45 | + { | |
46 | + super.onCreate(savedInstanceState); | |
47 | + setContentView(R.layout.edit_age_activity); | |
48 | + | |
49 | + getWindow().setSoftInputMode( | |
50 | + WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | |
51 | + | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | |
52 | + | |
53 | + okButton = (Button) findViewById(R.id.ok_button); | |
54 | + ageEditText = (EditText) findViewById(R.id.age_edittext); | |
55 | + okButton.setOnClickListener(this); | |
56 | + | |
57 | + Intent intent = getIntent(); | |
58 | + String userAge = intent.getStringExtra(EXTRA_USER_AGE); | |
59 | + if (userAge != null) | |
60 | + { | |
61 | + ageEditText.setText(userAge); | |
62 | + } | |
63 | + | |
64 | + } | |
65 | + | |
66 | + public void onClick(View v) | |
67 | + { | |
68 | + switch (v.getId()) | |
69 | + { | |
70 | + case R.id.ok_button: | |
71 | + String age = ageEditText.getText().toString(); | |
72 | + Intent resultIntent = new Intent(); | |
73 | + resultIntent.putExtra(EXTRA_USER_AGE, age); | |
74 | + setResult(RESULT_OK, resultIntent); | |
75 | + finish(); | |
76 | + break; | |
77 | + default: | |
78 | + throw new AssertionError(); | |
79 | + } | |
80 | + } | |
81 | +} |
@@ -0,0 +1,81 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor; | |
25 | + | |
26 | +import android.app.Activity; | |
27 | +import android.content.Intent; | |
28 | +import android.os.Bundle; | |
29 | +import android.view.View; | |
30 | +import android.view.View.OnClickListener; | |
31 | +import android.view.WindowManager; | |
32 | +import android.widget.Button; | |
33 | +import android.widget.EditText; | |
34 | + | |
35 | +public class EditEmailActivity extends Activity | |
36 | + implements OnClickListener | |
37 | +{ | |
38 | + | |
39 | + public static final String EXTRA_USER_EMAIL = EditEmailActivity.class.getPackage().getName() + ".EXTRA_USER_EMAIL"; | |
40 | + private EditText emailEditText; | |
41 | + private Button okButton; | |
42 | + | |
43 | + @Override | |
44 | + protected void onCreate(Bundle savedInstanceState) | |
45 | + { | |
46 | + super.onCreate(savedInstanceState); | |
47 | + setContentView(R.layout.edit_email_activity); | |
48 | + | |
49 | + getWindow().setSoftInputMode( | |
50 | + WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | |
51 | + | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | |
52 | + | |
53 | + okButton = (Button) findViewById(R.id.ok_button); | |
54 | + emailEditText = (EditText) findViewById(R.id.email_edittext); | |
55 | + okButton.setOnClickListener(this); | |
56 | + | |
57 | + Intent intent = getIntent(); | |
58 | + String userName = intent.getStringExtra(EXTRA_USER_EMAIL); | |
59 | + if (userName != null) | |
60 | + { | |
61 | + emailEditText.setText(userName); | |
62 | + } | |
63 | + | |
64 | + } | |
65 | + | |
66 | + public void onClick(View v) | |
67 | + { | |
68 | + switch (v.getId()) | |
69 | + { | |
70 | + case R.id.ok_button: | |
71 | + String email = emailEditText.getText().toString(); | |
72 | + Intent resultIntent = new Intent(); | |
73 | + resultIntent.putExtra(EXTRA_USER_EMAIL, email); | |
74 | + setResult(RESULT_OK, resultIntent); | |
75 | + finish(); | |
76 | + break; | |
77 | + default: | |
78 | + throw new AssertionError(); | |
79 | + } | |
80 | + } | |
81 | +} |
@@ -0,0 +1,81 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor; | |
25 | + | |
26 | +import android.app.Activity; | |
27 | +import android.content.Intent; | |
28 | +import android.os.Bundle; | |
29 | +import android.view.View; | |
30 | +import android.view.View.OnClickListener; | |
31 | +import android.view.WindowManager; | |
32 | +import android.widget.Button; | |
33 | +import android.widget.EditText; | |
34 | + | |
35 | +public class EditNameActivity extends Activity | |
36 | + implements OnClickListener | |
37 | +{ | |
38 | + | |
39 | + public static final String EXTRA_USER_NAME = EditNameActivity.class.getPackage().getName() + ".EXTRA_USER_NAME"; | |
40 | + private EditText nameEditText; | |
41 | + private Button okButton; | |
42 | + | |
43 | + @Override | |
44 | + protected void onCreate(Bundle savedInstanceState) | |
45 | + { | |
46 | + super.onCreate(savedInstanceState); | |
47 | + setContentView(R.layout.edit_name_activity); | |
48 | + | |
49 | + getWindow().setSoftInputMode( | |
50 | + WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | |
51 | + | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | |
52 | + | |
53 | + okButton = (Button) findViewById(R.id.ok_button); | |
54 | + nameEditText = (EditText) findViewById(R.id.name_edittext); | |
55 | + okButton.setOnClickListener(this); | |
56 | + | |
57 | + Intent intent = getIntent(); | |
58 | + String userName = intent.getStringExtra(EXTRA_USER_NAME); | |
59 | + if (userName != null) | |
60 | + { | |
61 | + nameEditText.setText(userName); | |
62 | + } | |
63 | + | |
64 | + } | |
65 | + | |
66 | + public void onClick(View v) | |
67 | + { | |
68 | + switch (v.getId()) | |
69 | + { | |
70 | + case R.id.ok_button: | |
71 | + String name = nameEditText.getText().toString(); | |
72 | + Intent resultIntent = new Intent(); | |
73 | + resultIntent.putExtra(EXTRA_USER_NAME, name); | |
74 | + setResult(RESULT_OK, resultIntent); | |
75 | + finish(); | |
76 | + break; | |
77 | + default: | |
78 | + throw new AssertionError(); | |
79 | + } | |
80 | + } | |
81 | +} |
@@ -0,0 +1,116 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor; | |
25 | + | |
26 | +/** | |
27 | + * | |
28 | + * @author Masahiko, SAWAI <masahiko.sawai@gmail.com> | |
29 | + */ | |
30 | +public class User | |
31 | +{ | |
32 | + | |
33 | + private String name; | |
34 | + private String email; | |
35 | + private int age; | |
36 | + | |
37 | + public User(String name, String email, int age) | |
38 | + { | |
39 | + this.name = name; | |
40 | + this.email = email; | |
41 | + this.age = age; | |
42 | + } | |
43 | + | |
44 | + public String getName() | |
45 | + { | |
46 | + return name; | |
47 | + } | |
48 | + | |
49 | + public void setName(String name) | |
50 | + { | |
51 | + this.name = name; | |
52 | + } | |
53 | + | |
54 | + public String getEmail() | |
55 | + { | |
56 | + return email; | |
57 | + } | |
58 | + | |
59 | + public void setEmail(String email) | |
60 | + { | |
61 | + this.email = email; | |
62 | + } | |
63 | + | |
64 | + public int getAge() | |
65 | + { | |
66 | + return age; | |
67 | + } | |
68 | + | |
69 | + public void setAge(int age) | |
70 | + { | |
71 | + this.age = age; | |
72 | + } | |
73 | + | |
74 | + @Override | |
75 | + public int hashCode() | |
76 | + { | |
77 | + int hash = 7; | |
78 | + hash = 61 * hash + (this.name != null ? this.name.hashCode() : 0); | |
79 | + hash = 61 * hash + (this.email != null ? this.email.hashCode() : 0); | |
80 | + hash = 61 * hash + this.age; | |
81 | + return hash; | |
82 | + } | |
83 | + | |
84 | + @Override | |
85 | + public boolean equals(Object obj) | |
86 | + { | |
87 | + if (obj == null) | |
88 | + { | |
89 | + return false; | |
90 | + } | |
91 | + if (getClass() != obj.getClass()) | |
92 | + { | |
93 | + return false; | |
94 | + } | |
95 | + final User other = (User) obj; | |
96 | + if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) | |
97 | + { | |
98 | + return false; | |
99 | + } | |
100 | + if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) | |
101 | + { | |
102 | + return false; | |
103 | + } | |
104 | + if (this.age != other.age) | |
105 | + { | |
106 | + return false; | |
107 | + } | |
108 | + return true; | |
109 | + } | |
110 | + | |
111 | + @Override | |
112 | + public String toString() | |
113 | + { | |
114 | + return "User{" + "name=" + name + ", email=" + email + ", age=" + age + '}'; | |
115 | + } | |
116 | +} |
@@ -0,0 +1,312 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor; | |
25 | + | |
26 | +import android.app.ListActivity; | |
27 | +import android.content.ContentValues; | |
28 | +import android.content.Intent; | |
29 | +import android.database.Cursor; | |
30 | +import android.database.sqlite.SQLiteDatabase; | |
31 | +import android.os.Bundle; | |
32 | +import android.util.Log; | |
33 | +import android.view.LayoutInflater; | |
34 | +import android.view.View; | |
35 | +import android.view.ViewGroup; | |
36 | +import android.widget.AdapterView; | |
37 | +import android.widget.AdapterView.OnItemClickListener; | |
38 | +import android.widget.BaseAdapter; | |
39 | +import android.widget.TextView; | |
40 | +import com.example.hello.android.ui_pattern_listview_detail_editor.db.UserDBConstants; | |
41 | +import com.example.hello.android.ui_pattern_listview_detail_editor.db.UserDBHelper; | |
42 | + | |
43 | +public class UserDetailActivity extends ListActivity | |
44 | + implements UserDBConstants, OnItemClickListener | |
45 | +{ | |
46 | + | |
47 | + public static final String EXTRA_USER_ID = UserDetailActivity.class.getPackage() + ".EXTRA_USER_ID"; | |
48 | + private static final int REQUEST_CODE_EDIT_NAME = 0; | |
49 | + private static final int REQUEST_CODE_EDIT_EMAIL = 1; | |
50 | + private static final int REQUEST_CODE_EDIT_AGE = 2; | |
51 | + private static final int POSITION_NAME = 0; | |
52 | + private static final int POSITION_EMAIL = 1; | |
53 | + private static final int POSITION_AGE = 2; | |
54 | + private static final String LOG_TAG = "XXX"; | |
55 | + private LayoutInflater inflator; | |
56 | + private SQLiteDatabase db; | |
57 | + private UserDetailListAdapter listAdapter; | |
58 | + private long currentId; | |
59 | + private User user; | |
60 | + | |
61 | + public void onItemClick(AdapterView<?> parent, View view, int position, long id) | |
62 | + { | |
63 | + switch (position) | |
64 | + { | |
65 | + case POSITION_NAME: | |
66 | + Intent editNameIntent = new Intent(this, EditNameActivity.class); | |
67 | + editNameIntent.putExtra(EditNameActivity.EXTRA_USER_NAME, user.getName()); | |
68 | + startActivityForResult(editNameIntent, REQUEST_CODE_EDIT_NAME); | |
69 | + break; | |
70 | + case POSITION_EMAIL: | |
71 | + Intent editEMailIntent = new Intent(this, EditEmailActivity.class); | |
72 | + editEMailIntent.putExtra(EditEmailActivity.EXTRA_USER_EMAIL, user.getEmail()); | |
73 | + startActivityForResult(editEMailIntent, REQUEST_CODE_EDIT_EMAIL); | |
74 | + break; | |
75 | + case POSITION_AGE: | |
76 | + Intent editAgeIntent = new Intent(this, EditAgeActivity.class); | |
77 | + editAgeIntent.putExtra(EditAgeActivity.EXTRA_USER_AGE, String.valueOf(user.getAge())); | |
78 | + startActivityForResult(editAgeIntent, REQUEST_CODE_EDIT_AGE); | |
79 | + break; | |
80 | + default: | |
81 | + throw new AssertionError(); | |
82 | + } | |
83 | + } | |
84 | + | |
85 | + @Override | |
86 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
87 | + { | |
88 | + Log.v(LOG_TAG, "onActivityResult() : Hello"); | |
89 | + if (resultCode == RESULT_OK) | |
90 | + { | |
91 | + if (requestCode == REQUEST_CODE_EDIT_NAME) | |
92 | + { | |
93 | + String name = data.getStringExtra(EditNameActivity.EXTRA_USER_NAME); | |
94 | + user.setName(name); | |
95 | + listAdapter.notifyDataSetChanged(); | |
96 | + } | |
97 | + else if (requestCode == REQUEST_CODE_EDIT_EMAIL) | |
98 | + { | |
99 | + String email = data.getStringExtra(EditEmailActivity.EXTRA_USER_EMAIL); | |
100 | + user.setEmail(email); | |
101 | + listAdapter.notifyDataSetChanged(); | |
102 | + } | |
103 | + else if (requestCode == REQUEST_CODE_EDIT_AGE) | |
104 | + { | |
105 | + String ageString = data.getStringExtra(EditAgeActivity.EXTRA_USER_AGE); | |
106 | + int age = Integer.parseInt(ageString); | |
107 | + user.setAge(age); | |
108 | + listAdapter.notifyDataSetChanged(); | |
109 | + } | |
110 | + } | |
111 | + Log.v(LOG_TAG, "onActivityResult() : Bye"); | |
112 | + } | |
113 | + | |
114 | + @Override | |
115 | + protected void onCreate(Bundle savedInstanceState) | |
116 | + { | |
117 | + super.onCreate(savedInstanceState); | |
118 | + setContentView(R.layout.user_detail_list_activity); | |
119 | + | |
120 | + inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); | |
121 | + | |
122 | + // Init data | |
123 | + UserDBHelper userDBHelper = new UserDBHelper(this); | |
124 | + db = userDBHelper.getReadableDatabase(); | |
125 | + | |
126 | + // Init ListAdapter | |
127 | + listAdapter = new UserDetailListAdapter(); | |
128 | + setListAdapter(listAdapter); | |
129 | + | |
130 | + // Init ListView | |
131 | + getListView().setOnItemClickListener(this); | |
132 | + | |
133 | + Intent intent = getIntent(); | |
134 | + currentId = intent.getLongExtra(EXTRA_USER_ID, -1); | |
135 | + if (currentId == -1) | |
136 | + { | |
137 | + createNewUser(); | |
138 | + } | |
139 | + else | |
140 | + { | |
141 | + loadUserData(); | |
142 | + } | |
143 | + } | |
144 | + | |
145 | + @Override | |
146 | + protected void onPause() | |
147 | + { | |
148 | + Log.v(LOG_TAG, "onPause() : Hello"); | |
149 | + saveUserData(); | |
150 | + super.onPause(); | |
151 | + Log.v(LOG_TAG, "onPause() : Bye"); | |
152 | + } | |
153 | + | |
154 | + private void loadUserData() | |
155 | + { | |
156 | + Log.v(LOG_TAG, "loadUserData() : Hello"); | |
157 | + if (currentId != -1) | |
158 | + { | |
159 | + String selection = Columns.ID + " = ?"; | |
160 | + String[] selectionArgs = | |
161 | + { | |
162 | + String.valueOf(currentId) | |
163 | + }; | |
164 | + Cursor cursor = db.query(TABLE_NAME, null, selection, selectionArgs, null, null, null); | |
165 | + | |
166 | + if (cursor != null) | |
167 | + { | |
168 | + try | |
169 | + { | |
170 | + if (cursor.moveToFirst()) | |
171 | + { | |
172 | + int nameIndex = cursor.getColumnIndex(Columns.NAME); | |
173 | + int emailIndex = cursor.getColumnIndex(Columns.EMAIL); | |
174 | + int ageIndex = cursor.getColumnIndex(Columns.AGE); | |
175 | + | |
176 | + String name = cursor.getString(nameIndex); | |
177 | + String email = cursor.getString(emailIndex); | |
178 | + int age = cursor.getInt(ageIndex); | |
179 | + user = new User(name, email, age); | |
180 | + Log.d(LOG_TAG, "loadUserData() : loaded user => " + user); | |
181 | + } | |
182 | + } | |
183 | + finally | |
184 | + { | |
185 | + cursor.close(); | |
186 | + } | |
187 | + } | |
188 | + } | |
189 | + else | |
190 | + { | |
191 | + user = new User("No name", "", 0); | |
192 | + Log.d(LOG_TAG, "loadUserData() : new user => " + user); | |
193 | + saveUserData(); | |
194 | + } | |
195 | + | |
196 | + Log.v(LOG_TAG, "loadUserData() : Bye"); | |
197 | + } | |
198 | + | |
199 | + private void saveUserData() | |
200 | + { | |
201 | + Log.v(LOG_TAG, "saveUserData() : Hello"); | |
202 | + | |
203 | + ContentValues values = new ContentValues(); | |
204 | + values.put(Columns.NAME, user.getName()); | |
205 | + values.put(Columns.EMAIL, user.getEmail()); | |
206 | + values.put(Columns.AGE, user.getAge()); | |
207 | + | |
208 | + String selection = Columns.ID + " = ?"; | |
209 | + String[] selectionArgs = | |
210 | + { | |
211 | + String.valueOf(currentId) | |
212 | + }; | |
213 | + db.update(TABLE_NAME, values, selection, selectionArgs); | |
214 | + Log.d(LOG_TAG, "saveUserData() : saved user => " + user); | |
215 | + | |
216 | + Log.v(LOG_TAG, "saveUserData() : Bye"); | |
217 | + } | |
218 | + | |
219 | + private void createNewUser() | |
220 | + { | |
221 | + Log.v(LOG_TAG, "createNewUser() : Hello"); | |
222 | + | |
223 | + user = new User("No name", "", 0); | |
224 | + Log.d(LOG_TAG, "createNewUser() : created user => " + user); | |
225 | + | |
226 | + ContentValues values = new ContentValues(); | |
227 | + values.put(Columns.NAME, user.getName()); | |
228 | + values.put(Columns.EMAIL, user.getEmail()); | |
229 | + values.put(Columns.AGE, user.getAge()); | |
230 | + currentId = db.insert(TABLE_NAME, null, values); | |
231 | + | |
232 | + Log.v(LOG_TAG, "createNewUser() : Bye"); | |
233 | + } | |
234 | + | |
235 | + class UserDetailListAdapter extends BaseAdapter | |
236 | + { | |
237 | + | |
238 | + public int getCount() | |
239 | + { | |
240 | + return 3; | |
241 | + } | |
242 | + | |
243 | + public Object getItem(int position) | |
244 | + { | |
245 | + Object result = null; | |
246 | + switch (position) | |
247 | + { | |
248 | + case POSITION_NAME: | |
249 | + result = user.getName(); | |
250 | + break; | |
251 | + case POSITION_EMAIL: | |
252 | + result = user.getEmail(); | |
253 | + break; | |
254 | + case POSITION_AGE: | |
255 | + result = user.getAge(); | |
256 | + break; | |
257 | + default: | |
258 | + Log.e(LOG_TAG, "Illegal position => " + position); | |
259 | + } | |
260 | + return result; | |
261 | + } | |
262 | + | |
263 | + public long getItemId(int position) | |
264 | + { | |
265 | + return position; | |
266 | + } | |
267 | + | |
268 | + public View getView(int position, View convertView, ViewGroup parent) | |
269 | + { | |
270 | + View itemView; | |
271 | + if (convertView == null) | |
272 | + { | |
273 | + itemView = inflator.inflate(R.layout.user_detail_list_item, null); | |
274 | + } | |
275 | + else | |
276 | + { | |
277 | + itemView = convertView; | |
278 | + } | |
279 | + | |
280 | + View nameContainer = itemView.findViewById(R.id.user_name_container); | |
281 | + View emailContainer = itemView.findViewById(R.id.user_email_container); | |
282 | + View ageContainer = itemView.findViewById(R.id.user_age_container); | |
283 | + | |
284 | + nameContainer.setVisibility(View.GONE); | |
285 | + emailContainer.setVisibility(View.GONE); | |
286 | + ageContainer.setVisibility(View.GONE); | |
287 | + | |
288 | + switch (position) | |
289 | + { | |
290 | + case POSITION_NAME: | |
291 | + TextView nameTextView = (TextView) itemView.findViewById(R.id.user_name_textview); | |
292 | + nameTextView.setText(user.getName()); | |
293 | + nameContainer.setVisibility(View.VISIBLE); | |
294 | + break; | |
295 | + case POSITION_EMAIL: | |
296 | + TextView emailTextView = (TextView) itemView.findViewById(R.id.user_email_textview); | |
297 | + emailTextView.setText(user.getEmail()); | |
298 | + emailContainer.setVisibility(View.VISIBLE); | |
299 | + break; | |
300 | + case POSITION_AGE: | |
301 | + TextView ageTextView = (TextView) itemView.findViewById(R.id.user_age_textview); | |
302 | + ageTextView.setText(String.valueOf(user.getAge())); | |
303 | + ageContainer.setVisibility(View.VISIBLE); | |
304 | + break; | |
305 | + default: | |
306 | + Log.e(LOG_TAG, "Illegal position => " + position); | |
307 | + } | |
308 | + | |
309 | + return itemView; | |
310 | + } | |
311 | + } | |
312 | +} |
@@ -0,0 +1,167 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor; | |
25 | + | |
26 | +import android.app.ListActivity; | |
27 | +import android.content.Intent; | |
28 | +import android.database.Cursor; | |
29 | +import android.database.sqlite.SQLiteDatabase; | |
30 | +import android.os.Bundle; | |
31 | +import android.view.Menu; | |
32 | +import android.view.MenuItem; | |
33 | +import android.view.View; | |
34 | +import android.widget.AdapterView; | |
35 | +import android.widget.AdapterView.OnItemClickListener; | |
36 | +import android.widget.SimpleCursorAdapter; | |
37 | +import com.example.hello.android.ui_pattern_listview_detail_editor.db.UserDBConstants; | |
38 | +import com.example.hello.android.ui_pattern_listview_detail_editor.db.UserDBHelper; | |
39 | + | |
40 | +public class UserListActivity extends ListActivity | |
41 | + implements UserDBConstants, OnItemClickListener | |
42 | +{ | |
43 | + | |
44 | + private static final String[] MAPPING_FROM = | |
45 | + { | |
46 | + Columns.NAME, | |
47 | + Columns.EMAIL, | |
48 | + }; | |
49 | + private static final int[] MAPPING_TO = | |
50 | + { | |
51 | + android.R.id.text1, | |
52 | + android.R.id.text2, | |
53 | + }; | |
54 | + private static final int REQUEST_USER_DETAIL_EDIT = 0; | |
55 | + private SQLiteDatabase db; | |
56 | + private Cursor cursor; | |
57 | + private SimpleCursorAdapter listAdapter; | |
58 | + | |
59 | + public void onItemClick(AdapterView<?> parent, View view, int position, long id) | |
60 | + { | |
61 | + startUserDetailActivity(id); | |
62 | + } | |
63 | + | |
64 | + @Override | |
65 | + public boolean onCreateOptionsMenu(Menu menu) | |
66 | + { | |
67 | + getMenuInflater().inflate(R.menu.user_list_activity_option_menu, menu); | |
68 | + return true; | |
69 | + } | |
70 | + | |
71 | + @Override | |
72 | + public boolean onOptionsItemSelected(MenuItem item) | |
73 | + { | |
74 | + boolean result = true; | |
75 | + switch (item.getItemId()) | |
76 | + { | |
77 | + case R.id.add_user_menuitem: | |
78 | + startUserDetailActivity(); | |
79 | + break; | |
80 | + default: | |
81 | + result = false; | |
82 | + } | |
83 | + return result; | |
84 | + } | |
85 | + | |
86 | + @Override | |
87 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
88 | + { | |
89 | + if (requestCode == REQUEST_USER_DETAIL_EDIT) | |
90 | + { | |
91 | + reload(); | |
92 | + } | |
93 | + } | |
94 | + | |
95 | + @Override | |
96 | + protected void onCreate(Bundle savedInstanceState) | |
97 | + { | |
98 | + super.onCreate(savedInstanceState); | |
99 | + setContentView(R.layout.user_list_activity); | |
100 | + | |
101 | + // Init data | |
102 | + UserDBHelper userDBHelper = new UserDBHelper(this); | |
103 | + db = userDBHelper.getReadableDatabase(); | |
104 | + cursor = db.query(UserDBConstants.TABLE_NAME, | |
105 | + null, null, null, null, null, | |
106 | + UserDBConstants.Columns.ID + " ASC"); | |
107 | + | |
108 | + // Init ListAdapter | |
109 | + listAdapter = new SimpleCursorAdapter(this, | |
110 | + android.R.layout.simple_list_item_2, null, | |
111 | + MAPPING_FROM, MAPPING_TO); | |
112 | + setListAdapter(listAdapter); | |
113 | + | |
114 | + getListView().setOnItemClickListener(this); | |
115 | + | |
116 | + reload(); | |
117 | + } | |
118 | + | |
119 | + @Override | |
120 | + protected void onDestroy() | |
121 | + { | |
122 | + if (cursor != null) | |
123 | + { | |
124 | + cursor.close(); | |
125 | + cursor = null; | |
126 | + } | |
127 | + if (db != null) | |
128 | + { | |
129 | + db.close(); | |
130 | + db = null; | |
131 | + } | |
132 | + super.onDestroy(); | |
133 | + } | |
134 | + | |
135 | + private void reload() | |
136 | + { | |
137 | + if (db != null) | |
138 | + { | |
139 | + Cursor newCursor = db.query(UserDBConstants.TABLE_NAME, | |
140 | + null, null, null, null, null, | |
141 | + UserDBConstants.Columns.ID + " ASC"); | |
142 | + swapCursor(newCursor); | |
143 | + } | |
144 | + } | |
145 | + | |
146 | + private void swapCursor(Cursor newCursor) | |
147 | + { | |
148 | + listAdapter.changeCursor(newCursor); | |
149 | + if (cursor != null) | |
150 | + { | |
151 | + cursor.close(); | |
152 | + } | |
153 | + cursor = newCursor; | |
154 | + } | |
155 | + | |
156 | + private void startUserDetailActivity(long id) | |
157 | + { | |
158 | + Intent intent = new Intent(this, UserDetailActivity.class); | |
159 | + intent.putExtra(UserDetailActivity.EXTRA_USER_ID, id); | |
160 | + startActivityForResult(intent, REQUEST_USER_DETAIL_EDIT); | |
161 | + } | |
162 | + | |
163 | + private void startUserDetailActivity() | |
164 | + { | |
165 | + startUserDetailActivity(-1); | |
166 | + } | |
167 | +} |
@@ -0,0 +1,39 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <say@users.sourceforge.jp>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor.db; | |
25 | + | |
26 | +public interface UserDBConstants | |
27 | +{ | |
28 | + | |
29 | + String TABLE_NAME = "Users"; | |
30 | + | |
31 | + interface Columns | |
32 | + { | |
33 | + | |
34 | + String ID = "_id"; | |
35 | + String NAME = "name"; | |
36 | + String EMAIL = "email"; | |
37 | + String AGE = "age"; | |
38 | + } | |
39 | +} |
@@ -0,0 +1,104 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <say@users.sourceforge.jp>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.ui_pattern_listview_detail_editor.db; | |
25 | + | |
26 | +import android.content.ContentValues; | |
27 | +import android.content.Context; | |
28 | +import android.database.sqlite.SQLiteDatabase; | |
29 | +import android.database.sqlite.SQLiteOpenHelper; | |
30 | +import android.util.Log; | |
31 | +import com.example.hello.android.ui_pattern_listview_detail_editor.R; | |
32 | +import java.util.Random; | |
33 | + | |
34 | +public class UserDBHelper extends SQLiteOpenHelper implements UserDBConstants | |
35 | +{ | |
36 | + | |
37 | + private static final String LOG_TAG = "XXX"; | |
38 | + private static final int DB_VERSION = 1; | |
39 | + private static final String DB_NAME = "UserDB"; | |
40 | + private static final String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + "(" | |
41 | + + " " + Columns.ID + " INTEGER PRIMARY KEY AUTOINCREMENT" | |
42 | + + ", " + Columns.NAME + " TEXT NOT NULL" | |
43 | + + ", " + Columns.EMAIL + " TEXT" | |
44 | + + ", " + Columns.AGE + " INTEGER NOT NULL" | |
45 | + + ");"; | |
46 | + private static final String DROP_TABLE_SQL = "DROP TABLE " + TABLE_NAME + ";"; | |
47 | + private Context context; | |
48 | + | |
49 | + public UserDBHelper(Context context) | |
50 | + { | |
51 | + super(context, DB_NAME, null, DB_VERSION); | |
52 | + this.context = context; | |
53 | + } | |
54 | + | |
55 | + @Override | |
56 | + public void onCreate(SQLiteDatabase db) | |
57 | + { | |
58 | + db.execSQL(CREATE_TABLE_SQL); | |
59 | + initializeUsers(db); | |
60 | + } | |
61 | + | |
62 | + @Override | |
63 | + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) | |
64 | + { | |
65 | + db.execSQL(DROP_TABLE_SQL); | |
66 | + onCreate(db); | |
67 | + } | |
68 | + | |
69 | + private void initializeUsers(SQLiteDatabase db) | |
70 | + { | |
71 | + Log.d(LOG_TAG, "initializeStrings() Hello"); | |
72 | + | |
73 | + ContentValues values = new ContentValues(); | |
74 | + db.beginTransaction(); | |
75 | + try | |
76 | + { | |
77 | + Random random = new Random(0); | |
78 | + String[] names = this.context.getResources().getStringArray(R.array.names); | |
79 | + for (String name : names) | |
80 | + { | |
81 | + try | |
82 | + { | |
83 | + int age = random.nextInt(10) + 15; | |
84 | + values.clear(); | |
85 | + values.put(Columns.NAME, name); | |
86 | + values.put(Columns.EMAIL, name + "@example.com"); | |
87 | + values.put(Columns.AGE, age); | |
88 | + db.insert(TABLE_NAME, null, values); | |
89 | + } | |
90 | + catch (IllegalArgumentException ex) | |
91 | + { | |
92 | + Log.e(LOG_TAG, "insert data failed.", ex); | |
93 | + } | |
94 | + } | |
95 | + db.setTransactionSuccessful(); | |
96 | + } | |
97 | + finally | |
98 | + { | |
99 | + db.endTransaction(); | |
100 | + } | |
101 | + | |
102 | + Log.d(LOG_TAG, "initializeStrings() Bye"); | |
103 | + } | |
104 | +} |