所以情况很简单,我有 RegisterActivity,我想创建一个类 user 的数组列表并存储有关 id、电子邮件和密码的数据。然后,我想将该值传递给 LoginActivity,以便我可以使用它来验证用户数据是否已创建。
但是,每次我传递 arraylist 值时,它都会第一次工作,然后当我返回 RegisterActivity 并尝试再次注册时,它不会存储到 arraylist 用户的第二个值,而是存储到第一个值,即当我返回 RegisterActivity 时 arraylist 为空。
这就是我所做的。
在RegisterActivity中
users.add(new User(newId,strEmail,strPass));
Intent i = new Intent(RegisterActivity.this,LoginActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("userData",users);
i.putExtras(bundle);
startActivity(i);
在登录 Activity 中
ArrayList<User> users;
Bundle bundleUser;
bundleUser = getIntent().getExtras();
users = (ArrayList<User>) bundleUser.getSerializable("userData");
你有什么解决办法吗?
请帮助我
最佳答案
Armit和Sniffer都是正确的(我没有时间检查lucefer关于序列化整个数组并一次性存储它的链接)。根据您的情况,您在这里有多种选择。
作为在 Android 中存储值时的一般“经验法则”:
当您想要在用户打开应用程序时临时存储数据(即使应用程序移至后台),但希望每次关闭应用程序时数据都重置为原始值(0、false 或 null,如果未定义):
-利用 SavedInstanceState,它甚至可以保护数据免受屏幕旋转变化的影响。
或者
-创建一个新的 Java 类并在其中存储您的数据。只要应用程序打开(即使应用程序移至后台),存储在 Java 类文件中的数据就会保留其值,而存储在 Activity 中的数据将在方向更改时重置其值,甚至在移动到另一个 Activity 时(如果未显式存储或传递数据)...(下面是 SINGELTON JAVA 类的示例)
public class RegularJavaClass {
public static RegularJavaClass myObj;
public RegularJavaClass(){}
public static RegularJavaClass getInstance() {
if (myObj == null) {
myObj = new RegularJavaClass();
}
return myObj;
}
private int myNumValue;
private String myStringValue;
public int getMyNumValue() {
return myNumValue;
}
public void setMyNumValue(int myNumValue) {
this.myNumValue = myNumValue;
}
public String getMyStringValue() {
return myStringValue;
}
public void setMyStringValue(String myStringValue) {
this.myStringValue = myStringValue;
}
}
那么当你调用这个类来存储东西时
int someNum = 3000;
String someString = "saveThisString";
RegularJavaClass.getInstance().setMyNumValue(someNum);
RegularJavaClass.getInstance().setMyStringValue(someString);
然后当您想要检索此数据时
int retrieveNum = RegularJavaClass.getInstance().getMyNumValue();
String retrieveString = RegularJavaClass.getInstance().getMyStringValue();
当您想要存储少量数据并保留它,即使应用程序关闭并稍后再次打开时(或者直到开发人员卸载或显式更改应用程序)时
-使用共享首选项。将其视为每个 Android 应用程序自动附带的小型数据库。请记住此处仅存储少量数据
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SharedPreferences prefs =
getSharedPreferences("StorageName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//The value type determines the "put" in your case break the
//array into a for statement and save each value
for (String s : myArray) {
//s would be the value
//make a key for your stored value just like a HashMap
editor.putString(key, value).apply();
}
}
});
t.start();
}
}
然后稍后检索您存储的值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs =
getSharedPreferences("StorageName", MODE_PRIVATE);
//Always enter a default value for just in case the storage
//value is empty
String myStoredValue = prefs.getString(key,null);
}
}
当您想要存储大量数据并保留这些数据时,即使应用程序关闭并稍后再次打开(或者直到开发人员卸载或显式更改应用程序)时
-使用 SQLite 等数据库
关于java - 从另一个 Activity 返回时数组列表保持为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52915253/