なぜMainActivityでのレイアウトのlistViewの中身が表示されま
java
1public class MainActivity extends AppCompatActivity { 2 // ListView 用アダプタ 3 SimpleAdapter mAdapter = null; 4 // ListView に設定するデーた 5 List<Map<String, String>> mList = null; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 13 ListView list = (ListView)findViewById(R.id.listView); 14 mList = new ArrayList<Map<String, String>>(); 15 16 mAdapter = new SimpleAdapter(this, 17 mList, 18 //独自で定義したlayoutを適応する 19 android.R.layout.simple_list_item_2, 20 new String[]{"text1","text2","text3"}, //Map側のkey 21 new int[]{android.R.id.text1,android.R.id.text2,R.id.eTxtData} //layout側のTextViewId 表示させたいMapのkey順に合わせる 22 ); 23 24 // ListView にアダプターをセット 25 list.setAdapter(mAdapter); 26 27 28 // ListView のアイテム選択イベント 29 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 30 31 @Override 32 public void onItemClick( 33 AdapterView<?> parent, View view, int pos, long id) { 34 // 編集画面に渡すデータをセットし、表示 35 Intent intent = new Intent(MainActivity.this, EditActivity.class); 36 intent.putExtra("NAME", mList.get(pos).get("filename")); 37 intent.putExtra("TITLE", mList.get(pos).get("title")); 38 intent.putExtra("CONTENT", mList.get(pos).get("content")); 39 intent.putExtra("DATA",mList.get(pos).get("data")); 40 startActivity(intent); 41 } 42 }); 43 44 // ListView をコンテキストメニューに登録 45 registerForContextMenu(list); 46 } 47 48 49 @Override 50 protected void onResume() { 51 super.onResume(); 52 53 // ListView 用アダプタのデータをクリア 54 mList.clear(); 55 56 // アプリの保存フォルダ内のファイル一覧を取得 57 String savePath = this.getFilesDir().getPath().toString(); 58 File[] files = new File(savePath).listFiles(); 59 // ファイル名の降順でソート 60 Arrays.sort (files, Collections.reverseOrder()); 61 // テキストファイル(*.txt)を取得し、ListView用アダプタのリストにセット 62 for (int i=0; i<files.length; i++) { 63 String fileName = files[i].getName(); 64 if (files[i].isFile() && fileName.endsWith(".txt")) { 65 String title = null; 66 String content = null; 67 String data=null; 68 69 // ファイルを読み込み 70 try { 71 // ファイルオープン 72 InputStream in = this.openFileInput(fileName); 73 BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); 74 // タイトル(1行目)を読み込み 75 title= reader.readLine(); 76 77 78 // 内容(2行目)を読み込み 79 content = reader.readLine(); 80 //日付(3行目)を読み込み 81 data=reader.readLine(); 82 83 84 // ファイルクローズ 85 reader.close(); 86 in.close(); 87 } catch (Exception e) { 88 Toast.makeText(this, "error", Toast.LENGTH_LONG).show(); 89 } 90 91 // ListView用のアダプタにデータをセット 92 Map<String, String> map = new HashMap<String, String>(); 93 map.put("filename", fileName); 94 map.put("title", title); 95 map.put("content", content); 96 map.put("data",data); 97 mList.add(map); 98 99 100 101 } 102 } 103 104 // ListView のデータ変更を表示に反映 105 mAdapter.notifyDataSetChanged(); 106 } 107 108
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/03 14:46
2017/04/03 14:51 編集
2017/04/03 14:53