在处理高分页面时,我遇到了错误..
此页面上有2个按钮。 ' Altijd'和Deze周'。每个按钮都会显示一个不同的“赢家”列表。
当我想更改列表时,我必须点击其他按钮ONCE。
当我想要回到第一个列表时,我的第一个点击什么也没做。要回到我的第一个列表,我必须点击两次。如果我想回到我的第二个清单(从我的第一个清单),我必须点击两次等等......
有谁知道我为什么要点击两次以及如何修复它?
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Person> arrayOfPersonsAltijd = new ArrayList<Person>();
PersonAdapter adapterAltijd = new PersonAdapter(this,arrayOfPersonsAltijd);
ListView v = (ListView) findViewById(R.id.listHighscores);
adapterAltijd.add(new Person(1, "Naomi", 3454));
adapterAltijd.add(new Person(2, "Steven", 2394));
adapterAltijd.add(new Person(3, "Lieven", 2254));
adapterAltijd.add(new Person(4, "Naomi", 3454));
adapterAltijd.add(new Person(5, "Steven", 2394));
v.setAdapter(adapterAltijd);
final Button buttonWeek = (Button) findViewById(R.id.btnWeek);
buttonWeek.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
// Perform action on click
ArrayList<Person> arrayOfPersonsWekelijks = new ArrayList<Person>();
PersonAdapter adapterWekelijks = new PersonAdapter(MainActivity.this, arrayOfPersonsWekelijks);
ListView vWeek = (ListView) findViewById(R.id.listHighscores);
adapterWekelijks.add(new Person(1, "Lieven", 3454));
adapterWekelijks.add(new Person(2, "Meindert", 2394));
adapterWekelijks.add(new Person(3, "Clara", 2254));
adapterWekelijks.add(new Person(4, "Yargo", 3454));
adapterWekelijks.add(new Person(5, "Bieke", 2394));
vWeek.setAdapter(adapterWekelijks);
Button buttonWekelijks = (Button) findViewById(R.id.btnWeek);
buttonWekelijks.setText("- Deze week -");
Button buttonAltijd = (Button) findViewById(R.id.btnAltijd);
buttonAltijd.setText("Altijd");
}
});
final Button buttonAltijd = (Button) findViewById(R.id.btnAltijd);
buttonAltijd.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
// Perform action on click
ArrayList<Person> arrayOfPersonsAltijd = new ArrayList<Person>();
PersonAdapter adapterAltijd = new PersonAdapter(MainActivity.this,arrayOfPersonsAltijd);
ListView v = (ListView) findViewById(R.id.listHighscores);
adapterAltijd.add(new Person(1, "Naomi", 3454));
adapterAltijd.add(new Person(2, "Steven", 2394));
adapterAltijd.add(new Person(3, "Lieven", 2254));
adapterAltijd.add(new Person(4, "Naomi", 3454));
adapterAltijd.add(new Person(5, "Steven", 2394));
v.setAdapter(adapterAltijd);
Button buttonWekelijks = (Button) findViewById(R.id.btnWeek);
buttonWekelijks.setText("Deze week");
Button buttonAltijd = (Button) findViewById(R.id.btnAltijd);
buttonAltijd.setText("- Altijd -");
}
});
}
}
答案 0 :(得分:0)
在将其分配给适配器之前添加arraylist中的人员。在onclick()方法中都是这样的
ArrayList<Person> arrayOfPersonsAltijd = new ArrayList<Person>();
PersonAdapter adapterAltijd = new PersonAdapter(MainActivity.this,arrayOfPersonsAltijd);
adapterAltijd.add(new Person(1, "Naomi", 3454));
adapterAltijd.add(new Person(2, "Steven", 2394));
adapterAltijd.add(new Person(3, "Lieven", 2254));
adapterAltijd.add(new Person(4, "Naomi", 3454));
adapterAltijd.add(new Person(5, "Steven", 2394));
ListView v = (ListView) findViewById(R.id.listHighscores);
v.setAdapter(adapterAltijd);
Button buttonWekelijks = (Button) findViewById(R.id.btnWeek);
buttonWekelijks.setText("Deze week");
Button buttonAltijd = (Button) findViewById(R.id.btnAltijd);
buttonAltijd.setText("- Altijd -");
或
您可以在adapterWekelijks.notifyDatasetChanged();
v.setAdapter(adapterAltijd);
答案 1 :(得分:0)
我认为你应该使用数组列表来添加适配器。
您可以修改代码以简化如下:您可以使用一个适配器,如果数据集发生更改,请使用notifyDatasetChanged()来更新。
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Person> arrayOfPersons = new ArrayList<Person>();
PersonAdapter adapter = new PersonAdapter(this,arrayOfPersons);
ListView v = (ListView) findViewById(R.id.listHighscores);
arrayOfPersons.add(new Person(1, "Naomi", 3454));
arrayOfPersons.add(new Person(2, "Steven", 2394));
arrayOfPersons.add(new Person(3, "Lieven", 2254));
arrayOfPersons.add(new Person(4, "Naomi", 3454));
arrayOfPersons.add(new Person(5, "Steven", 2394));
v.setAdapter(adapter);
final Button buttonWeek = (Button) findViewById(R.id.btnWeek);
buttonWeek.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
// Perform action on click
arrayOfPersons.clear();
arrayOfPersons.add(new Person(1, "Lieven", 3454));
arrayOfPersons.add(new Person(2, "Meindert", 2394));
arrayOfPersons.add(new Person(3, "Clara", 2254));
arrayOfPersons.add(new Person(4, "Yargo", 3454));
arrayOfPersons.add(new Person(5, "Bieke", 2394));
adapter.notifyDatasetChanged();
Button buttonWekelijks = (Button) findViewById(R.id.btnWeek);
buttonWekelijks.setText("- Deze week -");
Button buttonAltijd = (Button) findViewById(R.id.btnAltijd);
buttonAltijd.setText("Altijd");
}
});
final Button buttonAltijd = (Button) findViewById(R.id.btnAltijd);
buttonAltijd.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
// Perform action on click
arrayOfPersons.clear();
arrayOfPersons.add(new Person(1, "Naomi", 3454));
arrayOfPersons.add(new Person(2, "Steven", 2394));
arrayOfPersons.add(new Person(3, "Lieven", 2254));
arrayOfPersons.add(new Person(4, "Naomi", 3454));
arrayOfPersons.add(new Person(5, "Steven", 2394));
adapter.notifyDatasetChanged();
Button buttonWekelijks = (Button) findViewById(R.id.btnWeek);
buttonWekelijks.setText("Deze week");
Button buttonAltijd = (Button) findViewById(R.id.btnAltijd);
buttonAltijd.setText("- Altijd -");
}
});
}
}
希望这有效!!!