リストの一番下に Web サービスからの動的データを追加したいと考えています。リストを下にスクロールして古いメッセージを表示し、次のページをロードすると、リストが更新されます..私のコード。誰かが助けてくれたら...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.conversations_activity);
mListView.setOnScrollListener(new OnScrollListener() {
int maxLoads = 30;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,visibleItemCount, int totalItemCount) {
if ((totalItemCount - 1 > maxLoads)&& ((totalItemCount - 1) % maxLoads == 0)) {
System.gc();
}
if (((firstVisibleItem + visibleItemCount) == totalItemCount)
&& !isDataLoading && isScrollingEnable) {
new LoadNextPage(ConversationsActivity.this).execute();
}
}
});
new ConversationListTask(ConversationsActivity.this).execute();
}
}
private class ConversationListTask extends BaseAsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public ConversationListTask(Activity activity) {
super(activity);
}
@Override
public void runDoInBackground() throws RuntimeException, Exception {
isDataLoading = true;
isScrollingEnable = false;
mPageNumber = 0;
try {
listMessage = APIProcessor.listMessage(ConversationsActivity.this, "1",
mPageNumber);
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void runOnPostExecute() {
isDataLoading = false;
isScrollingEnable = true;
convoListAdapter = new ConversationListAdapter(
ConversationsActivity.this, listMessage, urlThumbImageLoader);
mListViewConversation.setAdapter(convoListAdapter);
mListViewConversation.setSelection(mListViewConversation.getAdapter()
.getCount() - 1);
}
}
class LoadNextPage extends BaseAsyncTask {
public LoadNextPage(Activity activity) {
super(activity);
setDefaultProgressDialogStatus(false);
findViewById(R.id.list_loading_view).setVisibility(View.VISIBLE);
}
@Override
public void runDoInBackground() throws RuntimeException, Exception {
isDataLoading = true;
tempList = APIProcessor.listMessage(ConversationsActivity.this, "1",
mPageNumber + 1);
}
@Override
public void runOnPostExecute() {
new LoadNextPage(ConversationsActivity.this).execute();
findViewById(R.id.list_loading_view).setVisibility(View.GONE);
isDataLoading = false;
if (tempList == null || tempList.isEmpty() || listMessage == null
|| listMessage.isEmpty()) {
}
else {
mPageNumber++;
for (int i = 0; i < tempList.size(); i++) {
listMessage.add(tempList.get(i));
}
convoListAdapter.notifyDataSetChanged();
}
}
}