質問編集履歴

2

画像を追加しました

2021/02/01 01:46

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -267,3 +267,7 @@
267
267
  }
268
268
 
269
269
  ```
270
+
271
+ ListViewのレイアウト
272
+
273
+ ![イメージ説明](2db6ea4453fe77205a7c0ca1bf976430.png)

1

コードを詳細に載せました

2021/02/01 01:46

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,116 +6,264 @@
6
6
 
7
7
  ■MainActivity
8
8
 
9
- refMsg.addChildEventListener(new ChildEventListener() {
10
-
11
- @Override
12
-
13
- public void onChildAdded(DataSnapshot dataSnapshot, String s) {
14
-
15
- //データー取得
16
-
17
- Post value = dataSnapshot.getValue(Post.class);
18
-
19
-
20
-
21
- //リストに追加
22
-
23
- items.add(value);
24
-
25
-
26
-
27
- //画面を更新
28
-
29
- postAdapter.clear();
30
-
31
- postAdapter.addAll(items);
32
-
33
- postAdapter.notifyDataSetChanged();
34
-
35
- }
36
-
37
-
38
-
39
- public void good(View v) {
40
-
41
- //ListViewのボタンの処理を行いたいところ
42
-
43
- }
44
-
45
-
9
+ public class MainActivity extends AppCompatActivity implements View.OnClickListener {
10
+
11
+
12
+
13
+ //gor firebase
14
+
15
+ FirebaseDatabase database = FirebaseDatabase.getInstance();
16
+
17
+ DatabaseReference refMsg = database.getReference("message");
18
+
19
+
20
+
21
+ //view component
22
+
23
+ ListView mListView;
24
+
25
+ Button mButton;
26
+
27
+ TextView likepoint;
28
+
29
+
30
+
31
+ //fields
32
+
33
+ ArrayList<Post> items;
34
+
35
+
36
+
37
+ // adapter
38
+
39
+ PostAdapter postAdapter;
40
+
41
+
42
+
43
+
44
+
45
+ ArrayList arrayList;
46
+
47
+
48
+
49
+ @Override
50
+
51
+ protected void onCreate(Bundle savedInstanceState) {
52
+
53
+ super.onCreate(savedInstanceState);
54
+
55
+ setContentView(R.layout.activity_main);
56
+
57
+
58
+
59
+ //関連付け
60
+
61
+ mListView = (ListView) findViewById(R.id.list_view);
62
+
63
+ mButton = (Button) findViewById(R.id.button);
64
+
65
+ // likepoint = (TextView) findViewById(R.id.likepoint);
66
+
67
+ mButton.setOnClickListener(this);
68
+
69
+
70
+
71
+ items = new ArrayList<>();
72
+
73
+ postAdapter = new PostAdapter(this, 0, items);
74
+
75
+ mListView.setAdapter(postAdapter);
76
+
77
+ refMsg.addChildEventListener(new ChildEventListener() {
78
+
79
+ @Override
80
+
81
+ public void onChildAdded(DataSnapshot dataSnapshot, String s) {
82
+
83
+ //データー取得
84
+
85
+ Post value = dataSnapshot.getValue(Post.class);
86
+
87
+
88
+
89
+ //リストに追加
90
+
91
+ items.add(value);
92
+
93
+
94
+
95
+ //画面を更新
96
+
97
+ postAdapter.clear();
98
+
99
+ postAdapter.addAll(items);
100
+
101
+ postAdapter.notifyDataSetChanged();
102
+
103
+
104
+
105
+ }
106
+
107
+
108
+
109
+ @Override
110
+
111
+ public void onChildChanged(DataSnapshot dataSnapshot, String s) {
112
+
113
+
114
+
115
+ }
116
+
117
+
118
+
119
+ @Override
120
+
121
+ public void onChildRemoved(DataSnapshot dataSnapshot) {
122
+
123
+
124
+
125
+ }
126
+
127
+
128
+
129
+ @Override
130
+
131
+ public void onChildMoved(DataSnapshot dataSnapshot, String s) {
132
+
133
+
134
+
135
+ }
136
+
137
+
138
+
139
+ @Override
140
+
141
+ public void onCancelled(DatabaseError databaseError) {
142
+
143
+
144
+
145
+ }
146
+
147
+ });
148
+
149
+ }
150
+
151
+ public void good(View v) {
152
+
153
+ //ListView上のボタンの処理を行いたいところ
154
+
155
+ }
46
156
 
47
157
  ■PostAdapter.java
48
158
 
49
159
 
50
160
 
51
- public PostAdapter(Context context, int resource, List<Post> objects) {
52
-
53
- -省略-
54
-
55
-
56
-
57
- }
58
-
59
- ~省略~
60
-
61
- }
62
-
63
-
64
-
65
-
66
-
67
- @Override
68
-
69
- public View getView(int position, View convertView, ViewGroup parent) {
70
-
71
-
72
-
73
- //もともとのコード
74
-
75
- // Post item = getItem(position);
76
-
77
-
78
-
79
- //順序を逆にする
80
-
81
- Post item = getItem(getCount() - 1 - position);
82
-
83
-
84
-
85
- final ViewHolder viewHolder;
86
-
87
-
88
-
89
- if (convertView != null) {
90
-
91
- viewHolder = (ViewHolder) convertView.getTag();
92
-
93
- } else {
94
-
95
- convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_item_post, parent, false);
96
-
97
- viewHolder = new ViewHolder();
98
-
99
- viewHolder.userNameText = (TextView) convertView.findViewById(R.id.username);
100
-
101
- ~省略~
102
-
103
- }
104
-
105
- viewHolder.userNameText.setText(item.getUserName());
106
-
107
- viewHolder.messageText.setText(item.getMessage());
108
-
109
-
110
-
111
- ~省略~
112
-
113
-
114
-
115
- return convertView;
116
-
117
-
118
-
119
- }
161
+ public class PostAdapter extends ArrayAdapter<Post> {
162
+
163
+
164
+
165
+ List<Post> items;
166
+
167
+
168
+
169
+
170
+
171
+ public PostAdapter(Context context, int resource, List<Post> objects) {
172
+
173
+ super(context, resource);
174
+
175
+ items = objects;
176
+
177
+
178
+
179
+ }
180
+
181
+ static class ViewHolder {
182
+
183
+ TextView userNameText;
184
+
185
+ TextView messageText;
186
+
187
+ TextView likepoint;
188
+
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+ @Override
196
+
197
+ public View getView(int position, View convertView, ViewGroup parent) {
198
+
199
+
200
+
201
+ //順序を逆にする
202
+
203
+ Post item = getItem(getCount() - 1 - position);
204
+
205
+
206
+
207
+ final ViewHolder viewHolder;
208
+
209
+
210
+
211
+ if (convertView != null) {
212
+
213
+ viewHolder = (ViewHolder) convertView.getTag();
214
+
215
+ } else {
216
+
217
+ convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_item_post, parent, false);
218
+
219
+ viewHolder = new ViewHolder();
220
+
221
+ viewHolder.userNameText = (TextView) convertView.findViewById(R.id.username);
222
+
223
+ viewHolder.messageText = (TextView) convertView.findViewById(R.id.message);
224
+
225
+ viewHolder.likepoint = (TextView) convertView.findViewById(R.id.likepoint);
226
+
227
+ convertView.setTag(viewHolder);
228
+
229
+ }
230
+
231
+ viewHolder.userNameText.setText(item.getUserName());
232
+
233
+ viewHolder.messageText.setText(item.getMessage());
234
+
235
+
236
+
237
+ int likespointnumber = item.getLike();
238
+
239
+ String likepointtext = String.valueOf(likespointnumber);
240
+
241
+ viewHolder.likepoint.setText(likepointtext);
242
+
243
+
244
+
245
+ return convertView;
246
+
247
+
248
+
249
+ }
250
+
251
+
252
+
253
+ @Override
254
+
255
+ public int getCount() {
256
+
257
+ return items.size();
258
+
259
+ }
260
+
261
+ @Override
262
+
263
+ public Post getItem(int position) {
264
+
265
+ return items.get(position);
266
+
267
+ }
120
268
 
121
269
  ```