我正在用listview實現分頁。我試圖在加載我的一些數據時添加頁腳視圖,並在列表視圖的末尾刪除頁腳視圖時,在列表視圖的最後一頁到達setOnScrollListener的最後一頁。在最後一頁上得到崩潰時,得到IndexOutOfBoundsException的問題:索引2無效,大小爲2java.lang.IndexOutOfBoundsException:索引2無效,大小爲2,而在Android的listview結尾刪除footerview?
我加入footerview初始化列表
ListView listView;
int pageCount = 0;
View footer;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = (ListView) findViewById(R.id.load);
// Add footer view
footer = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.prohressbar, null, false);
listView.addFooterView(footer);
List<All_Post> allDesc = dbhelper.getAllDescriptions(OFFSET);
for (All_Post all_Post : allDesc){
descArray.add(all_Post);
}
if (adapter == null) {
adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
listView.setAdapter(adapter);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
listView.setAdapter(adapter);
listView.invalidateViews();
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int item_position = sharedPreferences.getInt("item_position", 0);
listView.setSelection(item_position);
}
Pegination();
這裏後,我的分頁方法
public void Pegination() {
final int activityCount = sharedPreferences.getInt("ActivityListCount", 0);
Log.e("", "activityCount in Pegination method =" + activityCount);
listView.addFooterView(footer);
final Button btnLoadMore = new Button(this);
btnLoadMore.setText("Load More ....");
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
private int total;
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
}
@Override
public void onScroll(AbsListView absListView, int firstItem, int visibleItemCount, final int totalItems) {
total = firstItem + visibleItemCount;
Log.e("", "total =" + total);
if (totalItems > 0 && total > 0 && total == totalItems) {
if (preLast != total) {
if (pageCount < LocalPages) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
pageCount++;
List<All_Post> allDesc = dbhelper.getAllDescriptions(OFFSET_SCROLL);
for (All_Post all_Post : allDesc) {
descArray.add(all_Post);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setSelection(totalItems);
}
}
}, 2500);
}
preLast = total;
Log.e("", "11111111111 preLast=" + preLast);
}
preLast = total;
Log.e("", "222222222222222 preLast=" + preLast);
if (pageCount == LocalPages)
{
if (preLast == total)
{
if (activityCount == 200)
{
Log.e(" 11111 In btnLoadMore ", " ActivityListCount =" + activityCount);
Log.e("", "Go to else conditions --- 1 !!!!!! pageCount=" + pageCount + " LocalPages=" + LocalPages);
pageCount = 0;
listView.removeFooterView(footer);
listView.addFooterView(btnLoadMore);
}
}
if (activityCount < 200) {
Log.e(" 222222 In btnLoadMore ", " ActivityListCount =" + activityCount);
Log.e("", "Go to else conditions --- 2 !!!!!! pageCount=" + pageCount + " LocalPages=" + LocalPages);
pageCount = 0;
listView.removeFooterView(btnLoadMore);
}
}
}
}
}
});
}
這裏是我的日誌目錄的錯誤信息。
04-25 15:48:18.780 28787-28787/com.example.tazeen.classnkk E/﹕ total =137
04-25 15:48:18.780 28787-28787/com.example.tazeen.classnkk E/﹕ 222222222222222 preLast=137
04-25 15:48:18.781 28787-28787/com.example.tazeen.classnkk E/11111 In btnLoadMore﹕ ActivityListCount =200
04-25 15:48:18.781 28787-28787/com.example.tazeen.classnkk E/﹕ Go to else conditions --- 1 !!!!!! pageCount=3 LocalPages=3
04-25 15:48:18.785 28787-28787/com.example.tazeen.classnkk D/AndroidRuntime﹕ Shutting down VM
04-25 15:48:18.788 28787-28787/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tazeen.classnkk, PID: 28787
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
at android.widget.ListView.dispatchDraw(ListView.java:3334)
at android.view.View.draw(View.java:15364)
at android.widget.AbsListView.draw(AbsListView.java:4434)
at android.view.View.updateDisplayListIfDirty(View.java:14242)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.draw(View.java:15364)
at android.view.View.updateDisplayListIfDirty(View.java:14242)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at android.view.View.updateDisplayListIfDirty(View.java:14234)
at android.view.View.getDisplayList(View.java:14272)
at android.view.View.draw(View.java:15057)
at android.view.ViewGroup.drawChild(ViewGroup.java:3528)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3322)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchDraw(PhoneWindow.java:2845)
at android.view.View.draw(View.java:15364)
at android.widget.FrameLayout.draw(FrameLayout.java:647)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2824)
at android.view.View.updateDisplayListIfDirty(View.java:14242)
at android.view.View.getDisplayList(View.java:14272)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:267)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:312)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2852)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2689)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2313)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6438)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:795)
at android.view.Choreographer.doCallbacks(Choreographer.java:598)
at android.view.Choreographer.doFrame(Choreographer.java:567)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:781)
at android.os.Handler.handleCallback(Handler.java:810)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
有人可以幫我解決這個問題。爲什麼要獲取InvallidIndexOutOfBoundException。
郵政編碼在哪裏從arrayList中獲取值。 –
'for'循環中的'list'變量是什麼?它來自無處 –