java - 为什么 ListView 没有保存到内存中?
问题描述
我的应用程序有问题。有一个带有自定义对象的 ListView,我希望它保存在内存中,并能够在应用程序运行时在其中添加新的东西。问题是我不知道如何将我的 ListView 保存在内存中并显示它。你能帮忙吗?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String nume;
String prenume;
String email;
String eticheta;
int number;
TextView addColegButton = findViewById(R.id.textView3);
ListView nume_ListView = findViewById(R.id.ListView);
String nou = getString(R.string.nou);
String addClassmate = getString(R.string.AddClassmate);
TextView buna = findViewById(R.id.bunaTextView);
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour >= 12 && hour <= 18) {
buna.setText(R.string.bună);
buna.setTextSize(43);
}
if (hour >= 18 && hour <= 24) {
buna.setText(R.string.bunăSeara);
buna.setTextSize(39);
}
if (hour >= 0 && hour <= 12) {
buna.setText(R.string.bunăDimi);
buna.setTextSize(36);
}
ArrayList<nume_list> colegi_list = DataHolder.getInstance().people;
ColegListAdapter adapter1 = new ColegListAdapter(this, R.layout.list_item_layout, colegi_list);
nume_ListView.setAdapter(adapter1);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
nume = null;
prenume = null;
eticheta=null;
email=null;
number= 0;
} else {
nume = extras.getString("NumeFam");
prenume = extras.getString("Prenume");
email = extras.getString("Email");
number = extras.getInt("Number");
eticheta = extras.getString("Eticheta");
String nume_complet = prenume + " " + nume;
nume_list colegNou = new nume_list(nume_complet, eticheta, email, number);
nume_list colegNouv2 = new nume_list(prenume, eticheta, email, number);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom,
(ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.textToast);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,50);
toast.setView(layout);
if (prenume != null && nume != null && eticheta != null) {
colegi_list.add(colegNou);
text.setText(R.string.toast1);
toast.show();
}
else if (nume == null && eticheta != null && prenume != null) {
colegi_list.add(colegNouv2);
text.setText(R.string.toast1);
toast.show();
}
else{
text.setText(R.string.toast2);
toast.show();
}
}
} else {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Schoolbox", MODE_PRIVATE);
nume = sharedPreferences.getString("NumeFam", "");
prenume = sharedPreferences.getString("Prenume","");
eticheta = sharedPreferences.getString("Eticheta","");
email = sharedPreferences.getString("Email","");
number =sharedPreferences.getInt("Number",0);
String nume_complet = prenume + " " + nume;
nume_list colegNou = new nume_list(nume_complet, eticheta, email, number);
nume_list colegNouv2 = new nume_list(prenume, eticheta, email, number);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom,
(ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.textToast);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,50);
toast.setView(layout);
if (prenume != null && nume != null && eticheta != null) {
colegi_list.add(colegNou);
text.setText(R.string.toast1);
toast.show();
}
else if (nume == null && eticheta != null && prenume != null) {
colegi_list.add(colegNouv2);
text.setText(R.string.toast1);
toast.show();
}
else{
text.setText(R.string.toast2);
toast.show();
}
}
addColegButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ColegInfoActivity.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
}
});
//TODO: Create the new classmate activity and set OnClickListener for the button to it.
}
}
THE DETAILS ACTIVITY JAVA 代码(详细信息被添加到一个新对象,稍后在 ListView 中添加一个新项目)
Intent intent1 = new Intent(ColegInfoActivity.this, MainActivity.class);
Bundle extras = new Bundle();
extras.putString("Prenume", prenumeString);
extras.putString("NumeFam", numeFamString);
extras.putString("Eticheta",etichetaString);
extras.putString("Email", emailString);
extras.putInt("Number",number2);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Schoolbox", MODE_PRIVATE);
sharedPreferences.edit().putString("Prenume", prenumeString).apply();
sharedPreferences.edit().putString("NumeFam", numeFamString).apply();
sharedPreferences.edit().putString("Eticheta",etichetaString).apply();
sharedPreferences.edit().putString("Email", emailString).apply();
sharedPreferences.edit().putInt("Number",number2).apply();
解决方案
推荐阅读
- javascript - 循环遍历数组并将图标添加到仅匹配给定 ID
- python - AssertionError at /graphql/ 需要向 GraphQLView 提供 Schema
- c - 如何在 C 中声明指向结构的指针?
- java - 比较两个 Date() 与一天的差异时的一秒差异
- angular - mat stepper 步骤已完成且有效但未完成
- windows - 检索位图图像的调色板
- iframe - 使用 iframe 的聚合器
- azure - 使用 Azure.Storage.Blob v12.9.0 上传到 Azure Blob 存储时身份验证失败
- amazon-web-services - AWS CLI 不会自动为 s3 使用分段上传
- apache - 无法在 ubuntu (aws ec2) 实例上使用 apache2 更新 SSL 证书