我正在使用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的新手,对不起,如果我给出了太多或不足的代码。
答案 0 :(得分:2)
您需要保存来自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);
.
.
}
每次添加新项目或在BackButton上收听操作时,您都可以保存信息。
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
.
.
saveExpesiveList(arrayList);
}
});