質問編集履歴
2
初心者アイコンを付ける
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
1
ソースコード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -139,3 +139,411 @@
|
|
139
139
|
|
140
140
|
|
141
141
|
</LinearLayout>
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
mainactivity java↓
|
148
|
+
|
149
|
+
package com.example.tweetandroid;
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
import java.util.ArrayList;
|
154
|
+
|
155
|
+
import java.util.Calendar;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
import android.app.Activity;
|
160
|
+
|
161
|
+
import android.content.ContentValues;
|
162
|
+
|
163
|
+
import android.content.Intent;
|
164
|
+
|
165
|
+
import android.content.res.Resources;
|
166
|
+
|
167
|
+
import android.database.Cursor;
|
168
|
+
|
169
|
+
import android.database.sqlite.SQLiteDatabase;
|
170
|
+
|
171
|
+
import android.graphics.Bitmap;
|
172
|
+
|
173
|
+
import android.graphics.BitmapFactory;
|
174
|
+
|
175
|
+
import android.os.Bundle;
|
176
|
+
|
177
|
+
import android.view.Menu;
|
178
|
+
|
179
|
+
import android.view.MenuItem;
|
180
|
+
|
181
|
+
import android.view.View;
|
182
|
+
|
183
|
+
import android.view.View.OnClickListener;
|
184
|
+
|
185
|
+
import android.widget.AdapterView;
|
186
|
+
|
187
|
+
import android.widget.AdapterView.OnItemClickListener;
|
188
|
+
|
189
|
+
import android.widget.Button;
|
190
|
+
|
191
|
+
import android.widget.EditText;
|
192
|
+
|
193
|
+
import android.widget.ListView;
|
194
|
+
|
195
|
+
import android.widget.Toast;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
/*
|
200
|
+
|
201
|
+
* 画像はまだデータベースに保存出来ていません。
|
202
|
+
|
203
|
+
*/
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
public class MainActivity extends Activity implements OnItemClickListener,OnClickListener{
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
DataBase helper;
|
214
|
+
|
215
|
+
Bitmap bmp1,bmp2;
|
216
|
+
|
217
|
+
EditText name,comment;
|
218
|
+
|
219
|
+
Button button;
|
220
|
+
|
221
|
+
ListView list;
|
222
|
+
|
223
|
+
ArrayList<Tweet> array;
|
224
|
+
|
225
|
+
Globals globals;
|
226
|
+
|
227
|
+
int position;
|
228
|
+
|
229
|
+
final Calendar calendar = Calendar.getInstance();
|
230
|
+
|
231
|
+
// int year;
|
232
|
+
|
233
|
+
int month; //月
|
234
|
+
|
235
|
+
int day; //日
|
236
|
+
|
237
|
+
int hour; //時
|
238
|
+
|
239
|
+
int minute; //分
|
240
|
+
|
241
|
+
// int second;
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
@Override
|
248
|
+
|
249
|
+
protected void onCreate(Bundle savedInstanceState) {
|
250
|
+
|
251
|
+
super.onCreate(savedInstanceState);
|
252
|
+
|
253
|
+
setContentView(R.layout.activity_main);
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
//レイアウト部品と結び付け
|
260
|
+
|
261
|
+
list = (ListView)findViewById(R.id.listView1);
|
262
|
+
|
263
|
+
// name = (EditText)findViewById(R.id.name);
|
264
|
+
|
265
|
+
comment = (EditText)findViewById(R.id.comment);
|
266
|
+
|
267
|
+
button = (Button)findViewById(R.id.button1);
|
268
|
+
|
269
|
+
button.setOnClickListener(this);
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
//月日時分設定
|
274
|
+
|
275
|
+
// year = calendar.get(Calendar.YEAR);
|
276
|
+
|
277
|
+
month = calendar.get(Calendar.MONTH);
|
278
|
+
|
279
|
+
day = calendar.get(Calendar.DAY_OF_MONTH);
|
280
|
+
|
281
|
+
hour = calendar.get(Calendar.HOUR_OF_DAY);
|
282
|
+
|
283
|
+
minute = calendar.get(Calendar.MINUTE);
|
284
|
+
|
285
|
+
// second = calendar.get(Calendar.SECOND);
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
//画像用意
|
290
|
+
|
291
|
+
Resources res = getResources();
|
292
|
+
|
293
|
+
bmp1 = BitmapFactory.decodeResource(res, R.drawable.img14125);
|
294
|
+
|
295
|
+
bmp2 = BitmapFactory.decodeResource(res, R.drawable.img14123);
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
//グローバル変数
|
300
|
+
|
301
|
+
globals = (Globals)this.getApplication();
|
302
|
+
|
303
|
+
//配列用
|
304
|
+
|
305
|
+
array = new ArrayList<Tweet>();
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
helper = new DataBase(this);//ヘルパークラスの初期化
|
310
|
+
|
311
|
+
SQLiteDatabase db = helper.getWritableDatabase();
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
//初期リスト設定
|
316
|
+
|
317
|
+
String[] colms = {"name","comment","time"};
|
318
|
+
|
319
|
+
Cursor cursor = db.query ("tweet",colms,null,null,null,null,null);
|
320
|
+
|
321
|
+
// if (cursor.moveToFirst()){
|
322
|
+
|
323
|
+
if(cursor.moveToNext()){
|
324
|
+
|
325
|
+
array.add(new Tweet(bmp1,cursor.getString(0),cursor.getString(1),cursor.getString(2)));
|
326
|
+
|
327
|
+
cursor.moveToNext();
|
328
|
+
|
329
|
+
array.add(new Tweet(bmp2,cursor.getString(0),cursor.getString(1),cursor.getString(2)));
|
330
|
+
|
331
|
+
while(cursor.moveToNext()){
|
332
|
+
|
333
|
+
array.add(new Tweet(bmp1,cursor.getString(0),cursor.getString(1),cursor.getString(2)));
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
// }
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
list.setAdapter(new CustomAdapter(this, R.layout.list_item,array));
|
346
|
+
|
347
|
+
list.setOnItemClickListener(this);
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
@Override
|
356
|
+
|
357
|
+
public boolean onCreateOptionsMenu(Menu menu) {
|
358
|
+
|
359
|
+
// Inflate the menu; this adds items to the action bar if it is present.
|
360
|
+
|
361
|
+
getMenuInflater().inflate(R.menu.main, menu);
|
362
|
+
|
363
|
+
return true;
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
@Override
|
370
|
+
|
371
|
+
public boolean onOptionsItemSelected(MenuItem item) {
|
372
|
+
|
373
|
+
// Handle action bar item clicks here. The action bar will
|
374
|
+
|
375
|
+
// automatically handle clicks on the Home/Up button, so long
|
376
|
+
|
377
|
+
// as you specify a parent activity in AndroidManifest.xml.
|
378
|
+
|
379
|
+
int id = item.getItemId();
|
380
|
+
|
381
|
+
if (id == R.id.action_settings) {
|
382
|
+
|
383
|
+
return true;
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
return super.onOptionsItemSelected(item);
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
@Override
|
396
|
+
|
397
|
+
public void onItemClick(AdapterView<?> parent, View view, int position,
|
398
|
+
|
399
|
+
long id) {
|
400
|
+
|
401
|
+
/*
|
402
|
+
|
403
|
+
Intent intent = new Intent(this,TweetActivity.class);
|
404
|
+
|
405
|
+
intent.putExtra("image",array.get(position).image);
|
406
|
+
|
407
|
+
intent.putExtra("name",array.get(position).name);
|
408
|
+
|
409
|
+
intent.putExtra("comment",array.get(position).comment);
|
410
|
+
|
411
|
+
intent.putExtra("time",array.get(position).time);
|
412
|
+
|
413
|
+
*/
|
414
|
+
|
415
|
+
globals.position = position;
|
416
|
+
|
417
|
+
globals.img = array.get(position).image;
|
418
|
+
|
419
|
+
globals.name = array.get(position).name;
|
420
|
+
|
421
|
+
globals.comment = array.get(position).comment;
|
422
|
+
|
423
|
+
globals.time = array.get(position).time;
|
424
|
+
|
425
|
+
startActivity(new Intent(this, TweetActivity.class));
|
426
|
+
|
427
|
+
// startActivity(intent);
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
@Override
|
434
|
+
|
435
|
+
public void onClick(View v) {
|
436
|
+
|
437
|
+
month = calendar.get(Calendar.MONTH);
|
438
|
+
|
439
|
+
day = calendar.get(Calendar.DAY_OF_MONTH);
|
440
|
+
|
441
|
+
hour = calendar.get(Calendar.HOUR_OF_DAY);
|
442
|
+
|
443
|
+
minute = calendar.get(Calendar.MINUTE);
|
444
|
+
|
445
|
+
// second = calendar.get(Calendar.SECOND);
|
446
|
+
|
447
|
+
if (v == button){
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
if (comment.getText().toString().equals("")){
|
452
|
+
|
453
|
+
Toast.makeText(this, "投稿内容が空です。", Toast.LENGTH_LONG).show();
|
454
|
+
|
455
|
+
}else if (comment.getText().toString().length() > 140){
|
456
|
+
|
457
|
+
Toast.makeText(this, "140字以内で入力して下さい。", Toast.LENGTH_LONG).show();
|
458
|
+
|
459
|
+
}else{
|
460
|
+
|
461
|
+
// String today = (month+1)+"月"+day+"日"+hour+":"+minute;
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
SQLiteDatabase db = helper.getWritableDatabase();
|
466
|
+
|
467
|
+
ContentValues cv = new ContentValues();//挿入する情報を表現するクラス
|
468
|
+
|
469
|
+
cv.put("name","@Fukushima");//列名、中身のセット
|
470
|
+
|
471
|
+
cv.put("comment",comment.getText().toString());//
|
472
|
+
|
473
|
+
if (minute < 10){
|
474
|
+
|
475
|
+
cv.put("time",(month+1)+"月"+day+"日"+hour+":"+"0"+minute);
|
476
|
+
|
477
|
+
}else{
|
478
|
+
|
479
|
+
cv.put("time",(month+1)+"月"+day+"日"+hour+":"+minute);
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
db.insert("tweet",null,cv);//dbに情報を挿入する。tweetはテーブル名
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
// array.add(new Tweet(bmp1,"福島",comment.getText().toString(),(month+1)+"月"+day+"日"+hour+":"+minute));
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
helper = new DataBase(this);//ヘルパークラスの初期化
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
String[] colms = {"name","comment","time"};
|
504
|
+
|
505
|
+
Cursor cursor = db.query ("tweet",colms,null,null,null,null,null);
|
506
|
+
|
507
|
+
/*
|
508
|
+
|
509
|
+
if(cursor.moveToNext()){
|
510
|
+
|
511
|
+
array.add(new Tweet(bmp1,cursor.getString(0),cursor.getString(1),cursor.getString(2)));
|
512
|
+
|
513
|
+
cursor.moveToNext();
|
514
|
+
|
515
|
+
array.add(new Tweet(bmp2,cursor.getString(0),cursor.getString(1),cursor.getString(2)));
|
516
|
+
|
517
|
+
while(cursor.moveToNext()){
|
518
|
+
|
519
|
+
array.add(new Tweet(bmp1,cursor.getString(0),cursor.getString(1),cursor.getString(2)));
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
}*/
|
524
|
+
|
525
|
+
cursor.moveToLast();
|
526
|
+
|
527
|
+
array.add(new Tweet(bmp1,cursor.getString(0),cursor.getString(1),cursor.getString(2)));
|
528
|
+
|
529
|
+
PostDialog md = new PostDialog(this);
|
530
|
+
|
531
|
+
md.show(this.getFragmentManager(),"messageDialog");
|
532
|
+
|
533
|
+
// Toast.makeText(this, "投稿完了",Toast.LENGTH_LONG).show();
|
534
|
+
|
535
|
+
comment.setText("");
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
}
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
}
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
}
|
548
|
+
|
549
|
+
}
|