• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Android Samples


コミットメタ情報

リビジョン0d668f50272cfa92848dd40affaadd00a52ee925 (tree)
日時2014-06-14 08:52:57
作者Masahiko, SAWAI <say@user...>
コミッターMasahiko, SAWAI

ログメッセージ

Added ui-pattern-listview-crud-db module

変更サマリ

差分

--- a/pom.xml
+++ b/pom.xml
@@ -121,8 +121,9 @@
121121 <module>service-hello</module>
122122 <module>service-start-at-boottime</module>
123123
124- <module>ui-pattern-listview-detail-editor</module>
125124 <module>ui-pattern-listview-crud-array</module>
125+ <module>ui-pattern-listview-crud-db</module>
126+ <module>ui-pattern-listview-detail-editor</module>
126127
127128 <module>view-composite-hello</module>
128129 <module>view-custom-drawing-animation</module>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/AndroidManifest.xml
@@ -0,0 +1,37 @@
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_crud_db"
5+ android:versionCode="1"
6+ android:versionName="1.0"
7+>
8+ <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="19" />
9+
10+ <application
11+ android:label="@string/app_name"
12+ >
13+ <activity
14+ android:name=".UserListViewActivity"
15+ android:label="@string/app_name"
16+ >
17+ <intent-filter>
18+ <action android:name="android.intent.action.MAIN" />
19+ <category android:name="android.intent.category.LAUNCHER" />
20+ </intent-filter>
21+ </activity>
22+
23+ <activity
24+ android:name=".UserListDeleteActivity"
25+ android:label="@string/delete_users"
26+ />
27+ <activity
28+ android:name=".UserDetailViewActivity"
29+ android:label="@string/user_detail"
30+ />
31+ <activity
32+ android:name=".UserDetailEditActivity"
33+ android:label="@string/edit_user"
34+ />
35+
36+ </application>
37+</manifest>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/Makefile
@@ -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 $@
--- /dev/null
+++ b/ui-pattern-listview-crud-db/README.txt
@@ -0,0 +1,47 @@
1+
2+! ui-pattern-listview-crud-db
3+
4+ListView に表示するデータの CRUD のサンプル。
5+
6+この例では SimpleCursorAdapter を使って、SQLite データベースにある
7+データを表示、編集できるようにする。
8+
9+
10+!! 処理の流れ
11+
12+!!! データの追加(Create)
13+* 一覧画面:オプションメニューから「新規追加」を選択
14+* 一覧画面:データ入力用アクティビティを起動
15+* 編集画面:データを編集
16+* 編集画面:保存、キャンセルボタンにより終了
17+* 編集画面:保存ならばデータベースに挿入
18+* 一覧画面:ListView を更新
19+
20+!!! データの閲覧(Read)
21+* 一覧画面:データをタップ
22+* 一覧画面:データ詳細表示用アクティビティを起動
23+* 詳細画面:データを閲覧
24+* 詳細画面:バックボタンにより詳細画面を終了
25+* 一覧画面:ListView を更新
26+
27+!!! データの編集(Update)
28+* 一覧画面:編集したいデータを長押し
29+* 一覧画面:コンテキストメニューから編集を選択
30+* 編集画面:データを編集
31+* 編集画面:保存、キャンセルボタンにより編集画面を終了
32+* 編集画面:保存ならばデータベースのデータを更新
33+* 一覧画面:ListView を更新
34+
35+!!! 単一データの削除(Delete)
36+* 一覧画面:削除したいデータを長押し
37+* 一覧画面:コンテキストメニューから削除を選択
38+* 一覧画面:削除を実行
39+
40+!!! 複数データの一括削除(Delete)
41+* 一覧画面:オプションメニューから「一括削除」を選択
42+* 削除項目選択画面:削除したいものを一覧から選びチェックを付ける
43+* 削除項目選択画面:削除、キャンセルボタンにより終了
44+* 削除項目選択画面:削除ボタンが押されたらチェックの付いたものを削除
45+* 一覧画面:ListView を更新
46+
47+
--- /dev/null
+++ b/ui-pattern-listview-crud-db/pom.xml
@@ -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-crud-db</artifactId>
10+ <packaging>apk</packaging>
11+ <version>1.0.0</version>
12+ <name>UI ListView CRUD DB</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>4</platform>
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>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/layout/user_detail_edit_activity.xml
@@ -0,0 +1,84 @@
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="fill_parent"
6+ android:orientation="vertical"
7+ android:padding="4dip"
8+>
9+
10+ <ScrollView
11+ android:layout_width="fill_parent"
12+ android:layout_height="wrap_content"
13+ android:layout_weight="1"
14+ android:fillViewport="true"
15+ >
16+ <LinearLayout
17+ android:layout_width="fill_parent"
18+ android:layout_height="fill_parent"
19+ android:orientation="vertical"
20+ android:padding="4dip"
21+ >
22+ <TextView
23+ style="@style/header_text"
24+ android:text="@string/first_name"
25+ />
26+ <EditText
27+ style="@style/value_text"
28+ android:id="@+id/first_name_textview"
29+ android:inputType="textPersonName"
30+ />
31+ <TextView
32+ style="@style/header_text"
33+ android:text="@string/last_name"
34+ />
35+ <EditText
36+ style="@style/value_text"
37+ android:id="@+id/last_name_textview"
38+ android:inputType="textPersonName"
39+ />
40+ <TextView
41+ style="@style/header_text"
42+ android:text="@string/email"
43+ />
44+ <EditText
45+ style="@style/value_text"
46+ android:id="@+id/email_textview"
47+ android:inputType="textEmailAddress"
48+ />
49+ <TextView
50+ style="@style/header_text"
51+ android:text="@string/age"
52+ />
53+ <EditText
54+ style="@style/value_text"
55+ android:id="@+id/age_textview"
56+ android:inputType="number"
57+ />
58+
59+ </LinearLayout>
60+ </ScrollView>
61+
62+ <View style="@style/hline" />
63+
64+ <LinearLayout
65+ android:layout_width="fill_parent"
66+ android:layout_height="wrap_content"
67+ >
68+ <Button
69+ android:id="@+id/cancel_button"
70+ android:layout_width="0dip"
71+ android:layout_height="wrap_content"
72+ android:layout_weight="1"
73+ android:text="@android:string/cancel"
74+ />
75+ <Button
76+ android:id="@+id/save_button"
77+ android:layout_width="0dip"
78+ android:layout_height="wrap_content"
79+ android:layout_weight="1"
80+ android:text="@string/save"
81+ />
82+ </LinearLayout>
83+
84+</LinearLayout>
\ No newline at end of file
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/layout/user_detail_view_activity.xml
@@ -0,0 +1,70 @@
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="fill_parent"
6+ android:orientation="vertical"
7+ android:padding="4dip"
8+>
9+
10+ <ScrollView
11+ android:layout_width="fill_parent"
12+ android:layout_height="wrap_content"
13+ android:layout_weight="1"
14+ android:fillViewport="true"
15+ >
16+ <LinearLayout
17+ android:layout_width="fill_parent"
18+ android:layout_height="fill_parent"
19+ android:orientation="vertical"
20+ android:padding="4dip"
21+ >
22+ <TextView
23+ style="@style/header_text"
24+ android:text="@string/first_name"
25+ />
26+ <TextView
27+ style="@style/value_text"
28+ android:id="@+id/first_name_textview"
29+ />
30+ <TextView
31+ style="@style/header_text"
32+ android:text="@string/last_name"
33+ />
34+ <TextView
35+ style="@style/value_text"
36+ android:id="@+id/last_name_textview"
37+ />
38+ <TextView
39+ style="@style/header_text"
40+ android:text="@string/email"
41+ />
42+ <TextView
43+ style="@style/value_text"
44+ android:id="@+id/email_textview"
45+ />
46+ <TextView
47+ style="@style/header_text"
48+ android:text="@string/age"
49+ />
50+ <TextView
51+ style="@style/value_text"
52+ android:id="@+id/age_textview"
53+ />
54+
55+ </LinearLayout>
56+ </ScrollView>
57+
58+ <FrameLayout
59+ android:layout_width="fill_parent"
60+ android:layout_height="wrap_content"
61+ >
62+ <Button
63+ android:id="@+id/edit_button"
64+ android:layout_width="fill_parent"
65+ android:layout_height="wrap_content"
66+ android:text="@string/edit"
67+ />
68+ </FrameLayout>
69+
70+</LinearLayout>
\ No newline at end of file
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/layout/user_list_delete_activity.xml
@@ -0,0 +1,53 @@
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="fill_parent"
6+ android:orientation="vertical"
7+>
8+
9+ <FrameLayout
10+ android:layout_width="fill_parent"
11+ android:layout_height="fill_parent"
12+ android:layout_weight="1"
13+ android:padding="4dip"
14+ >
15+ <ListView
16+ android:id="@android:id/list"
17+ android:layout_width="fill_parent"
18+ android:layout_height="fill_parent"
19+ />
20+ <TextView
21+ android:id="@android:id/empty"
22+ android:layout_width="wrap_content"
23+ android:layout_height="wrap_content"
24+ android:layout_gravity="center"
25+ android:text="@string/no_data"
26+ />
27+ </FrameLayout>
28+
29+ <View style="@style/hline" />
30+
31+ <LinearLayout
32+ android:layout_width="fill_parent"
33+ android:layout_height="wrap_content"
34+ android:layout_weight="0"
35+ android:orientation="horizontal"
36+ >
37+ <Button
38+ android:id="@+id/cancel_button"
39+ android:layout_width="0dip"
40+ android:layout_height="wrap_content"
41+ android:layout_weight="1"
42+ android:text="@android:string/cancel"
43+ />
44+ <Button
45+ android:id="@+id/delete_button"
46+ android:layout_width="0dip"
47+ android:layout_height="wrap_content"
48+ android:layout_weight="1"
49+ android:text="@string/delete"
50+ />
51+ </LinearLayout>
52+
53+</LinearLayout>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/layout/user_list_item.xml
@@ -0,0 +1,8 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+ android:layout_width="fill_parent"
4+ android:layout_height="wrap_content"
5+ android:padding="4dip"
6+>
7+ <include layout="@layout/user_list_item_content" />
8+</FrameLayout>
\ No newline at end of file
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/layout/user_list_item_checkable.xml
@@ -0,0 +1,25 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<view xmlns:android="http://schemas.android.com/apk/res/android"
3+ class="say.android.widget.CheckableLinearLayout"
4+ android:layout_width="fill_parent"
5+ android:layout_height="wrap_content"
6+ android:orientation="horizontal"
7+ android:padding="4dip"
8+>
9+ <FrameLayout
10+ android:layout_width="wrap_content"
11+ android:layout_height="wrap_content"
12+ android:layout_weight="1"
13+ >
14+ <include layout="@layout/user_list_item_content" />
15+ </FrameLayout>
16+
17+ <CheckedTextView
18+ android:id="@android:id/checkbox"
19+ android:layout_width="wrap_content"
20+ android:layout_height="wrap_content"
21+ android:layout_gravity="right"
22+ android:layout_weight="0"
23+ android:checkMark="?android:attr/listChoiceIndicatorMultiple"
24+ />
25+</view>
\ No newline at end of file
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/layout/user_list_item_content.xml
@@ -0,0 +1,60 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+ android:layout_width="fill_parent"
4+ android:layout_height="wrap_content"
5+ android:orientation="vertical"
6+>
7+ <LinearLayout
8+ android:layout_width="fill_parent"
9+ android:layout_height="wrap_content"
10+ android:orientation="horizontal"
11+ android:padding="2dip"
12+ >
13+ <TextView
14+ android:id="@+id/first_name_textview"
15+ android:layout_width="0dip"
16+ android:layout_height="wrap_content"
17+ android:layout_weight="1"
18+ android:gravity="left"
19+ android:text="&lt;FIRST_NAME&gt;"
20+ android:textSize="20sp"
21+ android:textColor="?android:attr/textColorPrimary"
22+ />
23+ <TextView
24+ android:id="@+id/last_name_textview"
25+ android:layout_width="0dip"
26+ android:layout_height="wrap_content"
27+ android:layout_weight="1"
28+ android:gravity="left"
29+ android:text="&lt;LAST_NAME&gt;"
30+ android:textSize="20sp"
31+ android:textColor="?android:attr/textColorPrimary"
32+ />
33+ </LinearLayout>
34+
35+ <LinearLayout
36+ android:layout_width="fill_parent"
37+ android:layout_height="wrap_content"
38+ android:orientation="horizontal"
39+ android:padding="2dip"
40+ >
41+ <TextView
42+ android:id="@+id/email_textview"
43+ android:layout_width="wrap_content"
44+ android:layout_height="wrap_content"
45+ android:layout_weight="1"
46+ android:gravity="left"
47+ android:text="&lt;EMAIL&gt;"
48+ android:textSize="16sp"
49+ />
50+ <TextView
51+ android:id="@+id/age_textview"
52+ android:layout_width="wrap_content"
53+ android:layout_height="wrap_content"
54+ android:layout_weight="0"
55+ android:gravity="right"
56+ android:text="&lt;AGE&gt;"
57+ android:textSize="16sp"
58+ />
59+ </LinearLayout>
60+</LinearLayout>
\ No newline at end of file
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/layout/user_list_view_activity.xml
@@ -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="fill_parent"
6+ android:orientation="vertical"
7+>
8+
9+ <FrameLayout
10+ android:layout_width="fill_parent"
11+ android:layout_height="wrap_content"
12+ android:layout_weight="1"
13+ android:padding="4dip"
14+ >
15+ <ListView
16+ android:id="@android:id/list"
17+ android:layout_width="fill_parent"
18+ android:layout_height="fill_parent"
19+ />
20+ <TextView
21+ android:id="@android:id/empty"
22+ android:layout_width="wrap_content"
23+ android:layout_height="wrap_content"
24+ android:layout_gravity="center"
25+ android:text="@string/no_data"
26+ />
27+ </FrameLayout>
28+</LinearLayout>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/menu/user_list_context_menu.xml
@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+ <item
4+ android:id="@+id/edit_menuitem"
5+ android:title="@string/edit"
6+ android:icon="@android:drawable/ic_menu_edit"
7+ android:numericShortcut="1"
8+ android:alphabeticShortcut="e"
9+ />
10+ <item
11+ android:id="@+id/delete_menuitem"
12+ android:title="@string/delete"
13+ android:icon="@android:drawable/ic_menu_delete"
14+ android:numericShortcut="2"
15+ android:alphabeticShortcut="d"
16+ />
17+</menu>
\ No newline at end of file
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/menu/user_list_option_menu.xml
@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+ <item
4+ android:id="@+id/add_menuitem"
5+ android:title="@string/add"
6+ android:icon="@android:drawable/ic_menu_add"
7+ android:numericShortcut="1"
8+ android:alphabeticShortcut="a"
9+ />
10+ <item
11+ android:id="@+id/delete_menuitem"
12+ android:title="@string/delete_users"
13+ android:icon="@android:drawable/ic_menu_delete"
14+ android:numericShortcut="2"
15+ android:alphabeticShortcut="a"
16+ />
17+</menu>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/values/arrays_users.xml
@@ -0,0 +1,31 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<resources>
3+ <string-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+ </string-array>
31+</resources>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/values/strings.xml
@@ -0,0 +1,22 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<resources>
3+ <string name="app_name">List CRUD DB</string>
4+
5+ <string name="no_data">NO DATA</string>
6+ <string name="no_email">NO EMAIL</string>
7+
8+ <string name="user_list">User List</string>
9+ <string name="delete_users">Delete Users</string>
10+ <string name="user_detail">User Detail</string>
11+ <string name="edit_user">Edit User</string>
12+
13+ <string name="first_name">First name</string>
14+ <string name="last_name">Last name</string>
15+ <string name="email">E-Mail</string>
16+ <string name="age">Age</string>
17+
18+ <string name="add">Add</string>
19+ <string name="edit">Edit</string>
20+ <string name="delete">Delete</string>
21+ <string name="save">Save</string>
22+</resources>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/values/styles_action_bar.xml
@@ -0,0 +1,35 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<resources>
3+ <style name="actionbar">
4+ <item name="android:layout_width">fill_parent</item>
5+ <item name="android:layout_height">wrap_content</item>
6+ <item name="android:layout_margin">0dip</item>
7+ <item name="android:padding">4dip</item>
8+ <item name="android:background">#333</item>
9+ </style>
10+
11+ <style name="actionbar_title">
12+ <item name="android:layout_width">fill_parent</item>
13+ <item name="android:layout_height">wrap_content</item>
14+ <item name="android:layout_gravity">center</item>
15+ <item name="android:gravity">center</item>
16+ <item name="android:textSize">18sp</item>
17+ <item name="android:textColor">#FFF</item>
18+ <item name="android:textStyle">bold</item>
19+ </style>
20+
21+ <style name="actionbar_button">
22+ <item name="android:layout_width">wrap_content</item>
23+ <item name="android:layout_height">wrap_content</item>
24+ <item name="android:textSize">12sp</item>
25+ </style>
26+
27+ <style name="actionbar_button_left" parent="@style/actionbar_button" >
28+ <item name="android:layout_gravity">left</item>
29+ </style>
30+
31+ <style name="actionbar_button_right" parent="@style/actionbar_button" >
32+ <item name="android:layout_gravity">right</item>
33+ </style>
34+
35+</resources>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/values/styles_lines.xml
@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<resources>
3+ <!-- common -->
4+ <style name="dot">
5+ <item name="android:layout_width">1dip</item>
6+ <item name="android:layout_height">1dip</item>
7+ <item name="android:layout_weight">0</item>
8+ <item name="android:layout_margin">4dip</item>
9+ <item name="android:background">#333</item>
10+ </style>
11+ <style name="vline" parent="dot" >
12+ <item name="android:layout_height">fill_parent</item>
13+ </style>
14+ <style name="hline" parent="dot" >
15+ <item name="android:layout_width">fill_parent</item>
16+ </style>
17+</resources>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/res/values/styles_user_detail.xml
@@ -0,0 +1,20 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<resources>
3+ <style name="header_text">
4+ <item name="android:layout_width">fill_parent</item>
5+ <item name="android:layout_height">wrap_content</item>
6+ <item name="android:padding">4dip</item>
7+ <item name="android:gravity">left</item>
8+ <item name="android:textSize">16sp</item>
9+ <item name="android:textColor">#CCC</item>
10+ <item name="android:background">#333</item>
11+ </style>
12+ <style name="value_text">
13+ <item name="android:layout_width">fill_parent</item>
14+ <item name="android:layout_height">wrap_content</item>
15+ <item name="android:layout_margin">8dip</item>
16+ <item name="android:paddingLeft">8dip</item>
17+ <item name="android:gravity">left</item>
18+ <item name="android:textSize">20sp</item>
19+ </style>
20+</resources>
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/com/example/hello/android/ui_pattern_listview_crud_db/UserDetailEditActivity.java
@@ -0,0 +1,216 @@
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_crud_db;
25+
26+import android.app.Activity;
27+import android.content.Intent;
28+import android.database.Cursor;
29+import android.database.sqlite.SQLiteDatabase;
30+import android.os.Bundle;
31+import android.util.Log;
32+import android.view.View;
33+import android.widget.Button;
34+import android.widget.TextView;
35+import com.example.hello.android.ui_pattern_listview_crud_db.db.UserDBConstants;
36+import com.example.hello.android.ui_pattern_listview_crud_db.db.UserDBHelper;
37+
38+public class UserDetailEditActivity extends Activity
39+ implements View.OnClickListener
40+{
41+ public static final String EXTRA_ID = "ID";
42+ private static final String LOG_TAG = "XXX";
43+ private UserDBHelper dbHelper;
44+ private SQLiteDatabase db;
45+ private long dataId;
46+
47+ /** Called when the activity is first created. */
48+ @Override
49+ public void onCreate(Bundle savedInstanceState)
50+ {
51+ Log.v(LOG_TAG, "onCreate() : Hello");
52+ super.onCreate(savedInstanceState);
53+ setContentView(R.layout.user_detail_edit_activity);
54+
55+ initializeDB();
56+
57+ // init views
58+ Intent intent = getIntent();
59+ dataId = intent.getLongExtra(EXTRA_ID, -1);
60+ Log.d(LOG_TAG, "dataId => " + dataId);
61+ if (dataId >= 0)
62+ {
63+ loadData(dataId);
64+ }
65+
66+ Button saveButton = (Button) findViewById(R.id.save_button);
67+ saveButton.setOnClickListener(this);
68+
69+ Button cancelButton = (Button) findViewById(R.id.cancel_button);
70+ cancelButton.setOnClickListener(this);
71+
72+ Log.v(LOG_TAG, "onCreate() : Bye");
73+ }
74+
75+ @Override
76+ protected void onDestroy()
77+ {
78+ Log.v(LOG_TAG, "onCreate() : Hello");
79+
80+ finalizeDB();
81+ super.onDestroy();
82+
83+ Log.v(LOG_TAG, "onCreate() : Bye");
84+ }
85+
86+ public void onClick(View view)
87+ {
88+ Log.v(LOG_TAG, "onClick() : Hello");
89+
90+ int viewId = view.getId();
91+ if (viewId == R.id.save_button)
92+ {
93+ Log.d(LOG_TAG, "onClick() : Save Button is clicked.");
94+ saveUserData();
95+ setResult(Activity.RESULT_OK);
96+ finish();
97+ }
98+ else if (viewId == R.id.cancel_button)
99+ {
100+ Log.d(LOG_TAG, "onClick() : Cancel Button is clicked.");
101+ setResult(Activity.RESULT_CANCELED);
102+ finish();
103+ }
104+
105+ Log.v(LOG_TAG, "onClick() : Bye");
106+ }
107+
108+ /*
109+ @Override
110+ protected void onActivityResult(int requestCode, int resultCode, Intent data)
111+ {
112+ Log.v(LOG_TAG, "onActivityResult() : Hello");
113+ if (requestCode == REQUEST_CODE_EDIT_USER && resultCode == RESULT_OK)
114+ {
115+ Log.d(LOG_TAG, "onActivityResult() : User edit is done.");
116+ int id = data.getIntExtra(UserDetailActivity.EXTRA_ID, -1);
117+ if (id != -1)
118+ {
119+ loadData(id);
120+ }
121+ }
122+ Log.v(LOG_TAG, "onActivityResult() : Bye");
123+ }
124+ */
125+ private void initializeDB()
126+ {
127+ dbHelper = new UserDBHelper(this);
128+ db = dbHelper.getReadableDatabase();
129+ }
130+
131+ private void finalizeDB()
132+ {
133+
134+ if (db != null)
135+ {
136+ db.close();
137+ db = null;
138+ }
139+ if (dbHelper != null)
140+ {
141+ dbHelper.close();
142+ dbHelper = null;
143+ }
144+
145+ }
146+
147+ private void loadData(long id)
148+ {
149+ Log.v(LOG_TAG, "loadData() : Hello");
150+ Log.v(LOG_TAG, "loadData() : id => " + id);
151+
152+ Cursor cursor = dbHelper.selectUsersById(db, id);
153+ try
154+ {
155+ Log.d(LOG_TAG, "loadData() : cursor => " + cursor);
156+ if (cursor.moveToFirst())
157+ {
158+ String firstName = cursor.getString(cursor.getColumnIndex(UserDBConstants.Columns.FIRST_NAME));
159+ Log.d(LOG_TAG, "loadData() : firstName => " + firstName);
160+ String lastName = cursor.getString(cursor.getColumnIndex(UserDBConstants.Columns.LAST_NAME));
161+ String email = cursor.getString(cursor.getColumnIndex(UserDBConstants.Columns.EMAIL));
162+ int age = cursor.getInt(cursor.getColumnIndex(UserDBConstants.Columns.AGE));
163+
164+ TextView firstNameTextView = (TextView) findViewById(R.id.first_name_textview);
165+ TextView lastNameTextView = (TextView) findViewById(R.id.last_name_textview);
166+ TextView emailTextView = (TextView) findViewById(R.id.email_textview);
167+ TextView ageTextView = (TextView) findViewById(R.id.age_textview);
168+
169+ firstNameTextView.setText(firstName);
170+ lastNameTextView.setText(lastName);
171+ emailTextView.setText(email);
172+ if (age != -1)
173+ {
174+ ageTextView.setText(String.valueOf(age));
175+ }
176+ }
177+ }
178+ finally
179+ {
180+ cursor.close();
181+ }
182+
183+ Log.v(LOG_TAG, "loadData() : Bye");
184+ }
185+
186+ private void saveUserData()
187+ {
188+ TextView firstNameTextView = (TextView) findViewById(R.id.first_name_textview);
189+ TextView lastNameTextView = (TextView) findViewById(R.id.last_name_textview);
190+ TextView emailTextView = (TextView) findViewById(R.id.email_textview);
191+ TextView ageTextView = (TextView) findViewById(R.id.age_textview);
192+
193+ String firstName = firstNameTextView.getText().toString();
194+ String lastName = lastNameTextView.getText().toString();
195+ String email = emailTextView.getText().toString();
196+ String ageString = ageTextView.getText().toString();
197+ int age;
198+ try
199+ {
200+ age = Integer.parseInt(ageString);
201+ }
202+ catch (NumberFormatException e)
203+ {
204+ age = 0;
205+ }
206+
207+ if (dataId < 0)
208+ {
209+ dbHelper.insertUser(db, firstName, lastName, email, age);
210+ }
211+ else
212+ {
213+ dbHelper.updateUserById(db, dataId, firstName, lastName, email, age);
214+ }
215+ }
216+}
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/com/example/hello/android/ui_pattern_listview_crud_db/UserDetailViewActivity.java
@@ -0,0 +1,213 @@
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_crud_db;
25+
26+import android.app.Activity;
27+import android.content.Intent;
28+import android.database.Cursor;
29+import android.database.sqlite.SQLiteDatabase;
30+import android.net.Uri;
31+import android.os.Bundle;
32+import android.text.TextUtils;
33+import android.util.Log;
34+import android.view.View;
35+import android.widget.Button;
36+import android.widget.TextView;
37+import com.example.hello.android.ui_pattern_listview_crud_db.db.UserDBConstants;
38+import com.example.hello.android.ui_pattern_listview_crud_db.db.UserDBHelper;
39+
40+public class UserDetailViewActivity extends Activity
41+ implements View.OnClickListener, UserListConstants
42+{
43+
44+ private static final String LOG_TAG = "XXX";
45+ private UserDBHelper dbHelper;
46+ private SQLiteDatabase db;
47+ private long dataId;
48+
49+ @Override
50+ protected void onCreate(Bundle savedInstanceState)
51+ {
52+ Log.v(LOG_TAG, "onCreate() : Hello");
53+ super.onCreate(savedInstanceState);
54+ setContentView(R.layout.user_detail_view_activity);
55+
56+ // init views
57+ Intent intent = getIntent();
58+ dataId = intent.getLongExtra(EXTRA_ID, -1L);
59+ Log.d(LOG_TAG, "dataId => " + dataId);
60+ if (dataId == -1)
61+ {
62+ finish();
63+ }
64+ else
65+ {
66+ initializeDB();
67+
68+ loadData(dataId);
69+
70+ Button editButton = (Button) findViewById(R.id.edit_button);
71+ editButton.setOnClickListener(this);
72+
73+ TextView emailTextView = (TextView) findViewById(R.id.email_textview);
74+ emailTextView.setOnClickListener(this);
75+ }
76+
77+ Log.v(LOG_TAG, "onCreate() : Bye");
78+ }
79+
80+ @Override
81+ protected void onDestroy()
82+ {
83+ Log.v(LOG_TAG, "onCreate() : Hello");
84+
85+ finalizeDB();
86+ super.onDestroy();
87+
88+ Log.v(LOG_TAG, "onCreate() : Bye");
89+ }
90+
91+ @Override
92+ protected void onResume()
93+ {
94+ Log.v(LOG_TAG, "onResume() : Hello");
95+
96+ loadData(dataId);
97+ super.onResume();
98+
99+ Log.v(LOG_TAG, "onResume() : Bye");
100+ }
101+
102+ public void onClick(View view)
103+ {
104+ Log.v(LOG_TAG, "onClick() : Hello");
105+ int viewId = view.getId();
106+ if (viewId == R.id.email_textview)
107+ {
108+ Log.d(LOG_TAG, "onClick() : email_textview is clicked.");
109+ sendToEmailActivity();
110+ }
111+ else if (viewId == R.id.edit_button)
112+ {
113+ Log.d(LOG_TAG, "onClick() : edit_button is clicked. id => " + dataId);
114+ editUser();
115+ }
116+ Log.v(LOG_TAG, "onClick() : Bye");
117+ }
118+
119+ @Override
120+ protected void onActivityResult(int requestCode, int resultCode, Intent data)
121+ {
122+ Log.v(LOG_TAG, "onActivityResult() : Hello");
123+ if (requestCode == REQUEST_CODE_EDIT_USER && resultCode == RESULT_OK)
124+ {
125+ Log.d(LOG_TAG, "onActivityResult() : User edit is done.");
126+ setResult(RESULT_OK);
127+ loadData(dataId);
128+ }
129+ Log.v(LOG_TAG, "onActivityResult() : Bye");
130+ }
131+
132+ private void initializeDB()
133+ {
134+ dbHelper = new UserDBHelper(this);
135+ db = dbHelper.getReadableDatabase();
136+ }
137+
138+ private void finalizeDB()
139+ {
140+ if (db != null)
141+ {
142+ db.close();
143+ db = null;
144+ }
145+ if (dbHelper != null)
146+ {
147+ dbHelper.close();
148+ dbHelper = null;
149+ }
150+
151+ }
152+
153+ private void loadData(long id)
154+ {
155+ Log.v(LOG_TAG, "loadData() : Hello");
156+ Log.v(LOG_TAG, "loadData() : id => " + id);
157+
158+ Cursor cursor = dbHelper.selectUsersById(db, id);
159+ try
160+ {
161+ Log.d(LOG_TAG, "loadData() : cursor => " + cursor);
162+ Log.d(LOG_TAG, "loadData() : cursor.count => " + cursor.getCount());
163+ if (cursor.moveToFirst())
164+ {
165+ String firstName = cursor.getString(cursor.getColumnIndex(UserDBConstants.Columns.FIRST_NAME));
166+ Log.d(LOG_TAG, "loadData() : firstName => " + firstName);
167+ String lastName = cursor.getString(cursor.getColumnIndex(UserDBConstants.Columns.LAST_NAME));
168+ String email = cursor.getString(cursor.getColumnIndex(UserDBConstants.Columns.EMAIL));
169+ int age = cursor.getInt(cursor.getColumnIndex(UserDBConstants.Columns.AGE));
170+
171+ TextView firstNameTextView = (TextView) findViewById(R.id.first_name_textview);
172+ TextView lastNameTextView = (TextView) findViewById(R.id.last_name_textview);
173+ TextView emailTextView = (TextView) findViewById(R.id.email_textview);
174+ TextView ageTextView = (TextView) findViewById(R.id.age_textview);
175+
176+ firstNameTextView.setText(firstName);
177+ lastNameTextView.setText(lastName);
178+ emailTextView.setText(email);
179+ if (age != -1)
180+ {
181+ ageTextView.setText(String.valueOf(age));
182+ }
183+ }
184+ }
185+ finally
186+ {
187+ cursor.close();
188+ }
189+
190+ Log.v(LOG_TAG, "loadData() : Bye");
191+ }
192+
193+ private void editUser()
194+ {
195+ Intent intent = new Intent(this, UserDetailEditActivity.class);
196+ intent.setAction(Intent.ACTION_EDIT);
197+ intent.putExtra(UserDetailEditActivity.EXTRA_ID, dataId);
198+ startActivityForResult(intent, REQUEST_CODE_EDIT_USER);
199+ }
200+
201+ private void sendToEmailActivity()
202+ {
203+ TextView emailTextView = (TextView) findViewById(R.id.email_textview);
204+ CharSequence text = emailTextView.getText();
205+ if (TextUtils.isEmpty(text) == false)
206+ {
207+ Uri emailUri = Uri.parse("mailto:" + text);
208+ Log.d(LOG_TAG, "onClick() : emailUri => " + emailUri);
209+ Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri);
210+ startActivity(intent);
211+ }
212+ }
213+}
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/com/example/hello/android/ui_pattern_listview_crud_db/UserListConstants.java
@@ -0,0 +1,53 @@
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_crud_db;
25+
26+import com.example.hello.android.ui_pattern_listview_crud_db.db.UserDBConstants;
27+
28+/**
29+ *
30+ * @author sawai
31+ */
32+public interface UserListConstants
33+{
34+ String EXTRA_ID = UserListConstants.class.getPackage().getName() + ".EXTRA_ID";
35+ int REQUEST_CODE_VIEW_USER = 101;
36+ int REQUEST_CODE_EDIT_USER = 102;
37+ int REQUEST_CODE_ADD_USER = 103;
38+ int REQUEST_CODE_DELETE_USER = 104;
39+ String[] MAPPING_FROM =
40+ {
41+ UserDBConstants.Columns.FIRST_NAME,
42+ UserDBConstants.Columns.LAST_NAME,
43+ UserDBConstants.Columns.EMAIL,
44+ UserDBConstants.Columns.AGE,
45+ };
46+ int[] MAPPING_TO =
47+ {
48+ R.id.first_name_textview,
49+ R.id.last_name_textview,
50+ R.id.email_textview,
51+ R.id.age_textview,
52+ };
53+}
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/com/example/hello/android/ui_pattern_listview_crud_db/UserListDeleteActivity.java
@@ -0,0 +1,191 @@
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_crud_db;
25+
26+import android.app.ListActivity;
27+import android.database.Cursor;
28+import android.database.sqlite.SQLiteDatabase;
29+import android.os.Bundle;
30+import android.util.Log;
31+import android.util.SparseBooleanArray;
32+import android.view.View;
33+import android.widget.Button;
34+import android.widget.ListView;
35+import android.widget.SimpleCursorAdapter;
36+import com.example.hello.android.ui_pattern_listview_crud_db.db.UserDBHelper;
37+
38+public class UserListDeleteActivity extends ListActivity
39+ implements View.OnClickListener, UserListConstants
40+{
41+
42+ private static final String LOG_TAG = "XXX";
43+ // instances
44+ private SimpleCursorAdapter userListAdapter;
45+ private UserDBHelper dbHelper;
46+ private SQLiteDatabase db;
47+ private Cursor cursor;
48+
49+ @Override
50+ public void onCreate(Bundle savedInstanceState)
51+ {
52+ Log.v(LOG_TAG, "onCreate() : Hello");
53+
54+ super.onCreate(savedInstanceState);
55+ setContentView(R.layout.user_list_delete_activity);
56+
57+ initializeDb();
58+
59+ // Init ListAdapter
60+ userListAdapter = new SimpleCursorAdapter(this,
61+ R.layout.user_list_item_checkable, null, MAPPING_FROM, MAPPING_TO);
62+ setListAdapter(userListAdapter);
63+
64+ // Init ListView
65+ ListView listView = getListView();
66+ listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
67+
68+ // Init listeners
69+ Button cancelButton = (Button) findViewById(R.id.cancel_button);
70+ cancelButton.setOnClickListener(this);
71+
72+ Button deleteButton = (Button) findViewById(R.id.delete_button);
73+ deleteButton.setOnClickListener(this);
74+
75+ Log.v(LOG_TAG, "onCreate() : Bye");
76+ }
77+
78+ @Override
79+ protected void onDestroy()
80+ {
81+ Log.v(LOG_TAG, "onDestroy() : Hello");
82+ finalizeDb();
83+ super.onDestroy();
84+ Log.v(LOG_TAG, "onDestroy() : Bye");
85+ }
86+
87+ @Override
88+ protected void onResume()
89+ {
90+ Log.v(LOG_TAG, "onResume() : Hello");
91+ updateListData();
92+ super.onResume();
93+ Log.v(LOG_TAG, "onResume() : Bye");
94+ }
95+
96+ public void onClick(View view)
97+ {
98+ Log.v(LOG_TAG, "onClick() : Hello");
99+
100+ int viewId = view.getId();
101+ switch (viewId)
102+ {
103+ case R.id.cancel_button:
104+ Log.d(LOG_TAG, "Cancel Button is clicked.");
105+ setResult(RESULT_CANCELED);
106+ finish();
107+ break;
108+ case R.id.delete_button:
109+ Log.d(LOG_TAG, "Delete Button is clicked.");
110+ deleteCheckedUsers();
111+ setResult(RESULT_OK);
112+ finish();
113+ break;
114+ }
115+
116+ Log.v(LOG_TAG, "onClick() : Bye");
117+ }
118+
119+ private void initializeDb()
120+ {
121+ Log.v(LOG_TAG, "Hello");
122+ if (dbHelper == null)
123+ {
124+ dbHelper = new UserDBHelper(this);
125+ }
126+
127+ db = dbHelper.getReadableDatabase();
128+
129+ Log.v(LOG_TAG, "Bye");
130+ }
131+
132+ private void finalizeDb()
133+ {
134+ Log.v(LOG_TAG, "Hello");
135+ if (cursor != null)
136+ {
137+ cursor.close();
138+ cursor = null;
139+ }
140+ if (db != null)
141+ {
142+ db.close();
143+ db = null;
144+ }
145+ Log.v(LOG_TAG, "Bye");
146+ }
147+
148+ private void swapCursor(Cursor newCursor)
149+ {
150+ userListAdapter.changeCursor(newCursor);
151+ if (cursor != null)
152+ {
153+ cursor.close();
154+ }
155+ cursor = newCursor;
156+ }
157+
158+ private void updateListData()
159+ {
160+ Log.v(LOG_TAG, "updateListData() Hello");
161+
162+ if (db != null)
163+ {
164+ Log.d(LOG_TAG, "updateListData() : change new cursor!");
165+ Cursor newCursor = dbHelper.getAllUsers(db);
166+ swapCursor(newCursor);
167+ }
168+
169+ Log.v(LOG_TAG, "updateListData() Bye");
170+ }
171+
172+ private void deleteCheckedUsers()
173+ {
174+ Log.v(LOG_TAG, "deleteSelectedUsers() : Hello");
175+
176+ ListView listView = getListView();
177+ SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
178+
179+ for (int i = 0; i < checkedItemPositions.size(); i++)
180+ {
181+ if (checkedItemPositions.valueAt(i))
182+ {
183+ int position = checkedItemPositions.keyAt(i);
184+ long id = listView.getItemIdAtPosition(position);
185+ dbHelper.deleteUserById(db, id);
186+ }
187+ }
188+
189+ Log.v(LOG_TAG, "deleteSelectedUsers() : Bye");
190+ }
191+}
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/com/example/hello/android/ui_pattern_listview_crud_db/UserListViewActivity.java
@@ -0,0 +1,313 @@
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_crud_db;
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.util.Log;
32+import android.view.ContextMenu;
33+import android.view.ContextMenu.ContextMenuInfo;
34+import android.view.MenuInflater;
35+import android.view.MenuItem;
36+import android.view.Menu;
37+import android.view.View;
38+import android.widget.AdapterView;
39+import android.widget.AdapterView.AdapterContextMenuInfo;
40+import android.widget.AdapterView.OnItemClickListener;
41+import android.widget.ListView;
42+import android.widget.SimpleCursorAdapter;
43+import com.example.hello.android.ui_pattern_listview_crud_db.db.UserDBHelper;
44+
45+public class UserListViewActivity extends ListActivity
46+ implements OnItemClickListener, UserListConstants
47+{
48+
49+ private static final String LOG_TAG = "XXX";
50+ // instances
51+ private SimpleCursorAdapter userListAdapter;
52+ private UserDBHelper dbHelper;
53+ private SQLiteDatabase db;
54+ private Cursor cursor;
55+
56+ @Override
57+ protected void onCreate(Bundle savedInstanceState)
58+ {
59+ Log.v(LOG_TAG, "onCreate() : Hello");
60+
61+ super.onCreate(savedInstanceState);
62+ setContentView(R.layout.user_list_view_activity);
63+
64+ initializeDb();
65+
66+ // Init ListAdapter
67+ userListAdapter = new SimpleCursorAdapter(this,
68+ R.layout.user_list_item, null, MAPPING_FROM, MAPPING_TO);
69+ setListAdapter(userListAdapter);
70+
71+ // Init ListView
72+ ListView listView = getListView();
73+ listView.setOnItemClickListener(this);
74+ registerForContextMenu(listView);
75+
76+ Log.v(LOG_TAG, "onCreate() : Bye");
77+ }
78+
79+ @Override
80+ protected void onDestroy()
81+ {
82+ Log.v(LOG_TAG, "onDestroy() : Hello");
83+ finalizeDb();
84+ super.onDestroy();
85+ Log.v(LOG_TAG, "onDestroy() : Bye");
86+ }
87+
88+ @Override
89+ protected void onResume()
90+ {
91+ Log.v(LOG_TAG, "onResume() : Hello");
92+ super.onResume();
93+ updateListData();
94+ Log.v(LOG_TAG, "onResume() : Bye");
95+ }
96+
97+ @Override
98+ protected void onActivityResult(int requestCode, int resultCode, Intent data)
99+ {
100+ Log.v(LOG_TAG, "onActivityResult() : Hello");
101+ Log.d(LOG_TAG, "onActivityResult() : requestCode => " + requestCode);
102+ Log.d(LOG_TAG, "onActivityResult() : resultCode => " + resultCode);
103+
104+ switch (requestCode)
105+ {
106+ case REQUEST_CODE_ADD_USER:
107+ Log.d(LOG_TAG, "onActivityResult() : Add new user is done.");
108+ if (resultCode == RESULT_OK)
109+ {
110+ updateListData();
111+ }
112+ break;
113+ case REQUEST_CODE_EDIT_USER:
114+ Log.d(LOG_TAG, "onActivityResult() : Edit user is done.");
115+ if (resultCode == RESULT_OK)
116+ {
117+ Log.d(LOG_TAG, "onActivityResult() : RESULT_OK");
118+ updateListData();
119+ }
120+ break;
121+ case REQUEST_CODE_VIEW_USER:
122+ Log.d(LOG_TAG, "onActivityResult() : View user is done.");
123+ if (resultCode == RESULT_OK)
124+ {
125+ updateListData();
126+ }
127+ break;
128+ case REQUEST_CODE_DELETE_USER:
129+ Log.d(LOG_TAG, "onActivityResult() : Delete user is done.");
130+ if (resultCode == RESULT_OK)
131+ {
132+ updateListData();
133+ }
134+ break;
135+ }
136+
137+ Log.v(LOG_TAG, "onActivityResult() : Bye");
138+ }
139+
140+ @Override
141+ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
142+ {
143+ Log.v(LOG_TAG, "onCreateContextMenu() : Hello");
144+ super.onCreateContextMenu(menu, v, menuInfo);
145+
146+ MenuInflater menuInflater = getMenuInflater();
147+ menuInflater.inflate(R.menu.user_list_context_menu, menu);
148+
149+ Log.v(LOG_TAG, "onCreateContextMenu() : Bye");
150+ }
151+
152+ @Override
153+ public boolean onContextItemSelected(MenuItem item)
154+ {
155+ boolean result;
156+
157+ AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
158+ Log.v(LOG_TAG, "onCreateContextMenu() : list item position => " + menuInfo.position);
159+ Log.v(LOG_TAG, "onCreateContextMenu() : list item id => " + menuInfo.id);
160+ Log.v(LOG_TAG, "onCreateContextMenu() : list item targetView => " + menuInfo.targetView);
161+
162+ int itemId = item.getItemId();
163+ switch (itemId)
164+ {
165+ case R.id.edit_menuitem:
166+ editUserById(menuInfo.id);
167+ result = true;
168+ break;
169+ case R.id.delete_menuitem:
170+ deleteUserById(menuInfo.id);
171+ result = true;
172+ break;
173+ default:
174+ result = super.onContextItemSelected(item);
175+ }
176+
177+ return result;
178+ }
179+
180+ public void onItemClick(AdapterView<?> parentView, View view, int position, long id)
181+ {
182+ Log.v(LOG_TAG, "onItemClick() : Hello");
183+ Log.d(LOG_TAG, "onItemClick() : position => " + position + ", id => " + id);
184+
185+ viewUserDetailById(id);
186+
187+ Log.v(LOG_TAG, "onItemClick() : Bye");
188+ }
189+
190+ @Override
191+ public boolean onCreateOptionsMenu(Menu menu)
192+ {
193+ MenuInflater menuInflater = getMenuInflater();
194+ menuInflater.inflate(R.menu.user_list_option_menu, menu);
195+ return true;
196+ }
197+
198+ @Override
199+ public boolean onOptionsItemSelected(MenuItem item)
200+ {
201+ Log.v(LOG_TAG, "onOptionsItemSelected() : Hello");
202+ boolean result = true;
203+
204+ int itemId = item.getItemId();
205+ switch (itemId)
206+ {
207+ case R.id.add_menuitem:
208+ Log.d(LOG_TAG, "onOptionsItemSelected() : Add menu item selected.");
209+ addNewUser();
210+ break;
211+ case R.id.delete_menuitem:
212+ Log.d(LOG_TAG, "onOptionsItemSelected() : Delete menu item selected.");
213+ deleteUsers();
214+ break;
215+ default:
216+ result = super.onOptionsItemSelected(item);
217+ }
218+
219+ Log.v(LOG_TAG, "onOptionsItemSelected() : Bye");
220+ return result;
221+ }
222+
223+ private void initializeDb()
224+ {
225+ Log.v(LOG_TAG, "Hello");
226+ if (dbHelper == null)
227+ {
228+ dbHelper = new UserDBHelper(this);
229+ }
230+
231+ db = dbHelper.getReadableDatabase();
232+
233+ Log.v(LOG_TAG, "Bye");
234+ }
235+
236+ private void finalizeDb()
237+ {
238+ Log.v(LOG_TAG, "Hello");
239+ if (cursor != null)
240+ {
241+ cursor.close();
242+ cursor = null;
243+ }
244+ if (db != null)
245+ {
246+ db.close();
247+ db = null;
248+ }
249+ Log.v(LOG_TAG, "Bye");
250+ }
251+
252+ private void swapCursor(Cursor newCursor)
253+ {
254+ userListAdapter.changeCursor(newCursor);
255+ if (cursor != null)
256+ {
257+ cursor.close();
258+ }
259+ cursor = newCursor;
260+ }
261+
262+ private void updateListData()
263+ {
264+ Log.v(LOG_TAG, "updateListData() Hello");
265+
266+ if (db != null)
267+ {
268+ Log.d(LOG_TAG, "updateListData() : change new cursor!");
269+ Cursor newCursor = dbHelper.getAllUsers(db);
270+ swapCursor(newCursor);
271+ }
272+
273+ Log.v(LOG_TAG, "updateListData() Bye");
274+ }
275+
276+ private void addNewUser()
277+ {
278+ Intent intent = new Intent(this, UserDetailEditActivity.class);
279+ intent.setAction(Intent.ACTION_EDIT);
280+ startActivityForResult(intent, REQUEST_CODE_ADD_USER);
281+ }
282+
283+ private void viewUserDetailById(long id)
284+ {
285+ Intent intent = new Intent(this, UserDetailViewActivity.class);
286+ intent.setAction(Intent.ACTION_VIEW);
287+ intent.putExtra(UserDetailViewActivity.EXTRA_ID, id);
288+ startActivityForResult(intent, REQUEST_CODE_VIEW_USER);
289+ }
290+
291+ private void editUserById(long id)
292+ {
293+ Intent intent = new Intent(this, UserDetailEditActivity.class);
294+ intent.setAction(Intent.ACTION_EDIT);
295+ intent.putExtra(UserDetailEditActivity.EXTRA_ID, id);
296+ startActivityForResult(intent, REQUEST_CODE_EDIT_USER);
297+ }
298+
299+ private void deleteUserById(long id)
300+ {
301+ int count = dbHelper.deleteUserById(db, id);
302+ if (count > 0)
303+ {
304+ updateListData();
305+ }
306+ }
307+
308+ private void deleteUsers()
309+ {
310+ Intent intent = new Intent(this, UserListDeleteActivity.class);
311+ startActivityForResult(intent, REQUEST_CODE_DELETE_USER);
312+ }
313+}
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/com/example/hello/android/ui_pattern_listview_crud_db/db/UserDBConstants.java
@@ -0,0 +1,48 @@
1+/*
2+ * The MIT License
3+ *
4+ * Copyright 2011 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_crud_db.db;
25+
26+public interface UserDBConstants
27+{
28+
29+ String TABLE_NAME = "Users";
30+ String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + "("
31+ + " " + Columns.ID + " INTEGER PRIMARY KEY AUTOINCREMENT"
32+ + ", " + Columns.FIRST_NAME + " TEXT"
33+ + ", " + Columns.LAST_NAME + " TEXT"
34+ + ", " + Columns.EMAIL + " TEXT"
35+ + ", " + Columns.AGE + " INTEGER"
36+ + ");";
37+ String DROP_TABLE_SQL = "DROP TABLE " + TABLE_NAME + ";";
38+
39+ interface Columns
40+ {
41+
42+ String ID = "_id";
43+ String FIRST_NAME = "first_name";
44+ String LAST_NAME = "last_name";
45+ String EMAIL = "email";
46+ String AGE = "age";
47+ }
48+}
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/com/example/hello/android/ui_pattern_listview_crud_db/db/UserDBHelper.java
@@ -0,0 +1,203 @@
1+/*
2+ * The MIT License
3+ *
4+ * Copyright 2011 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_crud_db.db;
25+
26+import android.content.ContentValues;
27+import android.content.Context;
28+import android.database.Cursor;
29+import android.database.sqlite.SQLiteDatabase;
30+import android.database.sqlite.SQLiteOpenHelper;
31+import android.util.Log;
32+import com.example.hello.android.ui_pattern_listview_crud_db.R;
33+import java.util.Random;
34+
35+public class UserDBHelper extends SQLiteOpenHelper implements UserDBConstants
36+{
37+
38+ private static final String LOG_TAG = "XXX";
39+ static final int DB_VERSION = 1;
40+ static final String DB_NAME = "Users";
41+ private Context context;
42+
43+ public UserDBHelper(Context context)
44+ {
45+ // create database on main memory
46+ super(context, DB_NAME, null, DB_VERSION);
47+ this.context = context;
48+ }
49+
50+ @Override
51+ public void onCreate(SQLiteDatabase db)
52+ {
53+ db.execSQL(CREATE_TABLE_SQL);
54+ initializeUsers(db);
55+ }
56+
57+ @Override
58+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
59+ {
60+ db.execSQL(DROP_TABLE_SQL);
61+ onCreate(db);
62+ }
63+
64+ public Cursor selectUsersById(SQLiteDatabase db, long id)
65+ {
66+ Cursor cursor = db.query(TABLE_NAME,
67+ new String[]
68+ {
69+ Columns.ID,
70+ Columns.FIRST_NAME,
71+ Columns.LAST_NAME,
72+ Columns.EMAIL,
73+ Columns.AGE,
74+ },
75+ Columns.ID + " = ?",
76+ new String[]
77+ {
78+ String.valueOf(id)
79+ },
80+ null, null, null);
81+
82+ return cursor;
83+ }
84+
85+ public Cursor selectUsersByName(SQLiteDatabase db, String firstName)
86+ {
87+ Cursor cursor = db.query(TABLE_NAME,
88+ new String[]
89+ {
90+ Columns.ID,
91+ Columns.FIRST_NAME,
92+ Columns.LAST_NAME,
93+ Columns.EMAIL,
94+ Columns.AGE,
95+ },
96+ Columns.FIRST_NAME + " like ?",
97+ new String[]
98+ {
99+ '%' + firstName + '%'
100+ },
101+ null, null, Columns.FIRST_NAME + " ASC");
102+
103+ return cursor;
104+ }
105+
106+ public Cursor getAllUsers(SQLiteDatabase db)
107+ {
108+ Cursor cursor = db.query(TABLE_NAME,
109+ new String[]
110+ {
111+ Columns.ID,
112+ Columns.FIRST_NAME,
113+ Columns.LAST_NAME,
114+ Columns.FIRST_NAME,
115+ Columns.EMAIL,
116+ Columns.AGE,
117+ },
118+ null, null,
119+ null, null, Columns.FIRST_NAME + " ASC");
120+
121+ return cursor;
122+ }
123+
124+ public void insertUser(SQLiteDatabase db,
125+ String firstName, String lastName,
126+ String email, int age)
127+ {
128+ ContentValues values = new ContentValues();
129+ values.clear();
130+ values.put(Columns.FIRST_NAME, firstName);
131+ values.put(Columns.LAST_NAME, lastName);
132+ values.put(Columns.EMAIL, email);
133+ values.put(Columns.AGE, age);
134+ db.insert(TABLE_NAME, null, values);
135+ }
136+
137+ public void updateUserById(SQLiteDatabase db, long id,
138+ String firstName, String lastName,
139+ String email, int age)
140+ {
141+ ContentValues values = new ContentValues();
142+ values.clear();
143+ values.put(Columns.FIRST_NAME, firstName);
144+ values.put(Columns.LAST_NAME, lastName);
145+ values.put(Columns.EMAIL, email);
146+ values.put(Columns.AGE, age);
147+ db.update(TABLE_NAME, values,
148+ Columns.ID + " = ? ",
149+ new String[]
150+ {
151+ String.valueOf(id)
152+ });
153+ }
154+
155+ public int deleteUserById(SQLiteDatabase db, long id)
156+ {
157+ int deletedCount = db.delete(TABLE_NAME,
158+ Columns.ID + " = ? ",
159+ new String[]
160+ {
161+ String.valueOf(id)
162+ });
163+ return deletedCount;
164+ }
165+
166+ private void initializeUsers(SQLiteDatabase db)
167+ {
168+ Log.d(LOG_TAG, "initializeUsers() Hello");
169+
170+ ContentValues values = new ContentValues();
171+ db.beginTransaction();
172+ try
173+ {
174+ String[] names = this.context.getResources().getStringArray(R.array.names);
175+ Random rand = new Random(0);
176+ for (String firstName : names)
177+ {
178+ try
179+ {
180+ int age = 15 + rand.nextInt(10); // 15 - 24
181+ values.clear();
182+ values.put(Columns.FIRST_NAME, firstName);
183+ values.put(Columns.LAST_NAME, "Smith");
184+ values.put(Columns.EMAIL, firstName.toLowerCase() + "@example.com");
185+ values.put(Columns.AGE, age);
186+
187+ db.insert(TABLE_NAME, null, values);
188+ }
189+ catch (IllegalArgumentException ex)
190+ {
191+ Log.e(LOG_TAG, "insert data failed.", ex);
192+ }
193+ }
194+ db.setTransactionSuccessful();
195+ }
196+ finally
197+ {
198+ db.endTransaction();
199+ }
200+
201+ Log.d(LOG_TAG, "initializeUsers() Bye");
202+ }
203+}
--- /dev/null
+++ b/ui-pattern-listview-crud-db/src/say/android/widget/CheckableLinearLayout.java
@@ -0,0 +1,64 @@
1+/*
2+ * To change this template, choose Tools | Templates
3+ * and open the template in the editor.
4+ */
5+package say.android.widget;
6+
7+import android.content.Context;
8+import android.util.AttributeSet;
9+import android.view.View;
10+import android.widget.Checkable;
11+import android.widget.LinearLayout;
12+
13+/**
14+ *
15+ * @author sawai
16+ */
17+public class CheckableLinearLayout extends LinearLayout implements Checkable
18+{
19+
20+ private static final String LOG_TAG = "XXX";
21+ private Checkable checkableView;
22+
23+ public CheckableLinearLayout(Context context, AttributeSet attrs)
24+ {
25+ super(context, attrs);
26+ }
27+
28+ public CheckableLinearLayout(Context context)
29+ {
30+ super(context);
31+ }
32+
33+ public Checkable getCheckableView()
34+ {
35+ if (checkableView == null)
36+ {
37+ View view = this.findViewById(android.R.id.checkbox);
38+ if (view instanceof Checkable)
39+ {
40+ checkableView = (Checkable) view;
41+ }
42+ else
43+ {
44+ throw new IllegalStateException("No Checkable View");
45+ }
46+ }
47+ return checkableView;
48+ }
49+
50+ public void toggle()
51+ {
52+ getCheckableView().toggle();
53+ }
54+
55+ public void setChecked(boolean checked)
56+ {
57+ getCheckableView().setChecked(checked);
58+ }
59+
60+ public boolean isChecked()
61+ {
62+ return getCheckableView().isChecked();
63+ }
64+}