Java 我在OnResume()中有必要的代码,而不是OnCreate(),但当我转到其他活动然后返回时,ListView中的数组数据仍然会消失
正如标题所示,当我离开活动然后返回活动时,我的listview将消失。我搜索了其他问题,发现我有一个问题,因为一切都在OnCreate中。我把它移到了OnResume,但我的应用程序完全没有改变 我删除了一些不相关的东西,因为它说我有太多的代码,所以如果一些括号搞乱了,这就是原因。我在AndroidStudio中没有发现任何括号错误 这是我的密码:Java 我在OnResume()中有必要的代码,而不是OnCreate(),但当我转到其他活动然后返回时,ListView中的数组数据仍然会消失,java,android,Java,Android,正如标题所示,当我离开活动然后返回活动时,我的listview将消失。我搜索了其他问题,发现我有一个问题,因为一切都在OnCreate中。我把它移到了OnResume,但我的应用程序完全没有改变 我删除了一些不相关的东西,因为它说我有太多的代码,所以如果一些括号搞乱了,这就是原因。我在AndroidStudio中没有发现任何括号错误 这是我的密码: public class Income extends MainActivity { EditText enterIncomeEditTe
public class Income extends MainActivity {
EditText enterIncomeEditText;
EditText enterIncomeNamesEditText;
Button addIncomeButton;
ListView incomeAmountListView;
ListView incomeNameListView;
// new String Array for listview
private String[] incomeArray;
//create getter
public String[] getIncomeArray() {
return incomeArray;
}
//new string array for listview names
private String[] incomeNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_income);
//initialize new String array
incomeArray = new String[]{
""
};
//initialize new String Array for listview
incomeNames = new String[]{
""
};
//connect EditTexts
enterIncomeEditText = findViewById(R.id.enter_income_edit_text);
enterIncomeNamesEditText = findViewById(R.id.enter_income_names_edit_text);
addIncomeButton = findViewById(R.id.add_income_button);
incomeAmountListView = findViewById(R.id.income_amount_list_view);
incomeNameListView = findViewById(R.id.income_name_list_view);
}
//override onResume so things don't get deleted each time the activity changes
@Override
protected void onResume() {
super.onResume();
// Create a List from String Array elements
final List<String> income_list = new ArrayList<>(Arrays.asList(incomeArray));
// Create an ArrayAdapter from List
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>
(this, android.R.layout.simple_list_item_1, income_list);
//Bind ListView with ArrayAdapter items
incomeAmountListView.setAdapter(arrayAdapter);
// Create a List from String Array elements
final List<String> income_names_list = new ArrayList<>(Arrays.asList(incomeNames));
// Create an ArrayAdapter from List
final ArrayAdapter<String> arrayAdapterNames = new ArrayAdapter<>
(this, android.R.layout.simple_list_item_1, income_names_list);
//Bind ListView with ArrayAdapter items
incomeNameListView.setAdapter(arrayAdapterNames);
//add everything for navigation drawer
mDrawerLayout = findViewById(R.id.drawer_layout);
addIncomeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (TextUtils.isEmpty(enterIncomeEditText.getText()) ||
TextUtils.isEmpty(enterIncomeNamesEditText.getText())) {
Toast.makeText(Income.this, "Income Name or Amount Empty", Toast.LENGTH_SHORT).show();
} else {
String income = enterIncomeEditText.getText().toString() + "\n";
String incomeName = enterIncomeNamesEditText.getText().toString() + "\n";
//add new expenses to list and notify data set changed
income_list.add(income);
income_names_list.add(incomeName);
arrayAdapter.notifyDataSetChanged();
arrayAdapterNames.notifyDataSetChanged();
}
enterIncomeNamesEditText.setText("");
enterIncomeEditText.setText("");
});
}
}
若要在处理其他活动时保留当前活动的数据,您需要覆盖
public void onSaveInstanceState(Bundle outState)
方法,将所有数据保存在捆绑包中。这样,如果您转到另一个活动,当您按back键时,您的数据将保留在捆绑包中,然后您可以使用另一种方法检索数据
public void onRestoreInstanceState(Bundle savedInstanceState)
然后,您可以从该捆绑包中获取数据,并将其显示在活动中
希望这对您有所帮助。要在您处理其他活动时保留当前活动的数据,您需要覆盖
public void onSaveInstanceState(Bundle outState)
方法,将所有数据保存在捆绑包中。这样,如果您转到另一个活动,当您按back键时,您的数据将保留在捆绑包中,然后您可以使用另一种方法检索数据
public void onRestoreInstanceState(Bundle savedInstanceState)
然后,您可以从该捆绑包中获取数据,并将其显示在活动中
希望这能对您有所帮助。您遇到的错误是什么?我没有遇到错误,只是切换活动时填充了数组的listview消失了。您遇到的错误是什么?我没有遇到错误,只是切换活动时填充了数组的listview消失了。就是这样!谢谢,就是这样!谢谢