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

質問編集履歴

4

文言修正

2018/03/24 12:58

投稿

rick
rick

スコア18

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,7 @@
5
5
  (リスト一覧の部品の一部としてボタンが扱われているように見えます。)
6
6
  リスト一覧とは別に、最下部にボタンを配置する方法はありますでしょうか?3日くらい悩んでいるので、助けて頂ければ嬉しいです。
7
7
 
8
- (xml内では、RelativeLayout(横)内にImageView、TextView、TextView、LinearLayout定義し、LinearLayout内にボタン定義しております。
8
+ item_list.xmlというアイテム用のレイアウトファイル別途作成し、それactivity_main.xmlのlistViewに渡してます。
9
9
  アプリ一覧のリストは、listViewとArrayAdapterを使用して実装しております。)
10
10
 
11
11
  ### LancherApp.java

3

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

2018/03/24 12:58

投稿

rick
rick

スコア18

title CHANGED
File without changes
body CHANGED
@@ -12,111 +12,86 @@
12
12
  ```
13
13
  public class LancherApp extends Activity {
14
14
 
15
- //
15
+ @Override
16
- private ArrayList<String> items = null;
17
- //
18
- private ApplicationListAdapter adapter = null;
16
+ protected void onCreate (Bundle bundle) {
17
+ super.onCreate (bundle);
18
+ requestWindowFeature (Window.FEATURE_NO_TITLE);
19
+ setContentView (R.layout.activity_main);
19
20
 
20
- @Override
21
- protected void onCreate (Bundle bundle) {
21
+ // Get installed app info from device.
22
- super.onCreate (bundle);
23
- requestWindowFeature (Window.FEATURE_NO_TITLE);
22
+ PackageManager packageManager = getPackageManager();
23
+ final List<ApplicationInfo> installedAppList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
24
24
 
25
- setContentView (R.layout.activity_main);
26
- // create String Arraylist.
25
+ // Stack data to list.
27
- List<AppData> appList = new ArrayList<AppData>();
26
+ final List<AppData> appList = new ArrayList<AppData>();
27
+ for (ApplicationInfo app : installedAppList) {
28
+ AppData data = new AppData();
28
- // create PackageManager.
29
+ data.label = app.loadLabel(packageManager).toString();
29
- PackageManager packageManager = getPackageManager();
30
+ data.icon = app.loadIcon(packageManager);
30
- // make application list in your device has already installed.
31
+ data.name = app.packageName;
31
- final List<ApplicationInfo> installedAppList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
32
+ appList.add (data);
33
+ }
32
34
 
33
- for (ApplicationInfo app : installedAppList) {
35
+ // Show app list to listview.
34
- AppData data = new AppData();
36
+ ListView listView = new ListView(this);
35
- data.label = app.loadLabel(packageManager).toString();
37
+ listView = (ListView) findViewById(R.id.card_list);
36
- data.icon = app.loadIcon(packageManager);
37
- data.name = app.packageName;
38
- appList.add (data);
39
- }
40
38
 
41
- final ListView listView = new ListView(this);
39
+ ViewGroup parentView = (ViewGroup) listView.getParent();
40
+ if (parentView != null) {
41
+ parentView.removeView(listView);
42
+ }
42
- adapter = new ApplicationListAdapter(this, appList);
43
+ listView.setAdapter(new ApplicationListAdapter(this, appList));
43
- listView.setAdapter(adapter);
44
44
 
45
- setContentView(listView);
45
+ setContentView(listView);
46
46
  }
47
47
 
48
- // private Adapter Class indicates label and icon of application.
49
- private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
48
+ private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
50
- //
49
+ //
51
- private final LayoutInflater mInflater;
50
+ private final LayoutInflater mInflater;
52
51
 
53
- public ApplicationListAdapter (Context context, List<AppData> dataList) {
52
+ public ApplicationListAdapter (Context context, List<AppData> dataList) {
54
- super(context, R.layout.activity_main);
53
+ //super(context, R.layout.activity_main);
54
+ super(context, 0);
55
- mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
55
+ mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
56
- addAll(dataList);
56
+ addAll(dataList);
57
- }
57
+ }
58
58
 
59
- @Override
59
+ @Override
60
- public View getView (int position, View convertView, ViewGroup parent) {
60
+ public View getView (int position, View convertView, ViewGroup parent) {
61
- ViewHolder holder = new ViewHolder();
61
+ ViewHolder holder = new ViewHolder();
62
62
 
63
- if (convertView == null) {
63
+ if (convertView == null) {
64
- convertView = mInflater.inflate(R.layout.activity_main, parent, false);
64
+ convertView = mInflater.inflate(R.layout.item_list, parent, false);
65
- holder.textLabel = (TextView) convertView.findViewById(R.id.label);
65
+ holder.textLabel = (TextView) convertView.findViewById(R.id.label);
66
- holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
66
+ holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
67
- holder.packageName = (TextView) convertView.findViewById(R.id.name);
67
+ holder.packageName = (TextView) convertView.findViewById(R.id.name);
68
- convertView.setTag(holder);
69
- } else {
70
- holder = (ViewHolder) convertView.getTag();
71
- }
72
68
 
69
+ ViewGroup parentView = (ViewGroup) convertView.getParent();
73
- //
70
+ if (parentView != null) {
74
- final AppData data = getItem(position);
71
+ parentView.removeView(convertView);
75
- //
72
+ }
76
- holder.textLabel.setText(data.label);
73
+ convertView.setTag(holder);
77
- holder.imageIcon.setImageDrawable(data.icon);
74
+ } else {
78
- holder.packageName.setText(data.name);
75
+ holder = (ViewHolder) convertView.getTag();
76
+ }
79
77
 
78
+ // Get data to show.
79
+ final AppData data = getItem(position);
80
+ // Set label & icon to listview.
80
- return convertView;
81
+ holder.textLabel.setText(data.label);
82
+ holder.imageIcon.setImageDrawable(data.icon);
81
- }
83
+ holder.packageName.setText(data.name);
82
- }
83
84
 
84
- // private Adapter Class indicates label and icon of application.
85
- private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
85
+ return convertView;
86
+ }
87
+ }
86
88
  //
87
- private final LayoutInflater mInflater;
88
-
89
- public ApplicationListAdapter (Context context, List<AppData> dataList) {
90
- super(context, R.layout.activity_main);
91
- mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
92
- addAll(dataList);
93
- }
94
-
95
- @Override
96
- public View getView (int position, View convertView, ViewGroup parent) {
97
- ViewHolder holder = new ViewHolder();
98
-
99
- if (convertView == null) {
100
- convertView = mInflater.inflate(R.layout.activity_main, parent, false);
101
- holder.textLabel = (TextView) convertView.findViewById(R.id.label);
102
- holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
103
- holder.packageName = (TextView) convertView.findViewById(R.id.name);
104
- convertView.setTag(holder);
105
- } else {
106
- holder = (ViewHolder) convertView.getTag();
107
- }
108
-
109
- //
110
89
  final AppData data = getItem(position);
111
90
  //
112
91
  holder.textLabel.setText(data.label);
113
92
  holder.imageIcon.setImageDrawable(data.icon);
114
93
  holder.packageName.setText(data.name);
115
94
 
116
- return convertView;
117
- }
118
- }
119
-
120
95
  // private class for storing application data.
121
96
  private static class AppData {
122
97
  String label;
@@ -130,23 +105,33 @@
130
105
  ImageView imageIcon;
131
106
  TextView packageName;
132
107
  }
108
+ ```
133
109
 
