F
F
feniksdv2020-08-01 22:17:10
Java
feniksdv, 2020-08-01 22:17:10

How to add data to a file in SharedPreferences, and not replace it?

There is an input field and a save button, I enter a value into the line, press the button, the data is written to the file, I enter a new value, I press the button again, the data is overwritten, and I want them to be added to the existing lines in the file.
MainActivity.java

package ru.l.sharedpreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private Button buttonOk;
    private EditText name;

    SharedPreferences test;

    final String NAME = "Тут лежит введенное Имя";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonOk = (Button) findViewById(R.id.buttonOk);
        name = (EditText) findViewById(R.id.name);

        View.OnClickListener clickButton = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test();
            }
        };
        //присваеваем обработчик нажатия кнопки
        buttonOk.setOnClickListener(clickButton);
    }

    void test(){
        test = getSharedPreferences("name", MODE_PRIVATE);
        SharedPreferences.Editor ed = test.edit();
        ed.putString(NAME, name.getText().toString());
        ed.commit();
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <Button
        android:id="@+id/buttonOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="222dp"
        tools:layout_editor_absoluteY="0dp" />

</LinearLayout>


name.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="Тут лежит введенное Имя">Name1</string> //вот тут то я и хочу сохранять всю историю, всех символов, которые были введины.
</map>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
feniksdv, 2020-08-01
@feniksdv

You need to use SQLite or try to bind the counter
ed.putString(NAME+i, name.getText().toString());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question