質問編集履歴

2

微修正

2017/02/03 04:23

投稿

rain_sun
rain_sun

スコア12

test CHANGED
File without changes
test CHANGED
@@ -282,4 +282,6 @@
282
282
 
283
283
  }
284
284
 
285
+ }
286
+
285
- }```
287
+ ```

1

修正

2017/02/03 04:22

投稿

rain_sun
rain_sun

スコア12

test CHANGED
File without changes
test CHANGED
@@ -6,33 +6,105 @@
6
6
 
7
7
  ###発生している問題・エラーメッセージ
8
8
 
9
+ DBを読み込む際に
10
+
11
+ 末尾のデータが
12
+
9
- SQLiteに保存する際に
13
+ listview表示されない
10
-
11
- 一つ遅れて保存されます
14
+
12
-
13
-
14
-
15
-
16
-
15
+
16
+
17
+
18
+
17
- ###MainActivity.java
19
+ ###SqlLoad.java
18
20
 
19
21
  ```
20
22
 
23
+ package com.example.innovation.f_featnote;
24
+
25
+
26
+
27
+ import android.app.Activity;
28
+
29
+ import android.app.ListActivity;
30
+
31
+ import android.content.Context;
32
+
33
+ import android.content.Intent;
34
+
35
+ import android.database.Cursor;
36
+
37
+ import android.database.sqlite.SQLiteDatabase;
38
+
39
+ import android.database.sqlite.SQLiteException;
40
+
41
+ import android.database.sqlite.SQLiteOpenHelper;
42
+
43
+ import android.os.Bundle;
44
+
45
+ import android.provider.ContactsContract;
46
+
47
+ import android.util.Log;
48
+
49
+ import android.view.LayoutInflater;
50
+
51
+ import android.view.View;
52
+
53
+ import android.view.ViewGroup;
54
+
55
+ import android.widget.AdapterView;
56
+
57
+ import android.widget.ArrayAdapter;
58
+
59
+ import android.widget.BaseAdapter;
60
+
61
+ import android.widget.Button;
62
+
63
+ import android.widget.ListView;
64
+
65
+ import android.widget.TextView;
66
+
67
+ import android.widget.Toast;
68
+
69
+
70
+
71
+ import com.example.innovation.f_featnote.SubOpenHelper;
72
+
73
+ import com.example.innovation.f_featnote.MainActivity;
74
+
75
+
76
+
77
+ import java.util.ArrayList;
78
+
79
+ import java.util.Arrays;
80
+
21
- import 省略
81
+ import java.util.List;
82
+
83
+
84
+
22
-
85
+ /**
86
+
23
-
87
+ * Created by Innovation on 2017/01/26.
88
+
24
-
89
+ */
90
+
91
+
92
+
25
- public class MainActivity extends AppCompatActivity
93
+ public class SqlLoad extends Activity {
94
+
26
-
95
+ ListView lv;
96
+
97
+ static String[][] list;
98
+
27
- implements View.OnClickListener {
99
+ static String[] Did = new String[2];
28
-
29
- //前準備
100
+
30
-
31
- static int Tflg;
101
+ static long id_;
32
-
102
+
33
- //外クラス参照用
103
+ static boolean[] flg;
104
+
34
-
105
+ static String[] Data;
106
+
35
-
107
+ private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
36
108
 
37
109
 
38
110
 
@@ -42,512 +114,172 @@
42
114
 
43
115
  super.onCreate(savedInstanceState);
44
116
 
45
- setContentView(R.layout.activity_main);
117
+ setContentView(R.layout.sql_load);
46
-
118
+
47
- Button textSave = (Button) findViewById(R.id.Text_Save);
119
+ // final boolean[] flg = new boolean[1];
48
-
49
- Button textLoad = (Button) findViewById(R.id.Text_Load);
120
+
50
-
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
51
- textSave.setOnClickListener(this);
129
+ Main();
52
-
53
- textLoad.setOnClickListener(this);
130
+
54
-
55
-
56
-
57
- }
131
+ }
132
+
133
+
134
+
58
-
135
+ public void Main(){
136
+
137
+
138
+
139
+ Data = SQLDataGet();
140
+
141
+ // lv = new ListView(this);
142
+
143
+ // ArrayList<String> Data = new ArrayList<>(Arrays.asList(SQLDataGet()));
144
+
145
+ lv = (ListView)findViewById(R.id.sql_load_main);
146
+
147
+ ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, Data);
148
+
149
+ lv.setAdapter(adapter);//リスト表示
150
+
151
+ // setContentView(lv,new ViewGroup.LayoutParams(WC,WC));
152
+
153
+
154
+
155
+ lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
156
+
157
+ @Override
158
+
159
+ public void onItemClick(AdapterView<?> parent,View view,int position,long id){
160
+
161
+ // ListView listView = (ListView) parent;
162
+
163
+ // String item = (String)listView.getItemAtPosition(position);
164
+
165
+ Log.d("id",""+id);
166
+
167
+ id_ = id;
168
+
169
+ SQLDContentGet();
170
+
171
+ SQLDataSet();
172
+
173
+ finish();
174
+
175
+ // Toast.makeText(getApplicationContext(),item + "clicked",Toast.LENGTH_LONG).show();
176
+
177
+ }
178
+
179
+ });
180
+
181
+ }
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+ public String[] SQLDataGet(){
194
+
195
+ MainActivity main = new MainActivity();
196
+
197
+ Context con = this;
198
+
199
+ List ls = null;
200
+
201
+ SQLiteDatabase sdb = main.SQLopen(con.getApplicationContext());//MainActivity参照
202
+
203
+ final String[] columns = new String[]{"title","content","date"};//列名
204
+
205
+ String where = null;//条件
206
+
207
+ String[] Args = new String[]{""};//?の置き換え
208
+
209
+ Cursor c = sdb.query("MemoContents", columns,where, null, null, null, null);
210
+
211
+ int startPostion = c.getPosition();
212
+
213
+ c.moveToFirst();
214
+
215
+ Log.d("DBG",""+c.getCount());
216
+
217
+ list = new String[c.getCount()-1][columns.length];
218
+
219
+ String[] Dlist = new String[c.getCount()-1];
220
+
221
+ for (int i = 0; i< list.length; i++){
222
+
223
+ for (int j = 0; j < list[1].length; j++){
224
+
225
+ list[i][j] = c.getString(j);
226
+
227
+ Log.d(i+"-"+j,list[i][j]);//デバッグ
228
+
229
+ Dlist[i] = list[i][0];
230
+
231
+ }
232
+
233
+ c.moveToNext();
234
+
235
+ }
236
+
237
+ c.moveToPosition(startPostion);
238
+
239
+
240
+
59
- //前準備 ここまで
241
+ // finish();
242
+
243
+ return Dlist;
244
+
245
+ }
246
+
247
+
248
+
249
+ public void SQLDContentGet(){
250
+
251
+ Did[0] = list[(int)id_][0];
252
+
253
+ Did[1] = list[(int)id_][1];
254
+
255
+ }
256
+
257
+
258
+
259
+ public void SQLDataSet(){
260
+
261
+ Intent data = new Intent();
262
+
263
+ Bundle bundle = new Bundle();
264
+
265
+ data.putExtra("key.data",Did);
266
+
267
+ data.putExtras(bundle);
268
+
269
+ setResult(RESULT_OK,data);
270
+
271
+ finish();
272
+
273
+ }
60
274
 
61
275
 
62
276
 
63
277
  @Override
64
278
 
65
- //クリックイベント(一括管理)
66
-
67
- public void onClick(View v) {
68
-
69
- switch (v.getId()) {
70
-
71
- case R.id.Text_Load://読み込み
72
-
73
- 省略
74
-
75
- case R.id.Text_Save://保存
76
-
77
- AlertDialog.Builder alertDlg2 = new AlertDialog.Builder(this);
78
-
79
- final Toast toast2 = Toast.makeText(this, R.string.Text_Save_Error, Toast.LENGTH_LONG);
80
-
81
- final Toast toast3 = Toast.makeText(this, R.string.Text_Saved, Toast.LENGTH_LONG);
82
-
83
- alertDlg2.setTitle(R.string.Save_check_T);
84
-
85
- alertDlg2.setMessage(R.string.Save_check_C);
86
-
87
- alertDlg2.setPositiveButton(
88
-
89
- R.string.Save_check_O,
90
-
91
- new DialogInterface.OnClickListener() {
92
-
93
- public void onClick(DialogInterface dialog, int which) {
94
-
95
- //OK処理
96
-
97
- boolean flg = Flist(2);
98
-
99
- if (flg == false){
100
-
101
- MainActivity.Tflg = 1;
102
-
103
- }else {
104
-
105
- MainActivity.Tflg = 2;
106
-
107
- }
108
-
109
- switch (Tflg){
110
-
111
- case 1:
112
-
113
- toast2.show();
114
-
115
- break;
116
-
117
- case 2:
118
-
119
- toast3.show();
120
-
121
- break;
122
-
123
- }
124
-
125
- }
126
-
127
- });
128
-
129
- alertDlg2.setNegativeButton(
130
-
131
- R.string.Save_check_N,
132
-
133
- new DialogInterface.OnClickListener() {
134
-
135
- public void onClick(DialogInterface dialog, int which) {
136
-
137
- //キャンセル処理
138
-
139
- }
140
-
141
- });
142
-
143
-
144
-
145
- alertDlg2.create().show();//ダイアログ呼び出し
146
-
147
- break;
148
-
149
- }
150
-
151
- }
152
-
153
-
154
-
155
- public boolean Flist(int c) {//R/W処理 引数分岐
156
-
157
- switch (c) {
158
-
159
- case 1://txt出力用
160
-
161
- Fsavetxt();
162
-
163
- break;
164
-
165
- case 2://SQLite出力用
166
-
167
- boolean flg = FsaveSQL();
168
-
169
- return flg;
170
-
171
- case 3://sdカード書き込み実装用
172
-
173
- break;
174
-
175
- case 4://txt読み込み
176
-
177
- Fload();
178
-
179
- break;
180
-
181
- case 5://SQLite読み込み
182
-
183
- FSelectSQL();
184
-
185
-
186
-
187
- break;
188
-
189
- }
190
-
191
- return true;
192
-
193
- }
194
-
195
-
196
-
197
- public void Fsavetxt() {//保存処理 txt
198
-
199
-
200
-
201
- String path = DateG(1);
202
-
203
-
204
-
205
- EditText con = (EditText) findViewById(R.id.Text_Main);
206
-
207
- SpannableStringBuilder content = (SpannableStringBuilder) con.getText();
208
-
209
-
210
-
211
- OutputStream Csave;
212
-
213
- try
214
-
215
-
216
-
217
- {
218
-
219
- Csave = openFileOutput(path, MODE_PRIVATE | MODE_APPEND);
220
-
221
- PrintWriter writer = new PrintWriter(Csave, true);
222
-
223
- writer.println(ContentG(0));//書き込み
224
-
225
- writer.close();//終了
226
-
227
- } catch (
228
-
229
- IOException e
230
-
231
- )
232
-
233
-
234
-
235
- {
236
-
237
- e.printStackTrace();
238
-
239
- }
240
-
241
- }
242
-
243
-
244
-
245
- public boolean FsaveSQL() {//保存処理 SQL
246
-
247
- SQLiteDatabase sdb = SQLopen();
248
-
249
- ContentValues values = new ContentValues();
250
-
251
- String tit = ContentG(1);
252
-
253
- String con = ContentG(0);
254
-
255
- if (tit == null || con == null || tit.length() == 0 || con.length() == 0) {
256
-
257
- return false;
258
-
259
- }
260
-
261
- values.put("title", tit);
262
-
263
- values.put("content", con);
264
-
265
- values.put("date", DateG(2));
266
-
267
- long id = sdb.insert("MemoContents", null, values);
268
-
269
- if (id < 0){}
270
-
271
- return true;
272
-
273
- }
274
-
275
-
276
-
277
- public void Fload() {//txt読み込み
278
-
279
- FileListDialog dialog = new FileListDialog(this);
280
-
281
- dialog.setDirectorySelect(false);
282
-
283
- dialog.setOnFileListDialogListener(new FileListDialog.onFileListDialogListener() {//ファイル選択ダイアログ
284
-
285
- @Override
286
-
287
- public void onClickFileList(File file) {
288
-
289
- if (file == null) {
290
-
291
-
292
-
293
- } else {
294
-
295
- String Dfile = Ctext(file.toString());
296
-
297
- EditText text = (EditText) findViewById(R.id.Text_Main);
298
-
299
- try {
300
-
301
- FileInputStream input = openFileInput(Dfile);
302
-
303
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
304
-
305
- String str = "";
306
-
307
- String tmp;
308
-
309
- while ((tmp = reader.readLine()) != null) {
310
-
311
- str = str + tmp + "\n";
312
-
313
- }
314
-
315
- text.setText(str);//.txt読み込み
316
-
317
- reader.close();
318
-
319
- //デバッグ用
320
-
321
- // Log.d("test",str);
322
-
323
- } catch (IOException e) {
324
-
325
- e.printStackTrace();
326
-
327
- }
328
-
329
-
330
-
331
- }
332
-
333
- }
334
-
335
- });
336
-
337
- dialog.show("/data/data/com.example.innovation.f_featnote/files", "select");//ファイル保存場所
338
-
339
- }
340
-
341
-
342
-
343
- public void FSelectSQL() {
344
-
345
- Intent intent = new Intent(MainActivity.this,SqlLoad.class);
346
-
347
- // intent.putExtra();
348
-
349
- startActivity(intent);
350
-
351
- int lis = 0;
352
-
353
- startActivityForResult(intent,lis);
354
-
355
- FloadSQL();
356
-
357
- }
358
-
359
-
360
-
361
- @Override
362
-
363
- protected void onActivityResult(int requestCode,int resultCode,Intent data){
364
-
365
- super.onActivityResult(requestCode,resultCode,data);
366
-
367
- Bundle bundle = data.getExtras();
368
-
369
-
370
-
371
- switch (requestCode){
372
-
373
- case 0:
374
-
375
- if (resultCode == RESULT_OK) {
376
-
377
- String[] str = bundle.getStringArray("key.data");
378
-
379
- EditText tit = (EditText) findViewById(R.id.Text_Title);
380
-
381
- EditText cont = (EditText) findViewById(R.id.Text_Main);
382
-
383
- assert str != null;
384
-
385
- tit.setText(str[0]);
386
-
387
- cont.setText(str[1]);
388
-
389
- }
390
-
391
- }
392
-
393
-
394
-
395
- }
396
-
397
-
398
-
399
- public void FloadSQL(){
400
-
401
-
402
-
403
- }
404
-
405
-
406
-
407
- public String Ctext(String file) {//FileListDialog→openFileInput 変換用
408
-
409
-
410
-
411
- String Cfile = file.substring(51);
412
-
413
- return Cfile;
414
-
415
- }
416
-
417
-
418
-
419
- public String DateG(int flg) {//日時取得+拡張子
420
-
421
- Date date = new Date();
422
-
423
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddkkmmssSSS");//ファイルタイトル
424
-
425
- String path = null;
426
-
427
- switch (flg) {
428
-
429
- case 1://.txt
430
-
431
- path = sdf.format(date) + ".txt";
432
-
433
- break;
434
-
435
- case 2://SQL
436
-
437
- path = sdf.format(date);
438
-
439
- break;
440
-
441
- }
442
-
443
- return path;
444
-
445
- }
446
-
447
-
448
-
449
- public String ContentG(int d) {//テキスト内容取得
450
-
451
- EditText con = (EditText) findViewById(R.id.Text_Main);//0
452
-
453
- EditText tit = (EditText) findViewById(R.id.Text_Title);//1
454
-
455
- SpannableStringBuilder content = (SpannableStringBuilder) con.getText();
456
-
457
- SpannableStringBuilder title = (SpannableStringBuilder) tit.getText();
458
-
459
- String[] str = {content.toString(),title.toString()};
460
-
461
- return str[d];
462
-
463
- }
464
-
465
-
466
-
467
- public SQLiteDatabase SQLopen() {//SQL生成
468
-
469
- SQLiteDatabase sdb;
470
-
471
- SubOpenHelper helper =
472
-
473
- new SubOpenHelper(getApplicationContext(), "Memo.db", 1);
474
-
475
- try {
476
-
477
- sdb = helper.getWritableDatabase();
478
-
479
- } catch (SQLiteException e) {
480
-
481
- //異常終了
482
-
483
- sdb = helper.getReadableDatabase();
484
-
485
- }
486
-
487
- return sdb;
488
-
489
- }
490
-
491
-
492
-
493
- public SQLiteDatabase SQLopen(Context con) {//SQL生成
494
-
495
- SQLiteDatabase sdb;
496
-
497
- SubOpenHelper helper =
498
-
499
- new SubOpenHelper(con, "Memo.db", 1);
500
-
501
- try {
502
-
503
- sdb = helper.getWritableDatabase();
504
-
505
- } catch (SQLiteException e) {
506
-
507
- //異常終了
508
-
509
- sdb = helper.getReadableDatabase();
510
-
511
- }
512
-
513
- return sdb;
514
-
515
- }
516
-
517
- }
518
-
519
-
520
-
521
- class SubOpenHelper extends SQLiteOpenHelper {//SQL
522
-
523
-
524
-
525
- public SubOpenHelper(Context c, String dbname, int version) {
526
-
527
- super(c, dbname, null, version);
528
-
529
- }
530
-
531
-
532
-
533
- public void onCreate(SQLiteDatabase db) {
534
-
535
- db.execSQL("create table MemoContents(_id integer primary key autoincrement,title integer not null,content integer not null,date integer not null)");
536
-
537
- }
538
-
539
-
540
-
541
- @Override
542
-
543
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
544
-
545
-
546
-
547
- }
548
-
549
-
550
-
551
- }
552
-
553
- ```
279
+ public void finish() {
280
+
281
+ super.finish();
282
+
283
+ }
284
+
285
+ }```