110
+ ### activity_main.xml
111
+ ```
134
- // private class for storing application data.
112
+ <?xml version="1.0" encoding="utf-8"?>
113
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
114
+ android:layout_width="match_parent"
115
+ android:layout_height="match_parent"
135
- private static class AppData {
116
+ android:orientation="vertical">
136
- String label;
137
- Drawable icon;
138
- String name;
139
- }
140
117
 
118
+ <Button
119
+ android:id="@+id/button"
120
+ android:layout_width="wrap_content"
121
+ android:layout_height="50dp"
122
+ android:gravity="center"
123
+ android:text="Button" />
124
+
125
+ <ListView
126
+ android:id="@+id/card_list"
127
+ android:layout_width="match_parent"
128
+ android:layout_height="match_parent">
141
- // private class ViewHolder.
129
+ </ListView>
142
- private static class ViewHolder {
130
+
143
- TextView textLabel;
144
- ImageView imageIcon;
145
- TextView packageName;
131
+ </LinearLayout>
146
- }
147
132
  ```
148
133
 
149
- ### activity_main.xml
134
+ item_list.xml
150
135
  ```
151
136
  <?xml version="1.0" encoding="utf-8"?>
152
137
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
@@ -158,45 +143,22 @@
158
143
  android:id="@+id/icon"
159
144
  android:layout_width="50dp"
160
145
  android:layout_height="50dp"
161
- android:layout_alignParentLeft="true"
162
- android:layout_alignParentTop="true"
163
- android:layout_weight="1"/>
146
+ android:layout_below="@+id/button" />
164
147
 
165
148
  <TextView
166
149
  android:id="@+id/label"
167
150
  android:layout_width="wrap_content"
168
151
  android:layout_height="wrap_content"
169
- android:layout_alignParentRight="true"
170
- android:layout_alignParentTop="true"
152
+ android:layout_below="@+id/button"
171
153
  android:layout_toRightOf="@+id/icon"
172
- android:textSize="18sp"
154
+ android:textSize="18sp" />
173
- android:layout_weight="1"/>
174
155
 
175
156
  <TextView
176
157
  android:id="@+id/name"
177
158
  android:layout_width="wrap_content"
178
159
  android:layout_height="wrap_content"
179
160
  android:layout_alignLeft="@+id/label"
180
- android:layout_alignParentRight="true"
181
- android:layout_below="@+id/label"
161
+ android:layout_below="@+id/label" />
182
- android:layout_weight="1" />
183
162
 
184
- <LinearLayout
185
- android:layout_width="match_parent"
186
- android:layout_height="50dp"
187
- android:layout_alignParentBottom="true"
188
- android:layout_alignParentStart="true"
189
- android:orientation="vertical">
190
-
191
- <Button
192
- android:id="@+id/button4"
193
- android:layout_width="match_parent"
194
- android:layout_height="wrap_content"
195
- android:layout_alignParentBottom="true"
196
- android:layout_alignParentEnd="true"
197
- android:layout_weight="1"
198
- android:text="Button" />
199
- </LinearLayout>
200
-
201
163
  </RelativeLayout>
202
164
  ```

