質問編集履歴

4

文言修正

2018/03/24 12:58

投稿

rick
rick

スコア18

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
 
15
- (xml内ではRelativeLayout(横)内にImageView、TextView、TextView、LinearLayoutを定義し、LinearLayout内ボタンを定義しております。
15
+ item_list.xmlというアイテム用のレイアウトファイルを別途作成しそれをactivity_main.xmlのlistViewにしてます。
16
16
 
17
17
  アプリ一覧のリストは、listViewとArrayAdapterを使用して実装しております。)
18
18
 

3

item_list.xmlというアイテム用のレイアウトファイルを別途作成

2018/03/24 12:58

投稿

rick
rick

スコア18

test CHANGED
File without changes
test CHANGED
@@ -26,270 +26,192 @@
26
26
 
27
27
 
28
28
 
29
+ @Override
30
+
31
+ protected void onCreate (Bundle bundle) {
32
+
33
+ super.onCreate (bundle);
34
+
35
+ requestWindowFeature (Window.FEATURE_NO_TITLE);
36
+
37
+ setContentView (R.layout.activity_main);
38
+
39
+
40
+
41
+ // Get installed app info from device.
42
+
43
+ PackageManager packageManager = getPackageManager();
44
+
45
+ final List<ApplicationInfo> installedAppList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
46
+
47
+
48
+
49
+ // Stack data to list.
50
+
51
+ final List<AppData> appList = new ArrayList<AppData>();
52
+
53
+ for (ApplicationInfo app : installedAppList) {
54
+
55
+ AppData data = new AppData();
56
+
57
+ data.label = app.loadLabel(packageManager).toString();
58
+
59
+ data.icon = app.loadIcon(packageManager);
60
+
61
+ data.name = app.packageName;
62
+
63
+ appList.add (data);
64
+
65
+ }
66
+
67
+
68
+
69
+ // Show app list to listview.
70
+
71
+ ListView listView = new ListView(this);
72
+
73
+ listView = (ListView) findViewById(R.id.card_list);
74
+
75
+
76
+
77
+ ViewGroup parentView = (ViewGroup) listView.getParent();
78
+
79
+ if (parentView != null) {
80
+
81
+ parentView.removeView(listView);
82
+
83
+ }
84
+
85
+ listView.setAdapter(new ApplicationListAdapter(this, appList));
86
+
87
+
88
+
89
+ setContentView(listView);
90
+
91
+ }
92
+
93
+
94
+
95
+ private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
96
+
97
+ //
98
+
99
+ private final LayoutInflater mInflater;
100
+
101
+
102
+
103
+ public ApplicationListAdapter (Context context, List<AppData> dataList) {
104
+
105
+ //super(context, R.layout.activity_main);
106
+
107
+ super(context, 0);
108
+
109
+ mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
110
+
111
+ addAll(dataList);
112
+
113
+ }
114
+
115
+
116
+
117
+ @Override
118
+
119
+ public View getView (int position, View convertView, ViewGroup parent) {
120
+
121
+ ViewHolder holder = new ViewHolder();
122
+
123
+
124
+
125
+ if (convertView == null) {
126
+
127
+ convertView = mInflater.inflate(R.layout.item_list, parent, false);
128
+
129
+ holder.textLabel = (TextView) convertView.findViewById(R.id.label);
130
+
131
+ holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
132
+
133
+ holder.packageName = (TextView) convertView.findViewById(R.id.name);
134
+
135
+
136
+
137
+ ViewGroup parentView = (ViewGroup) convertView.getParent();
138
+
139
+ if (parentView != null) {
140
+
141
+ parentView.removeView(convertView);
142
+
143
+ }
144
+
145
+ convertView.setTag(holder);
146
+
147
+ } else {
148
+
149
+ holder = (ViewHolder) convertView.getTag();
150
+
151
+ }
152
+
153
+
154
+
155
+ // Get data to show.
156
+
157
+ final AppData data = getItem(position);
158
+
159
+ // Set label & icon to listview.
160
+
161
+ holder.textLabel.setText(data.label);
162
+
163
+ holder.imageIcon.setImageDrawable(data.icon);
164
+
165
+ holder.packageName.setText(data.name);
166
+
167
+
168
+
169
+ return convertView;
170
+
171
+ }
172
+
173
+ }
174
+
29
175
  //
30
176
 
31
- private ArrayList<String> items = null;
177
+ final AppData data = getItem(position);
32
178
 
33
179
  //
34
180
 
