Java 返回页面时是否重置列表视图?

Java 返回页面时是否重置列表视图?,java,android,android-listview,Java,Android,Android Listview,我正在使用Android Studio制作一个应用程序,在这个应用程序中,我有一个页面,其中包含一个列表,您可以向其中添加项目。一切正常,但当我离开页面返回时,列表消失了?当我返回到显示我之前创建的列表的页面时,我如何做到这一点 package com.example.graded_unit; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; i

我正在使用Android Studio制作一个应用程序,在这个应用程序中,我有一个页面,其中包含一个列表,您可以向其中添加项目。一切正常,但当我离开页面返回时,列表消失了?当我返回到显示我之前创建的列表的页面时,我如何做到这一点

package com.example.graded_unit;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class Test extends AppCompatActivity {


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

        // Displays costs
        SharedPreferences sharedPref = getSharedPreferences("Expense Value", Context.MODE_PRIVATE);
        Float number = sharedPref.getFloat("Expense Value",0);
        TextView costView = findViewById(R.id.costView);
        String cost = Float.toString(number);
        costView.setText("£" + cost);

        ListView listView;

        //Creates bar at the top with title of page and back button
        getSupportActionBar().setTitle("Test List");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        //Assigns ListView to the list on the design page
        listView=(ListView)findViewById(R.id.listview);

        //Creates an array list for strings
        final ArrayList<String> arrayList=new ArrayList<>();

        //Creates an array adapter for the arrayList
        ArrayAdapter arrayAdapter=new ArrayAdapter(Test.this, android.R.layout.simple_list_item_1, arrayList);

        //Sets the adapter to the listView so you can view the list on the page
        listView.setAdapter(arrayAdapter);



        //Creates the button for adding a new item to the list
        FloatingActionButton addBtn = findViewById(R.id.addBtn);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText testCost = findViewById(R.id.testCost);
                EditText testText = findViewById(R.id.testText);
                String cost = testCost.getText().toString();
                String expense = testText.getText().toString();

                //Adds expense to the list with a toast to confirm
                arrayList.add(expense + ": £" + cost);
                Toast.makeText(Test.this, "Added " + expense + ": £" + cost + " to the list!", Toast.LENGTH_SHORT).show();


                Float costs = Float.parseFloat(cost);

                //Saves the total expenses value
                SharedPreferences sharedPref = getSharedPreferences("Expense Value", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                Float test = sharedPref.getFloat("Expense Value", 0);
                costs = costs + test;
                editor.putFloat("Expense Value", costs);
                editor.apply();

                //Displays the total expenses value at the top
                TextView costView = findViewById(R.id.costView);
                String expenseValue = costs.toString();
                costView.setText("£" + expenseValue);

            }
        });

    }
}

要访问包含列表的活动,我在另一个页面上有一个启动该活动的按钮。
我是Java新手,如果我给出的代码太多或不够,我会为堆栈溢出感到抱歉。

您需要保存arraylist中的信息,并在进入页面时加载此信息(onCreate方法)


你能发布你如何退出活动并返回活动的代码吗?退出活动的代码已经在那里了我使用getSupportActionBar制作了一个后退按钮,你可以单击一切看起来都很好,当你返回活动时,你能检查日志吗?你的意思是什么?当你返回活动时,你能检查logcat吗?我如何保存列表并加载它呢?您可以在SharedReference中找到如何加载和保存arraylist。他们正在使用gson并保存一个字符串。或者您可以使用本地数据库和SQLLite抱歉,我是java新手,似乎无法让它工作,您是否可以发布一些示例代码或其他内容?
Button expensesBtn = (Button) findViewById(R.id.expensesBtn);
        expensesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Account.this, Test.class));
            }
        });
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .
    .
    //Load information from database/sharedpreferene/file wherever
    final ArrayList<String> arrayList = getExpensiveList();

    ArrayAdapter arrayAdapter = new ArrayAdapter(Test.this, android.R.layout.simple_list_item_1, arrayList);
    .
    .
}
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            .
            .
            saveExpesiveList(arrayList);
    }
});