2

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

2018/03/24 12:54

投稿

rick
rick

スコア18

title CHANGED
File without changes
body CHANGED
@@ -9,6 +9,7 @@
9
9
  アプリ一覧のリストは、listViewとArrayAdapterを使用して実装しております。)
10
10
 
11
11
  ### LancherApp.java
12
+ ```
12
13
  public class LancherApp extends Activity {
13
14
 
14
15
  //
@@ -80,6 +81,42 @@
80
81
  }
81
82
  }
82
83
 
84
+ // private Adapter Class indicates label and icon of application.
85
+ private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
86
+ //
87
+ private final LayoutInflater mInflater;
88
+
89
+ public ApplicationListAdapter (Context context, List<AppData> dataList) {
90
+ super(context, R.layout.activity_main);
91
+ mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
92
+ addAll(dataList);
93
+ }
94
+
95
+ @Override
96
+ public View getView (int position, View convertView, ViewGroup parent) {
97
+ ViewHolder holder = new ViewHolder();
98
+
99
+ if (convertView == null) {
100
+ convertView = mInflater.inflate(R.layout.activity_main, parent, false);
101
+ holder.textLabel = (TextView) convertView.findViewById(R.id.label);
102
+ holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
103
+ holder.packageName = (TextView) convertView.findViewById(R.id.name);
104
+ convertView.setTag(holder);
105
+ } else {
106
+ holder = (ViewHolder) convertView.getTag();
107
+ }
108
+
109
+ //
110
+ final AppData data = getItem(position);
111
+ //
112
+ holder.textLabel.setText(data.label);
113
+ holder.imageIcon.setImageDrawable(data.icon);
114
+ holder.packageName.setText(data.name);
115
+
116
+ return convertView;
117
+ }
118
+ }
119
+
83
120
  // private class for storing application data.
84
121
  private static class AppData {
85
122
  String label;
@@ -94,8 +131,23 @@
94
131
  TextView packageName;
95
132
  }
96
133
 
134
+ // private class for storing application data.
135
+ private static class AppData {
136
+ String label;
137
+ Drawable icon;
138
+ String name;
139
+ }
97
140
 
141
+ // private class ViewHolder.
142
+ private static class ViewHolder {
143
+ TextView textLabel;
144
+ ImageView imageIcon;
145
+ TextView packageName;
146
+ }
147
+ ```
148
+
98
149
  ### activity_main.xml
