ArrayList 中的项目被替换而不是追加

Item in the ArrayList gets replaced instead of appending

我正在尝试使用 SharedPreferences 将保存在 EditActivity 中的 ArrayList 中的所有项目带到 MainActivity。但是只有最后输入的数据才会显示在第一个 MainActivity 的 ListView 中。我怎样才能让所有输入到 EditText 中的文本在 ListView 中显示为不同的项目,而不是替换之前的项目。 这是我的代码:

MainActivity

public class MainActivity extends AppCompatActivity {

private ListView list;
private Button nextButton;

ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.list);
    nextButton = (Button) findViewById(R.id.nextButton);

        loadData();

    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
    list.setAdapter(adapter);
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            nextActivity();
        }
    });
}
public void nextActivity(){
    Intent intent = new Intent(this, EditActivity.class);
    startActivity(intent);
}

public void loadData(){
    SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sp.getString("list", null);
    Type type = new TypeToken<ArrayList<String>>() {}.getType();
    arrayList = gson.fromJson(json, type);

    if(arrayList == null){
        arrayList = new ArrayList<String>();
    }
}}

EditActivity

public class EditActivity extends AppCompatActivity {

private EditText input;
private Button addButton;

public ArrayList<String> list = new ArrayList<String>();
public ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit);

    input = (EditText) findViewById(R.id.input);
    addButton = (Button) findViewById(R.id.addButton);

    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String getInput = input.getText().toString();
            adapter.addAll(getInput);
            saveData();
            input.setText(""); //clear the value in the edit text
        }
    });

}
public void saveData(){
    SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    Gson gson = new Gson();
    String json = gson.toJson(list);
    editor.putString("list", json);
    editor.apply();
}}

我认为您在保存之前忘记将项目添加到列表中,因此结果是您只保存了最后一项。

addButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String getInput = input.getText().toString();
        list.add(getInput); //add this line
        adapter.addAll(getInput);
        saveData();
        input.setText(""); //clear the value in the edit text
    }
});