35
- private ApplicationListAdapter adapter = null;
36
-
37
-
38
-
39
- @Override
40
-
41
- protected void onCreate (Bundle bundle) {
42
-
43
- super.onCreate (bundle);
44
-
45
- requestWindowFeature (Window.FEATURE_NO_TITLE);
46
-
47
-
48
-
49
- setContentView (R.layout.activity_main);
181
+ holder.textLabel.setText(data.label);
182
+
50
-
183
+ holder.imageIcon.setImageDrawable(data.icon);
184
+
185
+ holder.packageName.setText(data.name);
186
+
187
+
188
+
51
- // create String Arraylist.
189
+ // private class for storing application data.
52
-
53
- List<AppData> appList = new ArrayList<AppData>();
190
+
54
-
55
- // create PackageManager.
56
-
57
- PackageManager packageManager = getPackageManager();
58
-
59
- // make application list in your device has already installed.
60
-
61
- final List<ApplicationInfo> installedAppList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
62
-
63
-
64
-
65
- for (ApplicationInfo app : installedAppList) {
66
-
67
- AppData data = new AppData();
191
+ private static class AppData {
68
-
69
- data.label = app.loadLabel(packageManager).toString();
192
+
70
-
71
- data.icon = app.loadIcon(packageManager);
72
-
73
- data.name = app.packageName;
74
-
75
- appList.add (data);
193
+ String label;
76
-
77
- }
194
+
78
-
79
-
80
-
81
- final ListView listView = new ListView(this);
82
-
83
- adapter = new ApplicationListAdapter(this, appList);
84
-
85
- listView.setAdapter(adapter);
195
+ Drawable icon;
86
-
87
-
88
-
196
+
89
- setContentView(listView);
197
+ String name;
90
198
 
91
199
  }
92
200
 
93
201
 
94
202
 
95
- // private Adapter Class indicates label and icon of application.
203
+ // private class ViewHolder.
96
-
97
- private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
204
+
98
-
99
- //
100
-
101
- private final LayoutInflater mInflater;
205
+ private static class ViewHolder {
102
-
103
-
104
-
105
- public ApplicationListAdapter (Context context, List<AppData> dataList) {
206
+
106
-
107
- super(context, R.layout.activity_main);
108
-
109
- mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
110
-
111
- addAll(dataList);
207
+ TextView textLabel;
208
+
209
+ ImageView imageIcon;
210
+
211
+ TextView packageName;
112
212
 
113
213
  }
114
214
 
115
-
116
-
117
- @Override
118
-
119
- public View getView (int position, View convertView, ViewGroup parent) {
120
-
121
- ViewHolder holder = new ViewHolder();
122
-
123
-
124
-
125
- if (convertView == null) {
126
-
127
- convertView = mInflater.inflate(R.layout.activity_main, parent, false);
128
-
129
- holder.textLabel = (TextView) convertView.findViewById(R.id.label);
130
-
131
- holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
132
-
133
- holder.packageName = (TextView) convertView.findViewById(R.id.name);
134
-
135
- convertView.setTag(holder);
136
-
137
- } else {
138
-
139
- holder = (ViewHolder) convertView.getTag();
140
-
141
- }
142
-
143
-
144
-
145
- //
146
-
147
- final AppData data = getItem(position);
148
-
149
- //
150
-
151
- holder.textLabel.setText(data.label);
152
-
153
- holder.imageIcon.setImageDrawable(data.icon);
154
-
155
- holder.packageName.setText(data.name);
156
-
157
-
158
-
159
- return convertView;
160
-
161
- }
162
-
163
- }
164
-
165
-
166
-
167
- // private Adapter Class indicates label and icon of application.
168
-
169
- private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
170
-
171
- //
172
-
173
- private final LayoutInflater mInflater;
174
-
175
-
176
-
177
- public ApplicationListAdapter (Context context, List<AppData> dataList) {
178
-
179
- super(context, R.layout.activity_main);
180
-
181
- mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
182
-
183
- addAll(dataList);
184
-
185
- }
186
-
187
-
188
-
189
- @Override
190
-
191
- public View getView (int position, View convertView, ViewGroup parent) {
192
-
193
- ViewHolder holder = new ViewHolder();
194
-
195
-
196
-
197
- if (convertView == null) {
198
-
199
- convertView = mInflater.inflate(R.layout.activity_main, parent, false);
200
-
201
- holder.textLabel = (TextView) convertView.findViewById(R.id.label);
202
-
203
- holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
204
-
205
- holder.packageName = (TextView) convertView.findViewById(R.id.name);
206
-
207
- convertView.setTag(holder);
208
-
209
- } else {
210
-
211
- holder = (ViewHolder) convertView.getTag();
212
-
213
- }
214
-
215
-
216
-
217
- //
218
-
219
- final AppData data = getItem(position);
220
-
221
- //
222
-
223
- holder.textLabel.setText(data.label);
224
-
225
- holder.imageIcon.setImageDrawable(data.icon);
226
-
227
- holder.packageName.setText(data.name);
228
-
229
-
230
-
231
- return convertView;
232
-
233
- }
234
-
235
- }
236
-
237
-
238
-
239
- // private class for storing application data.
240
-
241
- private static class AppData {
242
-
243
- String label;
244
-
245
- Drawable icon;
246
-
247
- String name;
248
-
249
- }
250
-
251
-
252
-
253
- // private class ViewHolder.
254
-
255
- private static class ViewHolder {
256
-
257
- TextView textLabel;
258
-
259
- ImageView imageIcon;
260
-
261
- TextView packageName;
262
-
263
- }
264
-
265
-
266
-
267
- // private class for storing application data.
268
-
269
- private static class AppData {
270
-
271
- String label;
272
-
273
- Drawable icon;
274
-
275
- String name;
276
-
277
- }
278
-
279
-
280
-
281
- // private class ViewHolder.
282
-
283
- private static class ViewHolder {
284
-
285
- TextView textLabel;
286
-
287
- ImageView imageIcon;
288
-
289
- TextView packageName;
290
-
291
- }
292
-
293
215
  ```
294
216
 
295
217
 
@@ -300,6 +222,54 @@
300
222
 
301
223
  <?xml version="1.0" encoding="utf-8"?>
302
224
 
225
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
226
+
227
+ android:layout_width="match_parent"
228
+
229
+ android:layout_height="match_parent"
230
+
231
+ android:orientation="vertical">
232
+
233
+
234
+
235
+ <Button
236
+
237
+ android:id="@+id/button"
238
+
239
+ android:layout_width="wrap_content"
240
+
241
+ android:layout_height="50dp"
242
+
243
+ android:gravity="center"
244
+
245
+ android:text="Button" />
246
+
247
+
248
+
249
+ <ListView
250
+
251
+ android:id="@+id/card_list"
252
+
253
+ android:layout_width="match_parent"
254
+
255
+ android:layout_height="match_parent">
256
+
257
+ </ListView>
258
+
259
+
260
+
261
+ </LinearLayout>
262
+
263
+ ```
264
+
265
+
266
+
267
+ item_list.xml
268
+
269
+ ```
270
+
271
+ <?xml version="1.0" encoding="utf-8"?>
272
+
303
273
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
304
274
 
305
275
  android:layout_width="match_parent"
@@ -318,11 +288,7 @@
318
288
 
319
289
  android:layout_height="50dp"
320
290
 
321
- android:layout_alignParentLeft="true"
322
-
323
- android:layout_alignParentTop="true"
324
-
325
- android:layout_weight="1"/>
291
+ android:layout_below="@+id/button" />
326
292
 
327
293
 
328
294
 
@@ -334,15 +300,11 @@
334
300
 
335
301
  android:layout_height="wrap_content"
336
302
 
337
- android:layout_alignParentRight="true"
338
-
339
- android:layout_alignParentTop="true"
303
+ android:layout_below="@+id/button"
340
304
 
341
305
  android:layout_toRightOf="@+id/icon"
342
306
 
343
- android:textSize="18sp"
307
+ android:textSize="18sp" />
344
-
345
- android:layout_weight="1"/>
346
308
 
347
309
 
348
310
 
@@ -356,45 +318,7 @@
356
318
 
357
319
  android:layout_alignLeft="@+id/label"
358
320
 
359
- android:layout_alignParentRight="true"
360
-
361
- android:layout_below="@+id/label"
321
+ android:layout_below="@+id/label" />
362
-
363
- android:layout_weight="1" />
364
-
365
-
366
-
367
- <LinearLayout
368
-
369
- android:layout_width="match_parent"
370
-
371
- android:layout_height="50dp"
372
-
373
- android:layout_alignParentBottom="true"
374
-
375
- android:layout_alignParentStart="true"
376
-
377
- android:orientation="vertical">
378
-
379
-
380
-
381
- <Button
382
-
383
- android:id="@+id/button4"
384
-
385
- android:layout_width="match_parent"
386
-
387
- android:layout_height="wrap_content"
388
-
389
- android:layout_alignParentBottom="true"
390
-
391
- android:layout_alignParentEnd="true"
392
-
393
- android:layout_weight="1"
394
-
395
- android:text="Button" />
396
-
397
- </LinearLayout>
398
322
 
399
323
 
400
324
 

2

コード体裁修正(```使用)

