英文:
Why is my first array item still there if I put a condition where it must be shown only when nothing is in the array?
问题
我有一个ListView,如果里面没有任何内容,就会显示一个"NEW"项目。问题是,即使数组中有更多元素,"NEW"项目也会显示出来。有什么想法吗?
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
nume = null;
prenume = null;
nume_list event_nou = new nume_list(nou, addClassmate);
if (colegi_list.isEmpty()) {
colegi_list.add(event_nou);
}
} else {
nume = extras.getString("NumeFam");
prenume = extras.getString("Prenume");
String nume_complet = prenume + " " + nume;
nume_list colegNou = new nume_list(nume_complet, "");
if (prenume != null && nume != null) {
colegi_list.add(colegNou);
}
nume_list colegNouv2 = new nume_list(prenume, "");
if (nume == null) {
colegi_list.add(colegNouv2);
}
}
} else {
nume = (String) savedInstanceState.getSerializable("NumeFam");
prenume = (String) savedInstanceState.getSerializable("Prenume");
}
// 从其他活动的按钮:
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle extras = new Bundle();
extras.putString("Prenume", prenumeString);
extras.putString("NumeFam", numeFamString);
Intent intent1 = new Intent(ColegInfoActivity.this, MainActivity.class);
intent1.putExtras(extras);
startActivity(intent1, ActivityOptions.makeSceneTransitionAnimation(ColegInfoActivity.this).toBundle());
}
});
备注:当按钮被按下时,它会转到另一个活动,数据应该包含在一个新项目中。每次都会有一个新项目(这是正常的),但"NEW"仍然存在。
英文:
I have a ListView where if nothing is in it, a "NEW" item is shown. The problem is that the "NEW" item is shown even if there are more elements in the array. Ideas?
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
nume = null;
prenume = null;
nume_list event_nou = new nume_list(nou, addClassmate);
if (colegi_list.isEmpty()) {
colegi_list.add(event_nou);
}
} else {
nume = extras.getString("NumeFam");
prenume = extras.getString("Prenume");
String nume_complet = prenume + " " + nume;
nume_list colegNou = new nume_list(nume_complet, "");
if (prenume != null && nume!=null) {
colegi_list.add(colegNou);
}
nume_list colegNouv2 = new nume_list(prenume, "");
if (nume==null) {
colegi_list.add(colegNouv2);
}
}
} else {
nume = (String) savedInstanceState.getSerializable("NumeFam");
prenume = (String) savedInstanceState.getSerializable("Prenume");
}
The button from the other activity:
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle extras = new Bundle();
extras.putString("Prenume", prenumeString);
extras.putString("NumeFam", numeFamString);
Intent intent1 = new Intent(ColegInfoActivity.this, MainActivity.class);
intent1.putExtras(extras);
startActivity(intent1, ActivityOptions.makeSceneTransitionAnimation(ColegInfoActivity.this).toBundle());
}
PS: when the button is pressed it goes to the other activity where the data should be included in a new item. There is every time a new item (that's ok) but the NEW is still there.
答案1
得分: 1
根据您所说,“'new'”是在列表中没有项目时应包含的项目。就像显示“新建,创建一个项目!”一样,因此当您添加到列表中或列表保持新状态时,必须删除此元素。
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
nume = null;
prenume = null;
nume_list event_nou = new nume_list(nou, addClassmate);
if (colegi_list.isEmpty()) {
colegi_list.add(event_nou);
}
} else {
colegi_list.remove(0); //因为“NEW”位于0索引
nume = extras.getString("NumeFam");
prenume = extras.getString("Prenume");
String nume_complet = prenume + " " + nume;
nume_list colegNou = new nume_list(nume_complet, "");
if (prenume != null && nume != null) {
colegi_list.add(colegNou);
}
nume_list colegNouv2 = new nume_list(prenume, "");
if (nume == null) {
colegi_list.add(colegNouv2);
}
}
} else {
nume = (String) savedInstanceState.getSerializable("NumeFam");
prenume = (String) savedInstanceState.getSerializable("Prenume");
}
英文:
According to what you said ' "new" is the item which should be included when no item is in the list. It's like showing " New, create an item!" ', so you have to delete this element when you add to the list or the list is going to keep new.
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
nume = null;
prenume = null;
nume_list event_nou = new nume_list(nou, addClassmate);
if (colegi_list.isEmpty()) {
colegi_list.add(event_nou);
}
} else {
colegi_list.remove(0); //as NEW is on 0 index
nume = extras.getString("NumeFam");
prenume = extras.getString("Prenume");
String nume_complet = prenume + " " + nume;
nume_list colegNou = new nume_list(nume_complet, "");
if (prenume != null && nume!=null) {
colegi_list.add(colegNou);
}
nume_list colegNouv2 = new nume_list(prenume, "");
if (nume==null) {
colegi_list.add(colegNouv2);
}
}
} else {
nume = (String) savedInstanceState.getSerializable("NumeFam");
prenume = (String) savedInstanceState.getSerializable("Prenume");
}
答案2
得分: 0
你是否已经在代码的其他地方像这样保存了你的信息:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("StringKeyForBoolean", false);
savedInstanceState.putDouble("StringKeyForDouble", 3.4);
savedInstanceState.putInt("StringKeyForInteger", 5);
savedInstanceState.putString("StringKey", "Folio3/blog");
// 等等。
}
英文:
Have you saved your information somewhere else in the code like this:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("StringKeyForBoolean", false);
savedInstanceState.putDouble("StringKeyForDouble", 3.4);
savedInstanceState.putInt("StringKeyForInteger", 5);
savedInstanceState.putString("StringKey", "Folio3/blog");
// etc.
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论