質問編集履歴
1
教えて頂いた方法にて修正を行ってみました
test
CHANGED
File without changes
|
test
CHANGED
@@ -281,3 +281,175 @@
|
|
281
281
|
}
|
282
282
|
|
283
283
|
```
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
onBackPressed()を使った方法で修正してみました。
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
```java
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
/* import 省略 */
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
private ListView listView;
|
312
|
+
|
313
|
+
private CustomAdapter adapter;
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
private String lastPath; // 追加
|
318
|
+
|
319
|
+
private String parentPath; // 追加
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
@Override
|
326
|
+
|
327
|
+
protected void onCreate(Bundle savedInstanceState) {
|
328
|
+
|
329
|
+
super.onCreate(savedInstanceState);
|
330
|
+
|
331
|
+
setContentView(R.layout.activity_main);
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
// リスト項目が選択された時のイベントを追加
|
336
|
+
|
337
|
+
listView = (ListView) findViewById(R.id.list_item);
|
338
|
+
|
339
|
+
listView.setOnItemClickListener(this);
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
// ファイルパスを取得
|
344
|
+
|
345
|
+
String path = Environment.getExternalStorageDirectory().getPath();
|
346
|
+
|
347
|
+
lastPath = Environment.getExternalStorageDirectory().getParent(); // 追加
|
348
|
+
|
349
|
+
fill(new File(path));
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
// アクティビティ内の表示内容構築
|
356
|
+
|
357
|
+
private void fill(File fileDirectory) {
|
358
|
+
|
359
|
+
// タイトル
|
360
|
+
|
361
|
+
setTitle(fileDirectory.getAbsolutePath());
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
readLocalStrage(fileDirectory.getPath());
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
// ファイルリスト
|
370
|
+
|
371
|
+
File[] aFile = fileDirectory.listFiles();
|
372
|
+
|
373
|
+
List<String> listString = new ArrayList<>();
|
374
|
+
|
375
|
+
if (aFile != null) {
|
376
|
+
|
377
|
+
for (File fileTemp : aFile) {
|
378
|
+
|
379
|
+
listString.add(new aFile.getName());
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
parentPath = fileDirectory.getParent(); // 追加
|
388
|
+
|
389
|
+
// 親フォルダに戻るパスの追加
|
390
|
+
|
391
|
+
if (fileDirectory.getParent() != null) {
|
392
|
+
|
393
|
+
listString.add(0, new String("..", new File(fileDirectory.getParent())));
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
// リストビューに表示
|
400
|
+
|
401
|
+
adapter = new CustomAdapter(this, listString);
|
402
|
+
|
403
|
+
listView.setAdapter(adapter);
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
// リストをクリックした時の動作
|
410
|
+
|
411
|
+
@Override
|
412
|
+
|
413
|
+
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
String String = adapter.getItem(pos);
|
418
|
+
|
419
|
+
if (String.getFile().isDirectory()) {
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
fill(String.getFile());
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
// 追加
|
432
|
+
|
433
|
+
@Override
|
434
|
+
|
435
|
+
public void onBackPressed(){
|
436
|
+
|
437
|
+
if(parentPath.equals(lastPath)){
|
438
|
+
|
439
|
+
// トップなら終了
|
440
|
+
|
441
|
+
super.onBackPressed();
|
442
|
+
|
443
|
+
}else {
|
444
|
+
|
445
|
+
fill(new File(parentPath));
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
```
|