How can I prevent AsyncTask from being run repeatedly in the onScroll event?

advertisements

I've got an AsyncTask which refreshes my listView in onPostExecute event and retrieves some data in doInBackground event. The problem is that my AsyncTask is being executed multiple times within onScroll event. How can I prevent it from doing that? By the way, i'm trying to create an Inifinite Scroll in my app. Maybe i'm doing something wrong? Here's my onScroll event:

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    previousTotal = totalItemCount;
    try {
        itemNews = listAdapter.getData();
    }catch (Exception e){
        System.out.println("itemNews is empty");
    }
    if(totalItemCount > 2) {
        artId = itemNews.get(firstVisibleItem).articleId;
    }
    if(firstVisibleItem > ((previousTotal)/2))
    {
        UpdateListAsyncTask updateListAsyncTask = new UpdateListAsyncTask(artId, mActivity, listAdapter);
        updateListAsyncTask.execute();
    }

And here's my AsyncTask:

public class UpdateListAsyncTask extends AsyncTask<Void, Void, Void> {
    public int currentArticleId;
    public Activity mActivity;
    RemoteData remoteData;
    public ListAdapterRegular listAdapter;
    UpdateListAsyncTask(int currentArticleId, Activity activity, ListAdapterRegular listAdapterRegular)
    {
        this.mActivity = activity;
        this.currentArticleId = currentArticleId;
        this.listAdapter = listAdapterRegular;
    }
    @Override
    protected void onPreExecute()
    {
        remoteData = new RemoteData(mActivity, "News");
    }
    @Override
    protected Void doInBackground(Void... params) {
        try{
            remoteData.writeServerData("&from=" + currentArticleId);
            System.out.println("DoInBackground writeServerData");
        }catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("UpdateListAsyncTask: doInBackground exception!");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        List<News> dataList = Select.from(News.class).orderBy("date DESC").list();
        System.out.println("onPostExecuteInitiated");
        listAdapter.clear();
        listAdapter.addAll(dataList);
        listAdapter.notifyDataSetChanged();
    }
}

Any help and ideas are so much appreciated! Update: I had to redo my code, but it worth. I've done it according to link: https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews


I would use something like this

// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    previousTotal = totalItemCount;
    try {
        itemNews = listAdapter.getData();
    }catch (Exception e){
        System.out.println("itemNews is empty");
    }

    // 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.previousTotalItemCount = totalItemCount;
        if (totalItemCount == 0) { this.loading = true; }
    }

    if(totalItemCount > 2) {
        artId = itemNews.get(firstVisibleItem).articleId;
    }

    // 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;
    }

    if(!loading && firstVisibleItem > ((previousTotal)/2))
    {
        loading = true;
        UpdateListAsyncTask updateListAsyncTask = new UpdateListAsyncTask(artId, mActivity, listAdapter);
        updateListAsyncTask.execute();
    }
}

PS When I implemented the infinite scroll I used this as an example https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews