我有一个列表视图,可以从parse.com检索数据。在用户达到每15个项目后,我添加了一个onscroll侦听器来加载新的列表项。当用户滚动执行前15的结束时,从那时开始显示加载页脚,但是没有显示下一个列表项。不知道问题是什么? 请帮帮我
我的代码
public class InterActivity extends Activity implements OnItemClickListener, OnItemLongClickListener,
AbsListView.OnScrollListener
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
List<CodeList> codelist = null;
SharedPreference shrdPreference;
private int limit = 15;
private final int AUTOLOAD_THRESHOLD = 4;
private final int MAXIMUM_ITEMS = 52;
private View mFooterView;
private Handler mHandler;
private boolean mIsLoading = false;
private boolean mMoreDataAvailable = true;
private boolean mWasLoading = false;
View footerView;
private Runnable mAddItemsRunnable = new Runnable() {
@Override public void run() {
new Loadmore().execute();
mIsLoading = false;
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.inter_layout);
shrdPreference = new SharedPreference();
footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.loadmore, null, false);
//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);
mHandler = new Handler();
mFooterView = LayoutInflater.from(InterActivity.this).inflate(R.layout.loadmore, null);
listview.addFooterView(mFooterView, null, false);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setOnItemClickListener(InterActivity.this);
listview.setOnItemLongClickListener(InterActivity.this);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(!mIsLoading && mMoreDataAvailable) {
if (totalItemCount >= MAXIMUM_ITEMS) {
mMoreDataAvailable = false;
listview.removeFooterView(mFooterView);
} else if (totalItemCount - AUTOLOAD_THRESHOLD <= firstVisibleItem + visibleItemCount) {
mIsLoading = true;
mHandler.postDelayed(mAddItemsRunnable, 1000);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Ignore
}
@Override
public void onStart() {
super.onStart(); if (mWasLoading) {
mWasLoading = false; mIsLoading = true;
mHandler.postDelayed(mAddItemsRunnable, 1000);
}
}
@Override
public void onStop() {
super.onStop();
mHandler.removeCallbacks(mAddItemsRunnable);
mWasLoading = mIsLoading;
mIsLoading = false;
}
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@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 += 15);
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) {
int position = listview.getLastVisiblePosition();
adapter = new FinalAdapter(InterActivity.this,
codelist);
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(adapter);
animationAdapter.setAbsListView(listview);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setSelectionFromTop(position, 0);
}
}