リストビューのスクロールに問題があります。私は次のようなクラスを持っています:
private int itemsPerPage = 6;
private int count = 0;
private boolean loadingMore = false;
private ArrayList<Item> item;
private ListItemAdapter adapter;
View footerView;
private JSONArray array;
private String json;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
lvListItem = (ListView) findViewById(R.id.lvListItem);
item = new ArrayList<Item>();
footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_footer, null, false);
json = new JSONParse()
.getData("....");
array = new JSONArray(json);
load();
adapter = new ListItemAdapter(this, 0, item);
lvListItem.addFooterView(footerView);
lvListItem.setAdapter(adapter);
lvListItem.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
int lastInScreen = firstVisibleItem + visibleItemCount;
// is the bottom item visible & not loading more already ? Load
// more !
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
Thread thread = new Thread(null, loadData);
thread.start();
}
}
});
}
public void load(){
for (int i = 0; i < itemsPerPage; i++) {
if (count == array.length()) {
lvListItem.removeFooterView(footerView);
break;
}
/*****Code to load data*******/
}
}
private Runnable loadData = new Runnable() {
@Override
public void run() {
loadingMore = true;
item = new ArrayList<Item>();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
load();
runOnUiThread(returnRes);
}
};
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if (item != null && item.size() > 0) {
for (int i = 0; i < item.size(); i++)
adapter.add(item.get(i));
}
if (count == array.length())
lvListItem.removeFooterView(footerView);
adapter.notifyDataSetChanged();
// Done loading more.
loadingMore = false;
}
};
load()だけを使用すると、実行され、リストビューにデータが表示されます。しかし、リストビューをスクロールすると、エラーがスローされます:
D/AndroidRuntime(1635): Shutting down VM W/dalvikvm(1635): threadid=1: thread exiting with uncaught exception (group=0x40a13300) E/AndroidRuntime(1635): FATAL EXCEPTION: main E/AndroidRuntime(1635): java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6 E/AndroidRuntime(1635): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) E/AndroidRuntime(1635): at java.util.ArrayList.get(ArrayList.java:304)
助けが必要です