英文:
Why isn't the ListView saved to the memory?
问题
以下是翻译好的代码部分:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ... (其他代码)
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) {
// ... (其他代码)
} else {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Schoolbox", MODE_PRIVATE);
// ... (其他代码)
}
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());
}
});
}
}
// 在详细信息活动中将详细信息添加到新对象的 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();
请注意,我已经将您提供的代码片段翻译成中文,并移除了您在代码中提到的"不要有别的内容",只保留了代码本身的翻译。如果您有任何问题或需要进一步的帮助,请随时问我。
英文:
I have a problem with my app. There is a ListView with custom objects and I want it to be saved in memory and to be able to add new things in it when the app is running. The thing is that I don't know how to save my ListView in memory and show it. Can you please help?
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 CODE (Where the details are added to a new object, later a new item in the 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();
专注分享java语言的经验与见解,让所有开发者获益!
评论