2018/03/24 12:54

投稿

rick
rick

スコア18

test CHANGED
File without changes
test CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  ### LancherApp.java
22
22
 
23
+ ```
24
+
23
25
  public class LancherApp extends Activity {
24
26
 
25
27
 
@@ -162,6 +164,78 @@
162
164
 
163
165
 
164
166
 
167
+ // private Adapter Class indicates label and icon of application.
168
+
169
+ private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
170
+
171
+ //
172
+
173
+ private final LayoutInflater mInflater;
174
+
175
+
176
+
177
+ public ApplicationListAdapter (Context context, List<AppData> dataList) {
178
+
179
+ super(context, R.layout.activity_main);
180
+
181
+ mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
182
+
183
+ addAll(dataList);
184
+
185
+ }
186
+
187
+
188
+
189
+ @Override
190
+
191
+ public View getView (int position, View convertView, ViewGroup parent) {
192
+
193
+ ViewHolder holder = new ViewHolder();
194
+
195
+
196
+
197
+ if (convertView == null) {
198
+
199
+ convertView = mInflater.inflate(R.layout.activity_main, parent, false);
200
+
201
+ holder.textLabel = (TextView) convertView.findViewById(R.id.label);
202
+
203
+ holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
204
+
205
+ holder.packageName = (TextView) convertView.findViewById(R.id.name);
206
+
207
+ convertView.setTag(holder);
208
+
209
+ } else {
210
+
211
+ holder = (ViewHolder) convertView.getTag();
212
+
213
+ }
214
+
215
+
216
+
217
+ //
218
+
219
+ final AppData data = getItem(position);
220
+
221
+ //
222
+
223
+ holder.textLabel.setText(data.label);
224
+
225
+ holder.imageIcon.setImageDrawable(data.icon);
226
+
227
+ holder.packageName.setText(data.name);
228
+
229
+
230
+
231
+ return convertView;
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
165
239
  // private class for storing application data.
166
240
 
167
241
  private static class AppData {
@@ -190,10 +264,40 @@
190
264
 
191
265
 
192
266
 
267
+ // private class for storing application data.
268
+
269
+ private static class AppData {
270
+
271
+ String label;
272
+
273
+ Drawable icon;
274
+
275
+ String name;
276
+
277
+ }
278
+
279
+
280
+
281
+ // private class ViewHolder.
282
+
283
+ private static class ViewHolder {
284
+
285
+ TextView textLabel;
286
+
287
+ ImageView imageIcon;
288
+
289
+ TextView packageName;
290
+
291
+ }
292
+
293
+ ```
294
+
193
295
 
