Android Samples
Revision | 347c0da6419aaa1080e84c59cd0a8b5e3f15de79 (tree) |
---|---|
Zeit | 2014-06-07 05:46:44 |
Autor | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
Added listview-array-adapter-custom-class module
@@ -12,12 +12,19 @@ package : | ||
12 | 12 | install : |
13 | 13 | mvn $@ |
14 | 14 | |
15 | +deploy : | |
16 | + mvn package android:deploy | |
17 | + | |
18 | +undeploy : | |
19 | + mvn android:undeploy | |
20 | + | |
15 | 21 | rebuild : |
16 | 22 | mvn clean package |
17 | 23 | |
18 | 24 | redeploy : |
19 | 25 | mvn clean package android:deploy |
20 | 26 | |
27 | + | |
21 | 28 | #################### document |
22 | 29 | |
23 | 30 | site : |
@@ -0,0 +1,26 @@ | ||
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.listview_array_adapter_custom_class" | |
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="UserEditActivity" | |
23 | + android:label="@string/user_edit_label" | |
24 | + /> | |
25 | + </application> | |
26 | +</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,14 @@ | ||
1 | +! listview-array-adapter-custom-class | |
2 | + | |
3 | +ArrayAdapter を継承して独自クラスの配列データを ListView で表示するサンプル。 | |
4 | + | |
5 | +ArrayAdapter をそのまま利用した場合、オブジェクトの文字列表現を | |
6 | +1つの TextView に表示することしかできない。 | |
7 | +オブジェクトの複数のプロパティを個別のビューに割り当てて表示するためには | |
8 | +ArrayAdapter を継承して独自の ListAdapter を作成する必要がある。 | |
9 | + | |
10 | +独自の ListAdapter を作るには以下のメソッドをオーバーライドする。 | |
11 | + | |
12 | + * ArrayAdapter#getView(int position, View convertView, ViewGroup parent) : View | |
13 | + | |
14 | + |
@@ -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>listview-array-adapter-custom-class</artifactId> | |
10 | + <packaging>apk</packaging> | |
11 | + <version>1.0.0</version> | |
12 | + <name>ListView ArrayAdapter Custom Class</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> |
@@ -0,0 +1,122 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:orientation="vertical" | |
5 | + android:layout_width="fill_parent" | |
6 | + android:layout_height="fill_parent" | |
7 | + android:padding="4dip" | |
8 | +> | |
9 | + <TableLayout | |
10 | + android:layout_width="fill_parent" | |
11 | + android:layout_height="wrap_content" | |
12 | + android:layout_weight="1" | |
13 | + android:layout_margin="4dip" | |
14 | + android:stretchColumns="2" | |
15 | + > | |
16 | + <TableRow | |
17 | + android:layout_width="fill_parent" | |
18 | + android:layout_height="wrap_content" | |
19 | + > | |
20 | + <TextView | |
21 | + android:layout_width="fill_parent" | |
22 | + android:layout_height="wrap_content" | |
23 | + android:gravity="right" | |
24 | + android:text="@string/first_name_label" | |
25 | + /> | |
26 | + <EditText | |
27 | + android:id="@+id/first_name_edittext" | |
28 | + android:layout_width="fill_parent" | |
29 | + android:layout_height="wrap_content" | |
30 | + android:layout_weight="1" | |
31 | + android:text="" | |
32 | + android:inputType="text" | |
33 | + android:hint="@string/first_name_hint" | |
34 | + /> | |
35 | + </TableRow> | |
36 | + <TableRow | |
37 | + android:layout_width="fill_parent" | |
38 | + android:layout_height="wrap_content" | |
39 | + > | |
40 | + <TextView | |
41 | + android:layout_width="fill_parent" | |
42 | + android:layout_height="wrap_content" | |
43 | + android:gravity="right" | |
44 | + android:text="@string/last_name_label" | |
45 | + /> | |
46 | + <EditText | |
47 | + android:id="@+id/last_name_edittext" | |
48 | + android:layout_width="fill_parent" | |
49 | + android:layout_height="wrap_content" | |
50 | + android:layout_weight="1" | |
51 | + android:text="" | |
52 | + android:inputType="text" | |
53 | + android:hint="@string/last_name_hint" | |
54 | + /> | |
55 | + </TableRow> | |
56 | + <TableRow | |
57 | + android:layout_width="fill_parent" | |
58 | + android:layout_height="wrap_content" | |
59 | + > | |
60 | + <TextView | |
61 | + android:layout_width="fill_parent" | |
62 | + android:layout_height="wrap_content" | |
63 | + android:text="@string/age_label" | |
64 | + android:gravity="right" | |
65 | + /> | |
66 | + <EditText | |
67 | + android:id="@+id/age_edittext" | |
68 | + android:layout_width="fill_parent" | |
69 | + android:layout_height="wrap_content" | |
70 | + android:layout_weight="1" | |
71 | + android:text="" | |
72 | + android:inputType="numberDecimal" | |
73 | + android:hint="@string/age_hint" | |
74 | + /> | |
75 | + </TableRow> | |
76 | + <TableRow | |
77 | + android:layout_width="fill_parent" | |
78 | + android:layout_height="wrap_content" | |
79 | + > | |
80 | + <TextView | |
81 | + android:layout_width="fill_parent" | |
82 | + android:layout_height="wrap_content" | |
83 | + android:gravity="right" | |
84 | + android:text="@string/email_label" | |
85 | + /> | |
86 | + <EditText | |
87 | + android:id="@+id/email_edittext" | |
88 | + android:layout_width="fill_parent" | |
89 | + android:layout_height="wrap_content" | |
90 | + android:layout_weight="1" | |
91 | + android:text="" | |
92 | + android:inputType="textEmailAddress" | |
93 | + android:hint="@string/email_hint" | |
94 | + /> | |
95 | + </TableRow> | |
96 | + </TableLayout> | |
97 | + | |
98 | + <LinearLayout | |
99 | + android:orientation="horizontal" | |
100 | + android:layout_width="fill_parent" | |
101 | + android:layout_height="wrap_content" | |
102 | + android:layout_weight="0" | |
103 | + android:background="#333" | |
104 | + android:padding="4dip" | |
105 | + > | |
106 | + <Button | |
107 | + android:id="@+id/cancel_button" | |
108 | + android:layout_width="0dip" | |
109 | + android:layout_height="wrap_content" | |
110 | + android:layout_weight="1" | |
111 | + android:text="@string/cancel" | |
112 | + /> | |
113 | + <Button | |
114 | + android:id="@+id/save_button" | |
115 | + android:layout_width="0dip" | |
116 | + android:layout_height="wrap_content" | |
117 | + android:layout_weight="1" | |
118 | + android:text="@string/save" | |
119 | + /> | |
120 | + </LinearLayout> | |
121 | + | |
122 | +</LinearLayout> | |
\ No newline at end of file |
@@ -0,0 +1,42 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:orientation="vertical" | |
5 | + android:layout_width="fill_parent" | |
6 | + android:layout_height="fill_parent" | |
7 | +> | |
8 | + <FrameLayout | |
9 | + android:layout_width="fill_parent" | |
10 | + android:layout_height="wrap_content" | |
11 | + android:layout_weight="1" | |
12 | + > | |
13 | + <ListView | |
14 | + android:id="@android:id/list" | |
15 | + android:layout_width="fill_parent" | |
16 | + android:layout_height="fill_parent" | |
17 | + /> | |
18 | + <TextView | |
19 | + android:id="@android:id/empty" | |
20 | + android:layout_width="wrap_content" | |
21 | + android:layout_height="wrap_content" | |
22 | + android:layout_gravity="center" | |
23 | + android:text="@string/empty_message" | |
24 | + /> | |
25 | + </FrameLayout> | |
26 | + <FrameLayout | |
27 | + android:orientation="horizontal" | |
28 | + android:layout_width="fill_parent" | |
29 | + android:layout_height="wrap_content" | |
30 | + android:layout_weight="0" | |
31 | + android:background="#333" | |
32 | + android:padding="4dip" | |
33 | + > | |
34 | + <Button | |
35 | + android:id="@+id/add_button" | |
36 | + android:layout_width="fill_parent" | |
37 | + android:layout_height="wrap_content" | |
38 | + android:layout_weight="0" | |
39 | + android:text="@string/add" | |
40 | + /> | |
41 | + </FrameLayout> | |
42 | +</LinearLayout> | |
\ No newline at end of file |
@@ -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:paddingLeft="10sp" | |
6 | + android:paddingRight="10sp" | |
7 | + android:orientation="vertical" | |
8 | +> | |
9 | + <LinearLayout | |
10 | + android:layout_width="fill_parent" | |
11 | + android:layout_height="wrap_content" | |
12 | + android:orientation="horizontal" | |
13 | + > | |
14 | + <TextView | |
15 | + android:id="@+id/first_name_textview" | |
16 | + android:layout_width="fill_parent" | |
17 | + android:layout_height="wrap_content" | |
18 | + android:layout_weight="1" | |
19 | + android:textSize="18sp" | |
20 | + android:text="FIRSTNAME" | |
21 | + /> | |
22 | + <TextView | |
23 | + android:id="@+id/last_name_textview" | |
24 | + android:layout_width="fill_parent" | |
25 | + android:layout_height="wrap_content" | |
26 | + android:layout_weight="1" | |
27 | + android:textSize="18sp" | |
28 | + android:text="LASTNAME" | |
29 | + /> | |
30 | + <TextView | |
31 | + android:layout_width="wrap_content" | |
32 | + android:layout_height="wrap_content" | |
33 | + android:text="@string/age_label" | |
34 | + /> | |
35 | + <TextView | |
36 | + android:id="@+id/age_textview" | |
37 | + android:layout_width="wrap_content" | |
38 | + android:layout_height="wrap_content" | |
39 | + android:text="AGE" | |
40 | + /> | |
41 | + </LinearLayout> | |
42 | + <LinearLayout | |
43 | + android:layout_width="fill_parent" | |
44 | + android:layout_height="wrap_content" | |
45 | + android:orientation="horizontal" | |
46 | + > | |
47 | + <TextView | |
48 | + android:layout_width="wrap_content" | |
49 | + android:layout_height="wrap_content" | |
50 | + android:paddingLeft="4sp" | |
51 | + android:text="@string/email_label" | |
52 | + /> | |
53 | + <TextView | |
54 | + android:id="@+id/email_textview" | |
55 | + android:layout_width="wrap_content" | |
56 | + android:layout_height="wrap_content" | |
57 | + android:text="EMAIL" | |
58 | + /> | |
59 | + </LinearLayout> | |
60 | +</LinearLayout> | |
\ No newline at end of file |
@@ -0,0 +1,21 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <string name="app_name">ListView ArrayAdapter Custom Class</string> | |
4 | + <string name="user_list_label">User List</string> | |
5 | + <string name="add">Add</string> | |
6 | + <string name="empty_message">NO DATA</string> | |
7 | + <string name="age_label">Age : </string> | |
8 | + <string name="email_label">Email : </string> | |
9 | + <string name="email_none"><NO EMAIL></string> | |
10 | + | |
11 | + <!-- user_edit.xml --> | |
12 | + <string name="user_edit_label">Edit User</string> | |
13 | + <string name="cancel">Cancel</string> | |
14 | + <string name="save">Save</string> | |
15 | + <string name="first_name_label">First Name : </string> | |
16 | + <string name="first_name_hint"><FIRST NAME></string> | |
17 | + <string name="last_name_label">Last Name : </string> | |
18 | + <string name="last_name_hint"><LAST NAME></string> | |
19 | + <string name="age_hint"><AGE></string> | |
20 | + <string name="email_hint"><EMAIL ADDRESS></string> | |
21 | +</resources> |
@@ -0,0 +1,73 @@ | ||
1 | +/* | |
2 | + * To change this template, choose Tools | Templates | |
3 | + * and open the template in the editor. | |
4 | + */ | |
5 | +package com.example.hello.android.listview_array_adapter_custom_class; | |
6 | + | |
7 | +import java.io.Serializable; | |
8 | +import java.util.Random; | |
9 | + | |
10 | +/** | |
11 | + * | |
12 | + * @author sawai | |
13 | + */ | |
14 | +public class User implements Serializable | |
15 | +{ | |
16 | + private String firstName; | |
17 | + private String lastName; | |
18 | + private String email; | |
19 | + private int age; | |
20 | + | |
21 | + public User(String firstName, String lastName, int age) | |
22 | + { | |
23 | + this.firstName = firstName; | |
24 | + this.lastName = lastName; | |
25 | + this.age = age; | |
26 | + } | |
27 | + | |
28 | + public User() | |
29 | + { | |
30 | + this("", "", 0); | |
31 | + } | |
32 | + | |
33 | + public int getAge() | |
34 | + { | |
35 | + return age; | |
36 | + } | |
37 | + | |
38 | + public void setAge(int age) | |
39 | + { | |
40 | + this.age = age; | |
41 | + } | |
42 | + | |
43 | + public String getFirstName() | |
44 | + { | |
45 | + return firstName; | |
46 | + } | |
47 | + | |
48 | + public void setFirstName(String firstName) | |
49 | + { | |
50 | + this.firstName = firstName; | |
51 | + } | |
52 | + | |
53 | + public String getLastName() | |
54 | + { | |
55 | + return lastName; | |
56 | + } | |
57 | + | |
58 | + public void setLastName(String lastName) | |
59 | + { | |
60 | + this.lastName = lastName; | |
61 | + } | |
62 | + | |
63 | + public String getEmail() | |
64 | + { | |
65 | + return email; | |
66 | + } | |
67 | + | |
68 | + public void setEmail(String email) | |
69 | + { | |
70 | + this.email = email; | |
71 | + } | |
72 | + | |
73 | +} |
@@ -0,0 +1,112 @@ | ||
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.listview_array_adapter_custom_class; | |
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.widget.Button; | |
32 | +import android.widget.EditText; | |
33 | + | |
34 | +public class UserEditActivity extends Activity | |
35 | + implements OnClickListener | |
36 | +{ | |
37 | + | |
38 | + public static final String EXTRA_USER = "User"; | |
39 | + | |
40 | + @Override | |
41 | + public void onCreate(Bundle savedInstanceState) | |
42 | + { | |
43 | + super.onCreate(savedInstanceState); | |
44 | + setContentView(R.layout.user_edit_activity); | |
45 | + | |
46 | + Button cancelButton = (Button) findViewById(R.id.cancel_button); | |
47 | + cancelButton.setOnClickListener(new OnClickListener() | |
48 | + { | |
49 | + public void onClick(View view) | |
50 | + { | |
51 | + setResult(RESULT_CANCELED); | |
52 | + finish(); | |
53 | + } | |
54 | + }); | |
55 | + | |
56 | + Button saveButton = (Button) findViewById(R.id.save_button); | |
57 | + saveButton.setOnClickListener(new OnClickListener() | |
58 | + { | |
59 | + public void onClick(View view) | |
60 | + { | |
61 | + Intent intent = new Intent(); | |
62 | + intent.putExtra(EXTRA_USER, getUser()); | |
63 | + setResult(RESULT_OK, intent); | |
64 | + finish(); | |
65 | + } | |
66 | + }); | |
67 | + } | |
68 | + | |
69 | + private User getUser() | |
70 | + { | |
71 | + User user = new User(); | |
72 | + | |
73 | + EditText firstNameEditText = (EditText) findViewById(R.id.first_name_edittext); | |
74 | + user.setFirstName(firstNameEditText.getText().toString()); | |
75 | + | |
76 | + EditText lastNameEditText = (EditText) findViewById(R.id.last_name_edittext); | |
77 | + user.setLastName(lastNameEditText.getText().toString()); | |
78 | + | |
79 | + EditText ageEditText = (EditText) findViewById(R.id.age_edittext); | |
80 | + int age = 0; | |
81 | + try | |
82 | + { | |
83 | + age = Integer.parseInt(ageEditText.getText().toString()); | |
84 | + } | |
85 | + catch (NumberFormatException e) | |
86 | + { | |
87 | + } | |
88 | + user.setAge(age); | |
89 | + | |
90 | + EditText emailEditText = (EditText) findViewById(R.id.email_edittext); | |
91 | + user.setEmail(emailEditText.getText().toString()); | |
92 | + | |
93 | + return user; | |
94 | + } | |
95 | + | |
96 | + public void onClick(View view) | |
97 | + { | |
98 | + switch (view.getId()) | |
99 | + { | |
100 | + case R.id.cancel_button: | |
101 | + setResult(RESULT_CANCELED); | |
102 | + finish(); | |
103 | + break; | |
104 | + case R.id.save_button: | |
105 | + Intent intent = new Intent(); | |
106 | + intent.putExtra(EXTRA_USER, getUser()); | |
107 | + setResult(RESULT_OK, intent); | |
108 | + finish(); | |
109 | + break; | |
110 | + } | |
111 | + } | |
112 | +} |
@@ -0,0 +1,84 @@ | ||
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.listview_array_adapter_custom_class; | |
25 | + | |
26 | +import android.app.ListActivity; | |
27 | +import android.content.Intent; | |
28 | +import android.os.Bundle; | |
29 | +import android.view.View; | |
30 | +import android.view.View.OnClickListener; | |
31 | +import android.widget.Button; | |
32 | +import java.util.ArrayList; | |
33 | +import java.util.List; | |
34 | + | |
35 | +public class UserListActivity extends ListActivity | |
36 | + implements OnClickListener | |
37 | +{ | |
38 | + | |
39 | + private static final int USER_EDIT_ACTIVITY = 1; | |
40 | + private UserListAdapter userListAdapter; | |
41 | + private List<User> userList = new ArrayList<User>(); | |
42 | + | |
43 | + @Override | |
44 | + public void onCreate(Bundle savedInstanceState) | |
45 | + { | |
46 | + super.onCreate(savedInstanceState); | |
47 | + setContentView(R.layout.user_list_activity); | |
48 | + | |
49 | + // Init data list | |
50 | + userList = new ArrayList<User>(); | |
51 | + | |
52 | + // Init list adapter | |
53 | + userListAdapter = new UserListAdapter(this, userList); | |
54 | + setListAdapter(userListAdapter); | |
55 | + | |
56 | + Button addButton = (Button) findViewById(R.id.add_button); | |
57 | + addButton.setOnClickListener(this); | |
58 | + } | |
59 | + | |
60 | + @Override | |
61 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
62 | + { | |
63 | + if (requestCode == USER_EDIT_ACTIVITY) | |
64 | + { | |
65 | + if (resultCode == RESULT_OK) | |
66 | + { | |
67 | + User user = (User) data.getSerializableExtra(UserEditActivity.EXTRA_USER); | |
68 | + userList.add(user); | |
69 | + userListAdapter.notifyDataSetChanged(); | |
70 | + } | |
71 | + } | |
72 | + } | |
73 | + | |
74 | + public void onClick(View view) | |
75 | + { | |
76 | + switch (view.getId()) | |
77 | + { | |
78 | + case R.id.add_button: | |
79 | + Intent intent = new Intent(getApplicationContext(), UserEditActivity.class); | |
80 | + startActivityForResult(intent, USER_EDIT_ACTIVITY); | |
81 | + break; | |
82 | + } | |
83 | + } | |
84 | +} |
@@ -0,0 +1,84 @@ | ||
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.listview_array_adapter_custom_class; | |
25 | + | |
26 | +import android.content.Context; | |
27 | +import android.view.LayoutInflater; | |
28 | +import android.view.View; | |
29 | +import android.view.ViewGroup; | |
30 | +import android.widget.ArrayAdapter; | |
31 | +import android.widget.TextView; | |
32 | +import java.util.List; | |
33 | + | |
34 | +public class UserListAdapter extends ArrayAdapter<User> | |
35 | +{ | |
36 | + | |
37 | + private LayoutInflater inflater; | |
38 | + | |
39 | + public UserListAdapter(Context context, List<User> userList) | |
40 | + { | |
41 | + super(context, R.layout.user_list_item, userList); | |
42 | + inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
43 | + } | |
44 | + | |
45 | + @Override | |
46 | + public View getView(int position, View convertView, ViewGroup parent) | |
47 | + { | |
48 | + View view; | |
49 | + | |
50 | + if (convertView == null) | |
51 | + { | |
52 | + view = inflater.inflate(R.layout.user_list_item, null); | |
53 | + } | |
54 | + else | |
55 | + { | |
56 | + view = convertView; | |
57 | + } | |
58 | + | |
59 | + User user = getItem(position); | |
60 | + if (user != null) | |
61 | + { | |
62 | + TextView firstNameTextView = (TextView) view.findViewById(R.id.first_name_textview); | |
63 | + firstNameTextView.setText(user.getFirstName()); | |
64 | + | |
65 | + TextView lastNameTextView = (TextView) view.findViewById(R.id.last_name_textview); | |
66 | + lastNameTextView.setText(user.getLastName()); | |
67 | + | |
68 | + TextView ageTextView = (TextView) view.findViewById(R.id.age_textview); | |
69 | + ageTextView.setText(String.valueOf(user.getAge())); | |
70 | + | |
71 | + String email = user.getEmail(); | |
72 | + TextView emailTextView = (TextView) view.findViewById(R.id.email_textview); | |
73 | + if (email != null) | |
74 | + { | |
75 | + emailTextView.setText(email); | |
76 | + } | |
77 | + else | |
78 | + { | |
79 | + emailTextView.setText(R.string.email_none); | |
80 | + } | |
81 | + } | |
82 | + return view; | |
83 | + } | |
84 | +} |
@@ -79,6 +79,7 @@ | ||
79 | 79 | <module>layout-tablelayout-hello</module> |
80 | 80 | <module>layout-tablelayout-stripe-style</module> |
81 | 81 | <module>listview-array-adapter</module> |
82 | + <module>listview-array-adapter-custom-class</module> | |
82 | 83 | <module>listview-custom-listselector</module> |
83 | 84 | <module>listview-custom-listselector-theme</module> |
84 | 85 | <module>listview-fast-scroll</module> |