初投稿です。
AndroidStudioで、私はSDカードからテキストファイルを読み込み、
その内容をarrayListへ格納し、最後にListViewへ出力するというアプリを作成しております。
不明点は、上記のアプリのListViewのセルをタップすると編集画面に行き、
保存ボタンを押下すると、EditTextで編集した文字をListView に反映させるのと同時に、テキストファイルにも選択した項目をEditText内の文字に上書きするという所です。
分かり辛い文章で申し訳ありませんがお願い致します。
public void onClick(View v) {
//データ受け取り
Intent intent = getIntent();
String res = intent.getStringExtra("item");
if (v.getId() == R.id.save_button) {
String path = Environment.getExternalStorageDirectory() + "/sample2.txt";
FileOutputStream fos;
Intent send_intent = new Intent();
if(res == null) {
try {
File File = new File(path);
File.getParentFile().mkdir();
fos = new FileOutputStream(File, true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
res = mWriteText.getText().toString();
bw.write(res + "\n");
bw.close();
send_intent.putExtra("send",res);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
res = mWriteText.getText().toString();
File File = new File(path);
File.getParentFile().mkdir();
fos = new FileOutputStream(File, true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write(res +"\n");
bw.close();
send_intent.putExtra("send",res);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
finish();
} else {
// キャンセルボタン押下時
finish();
}
このような形で組んではみたのですが、新たな行に書くのでは無く、上書きする方法が分かりません。よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/05 13:14
2016/02/07 00:24 編集
2016/02/08 14:22
2016/02/09 05:02