194
296
 
195
297
  ### activity_main.xml
196
298
 
299
+ ```
300
+
197
301
  <?xml version="1.0" encoding="utf-8"?>
198
302
 
199
303
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
@@ -295,3 +399,5 @@
295
399
 
296
400
 
297
401
  </RelativeLayout>
402
+
403
+ ```

1

ApplicationListAdapterのコードが抜けていたため追加

2018/03/23 12:46

投稿

rick
rick

スコア18

test CHANGED
File without changes
test CHANGED
@@ -90,6 +90,108 @@
90
90
 
91
91
 
92
92
 
93
+ // private Adapter Class indicates label and icon of application.
94
+
95
+ private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
96
+
97
+ //
98
+
99
+ private final LayoutInflater mInflater;
100
+
101
+
102
+
103
+ public ApplicationListAdapter (Context context, List<AppData> dataList) {
104
+
105
+ super(context, R.layout.activity_main);
106
+
107
+ mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
108
+
109
+ addAll(dataList);
110
+
111
+ }
112
+
113
+
114
+
115
+ @Override
116
+
117
+ public View getView (int position, View convertView, ViewGroup parent) {
118
+
119
+ ViewHolder holder = new ViewHolder();
120
+
121
+
122
+
123
+ if (convertView == null) {
124
+
125
+ convertView = mInflater.inflate(R.layout.activity_main, parent, false);
126
+
127
+ holder.textLabel = (TextView) convertView.findViewById(R.id.label);
128
+
129
+ holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
130
+
131
+ holder.packageName = (TextView) convertView.findViewById(R.id.name);
132
+
133
+ convertView.setTag(holder);
134
+
135
+ } else {
136
+
137
+ holder = (ViewHolder) convertView.getTag();
138
+
139
+ }
140
+
141
+
142
+
143
+ //
144
+
145
+ final AppData data = getItem(position);
146
+
147
+ //
148
+
149
+ holder.textLabel.setText(data.label);
150
+
151
+ holder.imageIcon.setImageDrawable(data.icon);
152
+
153
+ holder.packageName.setText(data.name);
154
+
155
+
156
+
157
+ return convertView;
158
+
159
+ }
160
+
161
+ }
162
+
163
+
164
+
165
+ // private class for storing application data.
166
+
167
+ private static class AppData {
168
+
169
+ String label;
170
+
171
+ Drawable icon;
172
+
173
+ String name;
174
+
175
+ }
176
+
177
+
178
+
179
+ // private class ViewHolder.
180
+
181
+ private static class ViewHolder {
182
+
183
+ TextView textLabel;
184
+
185
+ ImageView imageIcon;
186
+
187
+ TextView packageName;
188
+
189
+ }
190
+
191
+
192
+
193
+
194
+
93
195
  ### activity_main.xml
94
196
 
95
197
  <?xml version="1.0" encoding="utf-8"?>