java - 返回页面时列表视图重置?
问题描述
我正在使用 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);
}
});
}
}
这是访问活动的代码
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));
}
});
要访问包含列表的活动,我在另一个页面上有一个按钮来启动该活动。如果我给出的代码太多或不够,我是 Java 和 Stack Overflow 的新手。
解决方案
您需要保存来自arraylist的信息并在您进入页面时加载此信息(onCreate方法)
@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);
.
.
}
您可以在每次添加新项目或侦听 onBackButton 操作时保存信息。
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
.
.
saveExpesiveList(arrayList);
}
});
推荐阅读
- django - Dockerize django 应用程序以及黄瓜测试
- mongodb - Mongodb 建议我创建 collection.sessions 索引?
- php - file_get_contents HTTP 请求失败错误
- jira - Jira JQL 运算符和 + 和 + 和
- html - 如何使徽标的可点击区域与导航栏的徽标大小相同?
- python - 给出字符串输入并将它们存储在一个列表中,没有数量限制
- docker - 如何连续将文件复制到docker
- html - Bootstrap Modal 搞砸了 Carousel CSS
- python - 带有 skbio 的二维 PCoA 图
- google-chrome - Chrome 扩展程序不同步