質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1444閲覧

端末内のファイルに追加で書き込みたい。

KU_Slab19

総合スコア5

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2019/10/15 09:01

こんにちは
早速ですが、、、
今のところ、EditTextで書き込んだものを端末内に保存し、アプリを閉じて
読み込むと、前のデータが表示されるところまでできています。

デバイス・ファイル・エクスプローラーで保存先にちゃんとあるのも確認しました。
そこで次に、すでにあるファイルに、新しい文字を追加で書き込みたいと思っているのですが、上手くいきません。
MODE_PRIVATEではなく、MODE_APPENDを使うらしいですが、赤波線が表示されます。
PRIVATEのところのコードに付け加える必要があるのか、コード自体が間違っているのかを教えていただきたいです。
よろしくお願いします。

java

1package com.example.prectice_10152019; 2import android.content.SharedPreferences; 3import android.os.Bundle; 4import android.view.View; 5import android.widget.Button; 6import android.widget.EditText; 7import android.widget.TextView; 8 9import androidx.appcompat.app.AppCompatActivity; 10 11 12public class MainActivity extends AppCompatActivity { 13 private SharedPreferences dataStore; 14 private EditText editText; 15 private TextView textWrite, textRead; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 // "DataStore"という名前でインスタンスを生成 23 dataStore = getSharedPreferences("DataStore", MODE_PRIVATE); 24 25 editText = findViewById(R.id.edit_text1); 26 textWrite = findViewById(R.id.text_write1); 27 textRead = findViewById(R.id.text_read1); 28 29 Button buttonWrite = findViewById(R.id.button_write1); 30 buttonWrite.setOnClickListener(new View.OnClickListener() { 31 @Override 32 public void onClick(View v) { 33 // エディットテキストのテキストを取得 34 String text = editText.getText().toString(); 35 textWrite.setText(text); 36 // 入力文字列を"input"に書き込む 37 SharedPreferences.Editor editor = dataStore.edit(); 38 editor.putString("input", text); 39 //editor.commit(); 40 editor.apply(); 41 } 42 }); 43 44 Button buttonRead = findViewById(R.id.button_read1); 45 buttonRead.setOnClickListener(new View.OnClickListener() { 46 @Override 47 public void onClick(View v) { 48 // "input"から読み出す、何もないときは"Nothing"を返す 49 String str = dataStore.getString("input", "Nothing"); 50 if(!str.equals("Nothing")) { 51 textRead.setText(str); 52 } 53 } 54 }); 55 } 56}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:background="#6FBBCF" 9 tools:context=".MainActivity"> 10 11 <EditText 12 android:id="@+id/edit_text1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:autofillHints="@string/hint" 16 android:hint="@string/hint" 17 android:inputType="text" 18 android:textSize="30dp" 19 app:layout_constraintBottom_toBottomOf="parent" 20 app:layout_constraintLeft_toLeftOf="parent" 21 app:layout_constraintRight_toRightOf="parent" 22 app:layout_constraintTop_toTopOf="parent" 23 app:layout_constraintVertical_bias="0.1" /> 24 25 <Button 26 android:id="@+id/button_write1" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:layout_marginStart="40dp" 30 android:layout_marginEnd="40dp" 31 android:text="@string/write" 32 app:layout_constraintBottom_toBottomOf="parent" 33 app:layout_constraintLeft_toLeftOf="parent" 34 app:layout_constraintRight_toRightOf="parent" 35 app:layout_constraintTop_toTopOf="parent" 36 app:layout_constraintVertical_bias="0.3" /> 37 38 39 <TextView 40 android:id="@+id/text_write1" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:gravity="center" 44 android:textSize="50dp" 45 app:layout_constraintBottom_toBottomOf="parent" 46 app:layout_constraintLeft_toLeftOf="parent" 47 app:layout_constraintRight_toRightOf="parent" 48 app:layout_constraintTop_toTopOf="parent" 49 app:layout_constraintVertical_bias="0.4" /> 50 51 52 <Button 53 android:id="@+id/button_read1" 54 android:layout_width="match_parent" 55 android:layout_height="wrap_content" 56 android:layout_marginStart="40dp" 57 android:layout_marginEnd="40dp" 58 android:text="@string/read" 59 app:layout_constraintBottom_toBottomOf="parent" 60 app:layout_constraintLeft_toLeftOf="parent" 61 app:layout_constraintRight_toRightOf="parent" 62 app:layout_constraintTop_toTopOf="parent" 63 app:layout_constraintVertical_bias="0.6" /> 64 65 <TextView 66 android:id="@+id/text_read1" 67 android:layout_width="wrap_content" 68 android:layout_height="wrap_content" 69 android:gravity="center" 70 android:textColor="#0000ff" 71 android:textSize="50dp" 72 app:layout_constraintBottom_toBottomOf="parent" 73 app:layout_constraintLeft_toLeftOf="parent" 74 app:layout_constraintRight_toRightOf="parent" 75 app:layout_constraintTop_toTopOf="parent" 76 app:layout_constraintVertical_bias="0.7" /> 77 78</androidx.constraintlayout.widget.ConstraintLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

MODE_PRIVATEではなく、MODE_APPENDを使うらしいですが、赤波線が表示されます
dataStore = getSharedPreferences("DataStore", MODE_PRIVATE);

この getSharedPreferences の MODE_PRIVATE を MODE_APPEND にしてみたということでしょうか.
だとすれば「考え方が間違っている」と思います.

まず, getSharedPreferences の第二引数は MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, または MODE_MULTI_PROCESS で, MODE_APPEND はありません.

getSharedPreferences

Parameters

name String: Desired preferences file.
mode int: Operating mode. Value is either 0 or a combination of MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, and MODE_MULTI_PROCESS

モードとして追加というのはありませんので, 既存のデータを取得・追加・書き込みという操作を行うことになると思います.

投稿2019/10/15 14:39

jimbe

総合スコア12696

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KU_Slab19

2019/10/16 03:12

ご指摘ありがとうございます。 では、最初からDataStoreというファイルを作っておいて、書き込み→追加をするという手順ですね。 今のコードだと、毎回DataStoreを新しく作ってしまうということでしょうか
jimbe

2019/10/16 03:49

ファイルは無ければ作成され, あれば上書きされると思います. SharedPreferences の基本機能は「値を保存する」ことで, ファイルを作成することではありません. ファイルが出来るのはそういう実装というだけではないでしょうか. ですので, もし普通にファイルを作成したいのでしたら, ローカルファイルを使用するべきかと思います.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問