考虑以下示例,列表项的顺序在重写 getView() 方法之前是正确的。每当我重写它以完成一些额外的工作(示例中未显示)时,项目的顺序就会变得一团糟。我向下滚动以查看一个数字,然后向上滚动以再次查看相同的数字。这种奇怪行为的原因是什么,我该如何解决?
package com.staticlist;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
String[] items = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",
"34", "35", "36", "37", "38", "39", "40" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MyListAdapter(this, R.layout.main, items));
}
}
class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter(MainActivity mainActivity, int main, String[] items) {
super(mainActivity, main, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null) {
row = super.getView(position, convertView, parent);
} else {
row = convertView;
}
return row;
}
}