150
+ ```
99
151
  <?xml version="1.0" encoding="utf-8"?>
100
152
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
101
153
  android:layout_width="match_parent"
@@ -146,4 +198,5 @@
146
198
  android:text="Button" />
147
199
  </LinearLayout>
148
200
 
149
- </RelativeLayout>
201
+ </RelativeLayout>
202
+ ```

1

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

2018/03/23 12:46

投稿

rick
rick

スコア18

title CHANGED
File without changes
body CHANGED
@@ -44,6 +44,57 @@
44
44
  setContentView(listView);
45
45
  }
46
46
 
47
+ // private Adapter Class indicates label and icon of application.
48
+ private static class ApplicationListAdapter extends ArrayAdapter<AppData> {
49
+ //
50
+ private final LayoutInflater mInflater;
51
+
52
+ public ApplicationListAdapter (Context context, List<AppData> dataList) {
53
+ super(context, R.layout.activity_main);
54
+ mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
55
+ addAll(dataList);
56
+ }
57
+
58
+ @Override
59
+ public View getView (int position, View convertView, ViewGroup parent) {
60
+ ViewHolder holder = new ViewHolder();
61
+
62
+ if (convertView == null) {
63
+ convertView = mInflater.inflate(R.layout.activity_main, parent, false);
64
+ holder.textLabel = (TextView) convertView.findViewById(R.id.label);
65
+ holder.imageIcon = (ImageView) convertView.findViewById(R.id.icon);
66
+ holder.packageName = (TextView) convertView.findViewById(R.id.name);
67
+ convertView.setTag(holder);
68
+ } else {
69
+ holder = (ViewHolder) convertView.getTag();
70
+ }
71
+
72
+ //
73
+ final AppData data = getItem(position);
74
+ //
75
+ holder.textLabel.setText(data.label);
76
+ holder.imageIcon.setImageDrawable(data.icon);
77
+ holder.packageName.setText(data.name);
78
+
79
+ return convertView;
80
+ }
81
+ }
82
+
83
+ // private class for storing application data.
84
+ private static class AppData {
85
+ String label;
86
+ Drawable icon;
87
+ String name;
88
+ }
89
+
90
+ // private class ViewHolder.
91
+ private static class ViewHolder {
92
+ TextView textLabel;
93
+ ImageView imageIcon;
94
+ TextView packageName;
95
+ }
96
+
97
+
47
98
  ### activity_main.xml
48
99
  <?xml version="1.0" encoding="utf-8"?>
49
100
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"