我有一个列表视图,其中包含onscroll侦听器以加载更多项目。当加载更多项目时,将显示listvview页脚。当我运行应用程序时,列表的第一项被加载,当滚动到最后加载更多项目时,应用程序崩溃。
我的代码
public class InterActivity extends Activity
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
List<CodeList> codelist = null;
SharedPreference shrdPreference;
private int limit = 15;
View footerView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.inter_layout);
shrdPreference = new SharedPreference();
//Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading");
// Set progressdialog message
mProgressDialog.setMessage("Please wait loading ...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit);
ob = query.find();
for (ParseObject inter : ob) {
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.inter_layoutListView);
// Pass the results into ListViewAdapter.java
adapter = new FinalAdapter(InterActivity.this,
codelist);
// AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(adapter);
// animationAdapter.setAbsListView(listview);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
//listview.setOnItemClickListener(InterActivity.this);
//listview.setOnItemLongClickListener(InterActivity.this);
// Close the progressdialog
mProgressDialog.dismiss();
//设置onscrll侦听器
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) { // TODO Auto-generated method stub
int threshold = 1;
int count = listview.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (listview.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
new Loadmore().execute();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
}
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.loadmore, null, false);
listview.addFooterView(footerView);
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist.clear();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit += 15);
ob = query.find();
for (ParseObject inter : ob) {
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int position = listview.getLastVisiblePosition();
adapter.notifyDataSetChanged();
listview.removeFooterView(footerView);
listview.setSelectionFromTop(position, 0);
super.onPostExecute(result);
}
}
我的logcat
01-06 12:44:53.279 12753 12753 E AndroidRuntime致命例外:主要 01-06 12:44:53.279 12753 12753 E AndroidRuntime进程:com.enlightenme.pac,PID:12753 01-06 12:44:53.279 12753 12753 E AndroidRuntime java.lang.IndexOutOfBoundsException:索引10无效,大小为1 01-06 12:44:53.279 12753 12753 E AndroidRuntime at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at java.util.ArrayList.get(ArrayList.java:308) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.AbsListView.obtainView(AbsListView.java:2347) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.ListView.makeAndAddView(ListView.java:1864) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.ListView.fillDown(ListView.java:698) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.ListView.fillSpecific(ListView.java:1356) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.ListView.layoutChildren(ListView.java:1651) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.AbsListView.onLayout(AbsListView.java:2151) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.View.layout(View.java:15671) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewGroup.layout(ViewGroup.java:5038) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.View.layout(View.java:15671) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewGroup.layout(ViewGroup.java:5038) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 01-06 12:44:53.279 12753 12753 E androidRuntime at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.View.layout(View.java:15671) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewGroup.layout(ViewGroup.java:5038) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.View.layout(View.java:15671) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewGroup.layout(ViewGroup.java:5038) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 01-06 12:44:53.279 12753 12753 E androidRuntime at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.View.layout(View.java:15671) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewGroup.layout(ViewGroup.java:5038) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5885) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.Choreographer.doCallbacks(Choreographer.java:580) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.Choreographer.doFrame(Choreographer.java:550) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.os.Handler.handleCallback(Handler.java:739) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:95) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.os.Looper.loop(Looper.java:135) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5254) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at java.lang.reflect.Method.invoke(Native Method) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:372) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 01-06 12:44:53.279 12753 12753 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
你也可以这样使用
list.setOnScrollListener(new EndlessScrollListener() {
@Override
public boolean onLoadMore(int page, int totalItemsCount) {
// your load more code
return true;
}
});
创建EndlessScrollListener.java
import android.widget.AbsListView;
public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
// If it's still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
// If it isn't currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
loading = onLoadMore(currentPage + 1, totalItemCount);
}
}
// Defines the process for actually loading more data based on page
// Returns true if more data is being loaded; returns false if there is no more data to load.
public abstract boolean onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}