如何停止onScrollListener激活的Runnable线程?-Android

时间:2012-09-06 18:09:51

标签: android for-loop android-listview runnable arrays

我已经编辑了我的问题。我编辑我的问题的原因是,无论我做什么线程都是从数据库中获取所有项目。如果我的数据集很大,那么几行数据就不会出现问题。如果应用程序需要很长时间来检索所有数据并显示它,它可能会强制关闭我的应用程序。

现在,当我的活动开始时,我只检索了5个项目并将其显示在listview上。我通过使用“loadMoreItems1”线程完成此操作。 当用户向下滚动时,它会从数据库中检索另外5个项目并将其添加到listview。我通过线程“loadMoreItems”完成此任务

通过这样做我消除了应用程序强行关闭的可能性。但我遇到了另一个问题,并且它在列表中最初添加5项(使用listMoreItems1线程)后向下滚动时没有任何事情发生,接下来的5项不是当我向上滚动向下滚动后,我得到“inder out of bound”错误/警告。

有人可以建议我如何解决这个问题吗?

我已将更新的相关代码发布到以下问题:

以下是我的Runnables的代码,它负责从活动开始时从JSONArray获取数据并更新listview:

// Runnable to load the items
private Runnable loadMoreListItems1 = new Runnable() {
    public void run() {
        // Set flag so we cant load new items 2 at the same time
        loadingMore = true;
        // Reset the array that holds the new items
        int lastItemDisplayedIndex = myquestionsResults.size();
        HashMap<String, String> hm = new HashMap<String, String>();
        db = new DatabaseHandler(getApplicationContext());
        db.getReadableDatabase();
        hm = db.getUserDetails();
        db.close();
        String un = hm.get("username");
        uf = new UserFunctions();
        JSONArray json = uf.getAllQuestions(un);
        for (int i = lastItemDisplayedIndex; i < lastItemDisplayedIndex + 5; i++) {
            try {
                ja = json.getJSONArray(i);
                id = ja.getString(0);
                userName = ja.getString(1);
                question = ja.getString(2);
                tag1 = ja.getString(3);
                tag2 = ja.getString(4);
                tag3 = ja.getString(5);
                posted_on = ja.getString(6);
                DataHolder qih = new DataHolder();
                qih.setId(id);
                qih.setUserName(userName);
                qih.setQuestion(question);
                qih.setTag1(tag1);
                qih.setTag2(tag2);
                qih.setTag3(tag3);
                qih.setQuestionPostedOn(posted_on);
                myquestionsResults.add(qih);

            } catch (Exception e) {
                e.printStackTrace();

            }
        }
        // Done! now continue on the UI thread
        runOnUiThread(returnRes);
    }
};

下面是我的Runnables的代码,它负责从用户向下滚动时从JSONArray获取数据并更新listview:

// Runnable to load the items
private Runnable loadMoreListItems = new Runnable() {
    public void run() {
        // Set flag so we cant load new items 2 at the same time
        loadingMore = true;
        // Reset the array that holds the new items
        int lastItemDisplayedIndex = myquestionsResults.size();
        System.out.println("bhalubhai");
        System.out.println(lastItemDisplayedIndex);
        qid = myquestionsResults.get(lastItemDisplayedIndex-1).getId();
        System.out.println(myquestionsResults.get(lastItemDisplayedIndex-1).getId());
        HashMap<String, String> hm = new HashMap<String, String>();
        db = new DatabaseHandler(getApplicationContext());
        db.getReadableDatabase();
        hm = db.getUserDetails();
        db.close();
        String un = hm.get("username");
        uf = new UserFunctions();
        JSONArray json = uf.getAllQuestions2(un, qid);
        //int k = json.length();
        for (int i = lastItemDisplayedIndex; i < lastItemDisplayedIndex+5; i++) {
            try {
                ja = json.getJSONArray(i);
                id = ja.getString(0);
                userName = ja.getString(1);
                question = ja.getString(2);
                tag1 = ja.getString(3);
                tag2 = ja.getString(4);
                tag3 = ja.getString(5);
                posted_on = ja.getString(6);
                DataHolder qih = new DataHolder();
                qih.setId(id);
                qih.setUserName(userName);
                qih.setQuestion(question);
                qih.setTag1(tag1);
                qih.setTag2(tag2);
                qih.setTag3(tag3);
                qih.setQuestionPostedOn(posted_on);
                myquestionsResults.add(qih);

            } catch (Exception e) {
                e.printStackTrace();

            }
        }
        // Done! now continue on the UI thread
        runOnUiThread(returnRes);
    }
};

以下是runnable“returnRes”的代码,loadMoreItems和loadMoreItems1使用它来更新ListView:

private Runnable returnRes = new Runnable() {
    public void run() {
        // Tell to the adapter that changes have been made, this will cause
        // the list to refresh
            adapter.notifyDataSetChanged();
            // Done loading more.
            loadingMore = false;
    }
};

下面是我在向下滚动后向上滚动时收到的LogCat消息:

09-09 02:22:38.348: W/System.err(276): org.json.JSONException: Index 5 out of range [0..5)
09-09 02:22:38.368: W/System.err(276):  at org.json.JSONArray.get(JSONArray.java:263)
09-09 02:22:38.368: W/System.err(276):  at org.json.JSONArray.getJSONArray(JSONArray.java:455)
09-09 02:22:38.368: W/System.err(276):  at com.dimecore.qq.Questions$1.run(Questions.java:190)
09-09 02:22:38.368: W/System.err(276):  at java.lang.Thread.run(Thread.java:1096)
09-09 02:22:38.368: W/System.err(276): org.json.JSONException: Index 6 out of range [0..5)
09-09 02:22:38.368: W/System.err(276):  at org.json.JSONArray.get(JSONArray.java:263)
09-09 02:22:38.368: W/System.err(276):  at org.json.JSONArray.getJSONArray(JSONArray.java:455)
09-09 02:22:38.368: W/System.err(276):  at com.dimecore.qq.Questions$1.run(Questions.java:190)
09-09 02:22:38.368: W/System.err(276):  at java.lang.Thread.run(Thread.java:1096)
09-09 02:22:38.368: W/System.err(276): org.json.JSONException: Index 7 out of range [0..5)
09-09 02:22:38.368: W/System.err(276):  at org.json.JSONArray.get(JSONArray.java:263)
09-09 02:22:38.378: W/System.err(276):  at org.json.JSONArray.getJSONArray(JSONArray.java:455)
09-09 02:22:38.378: W/System.err(276):  at com.dimecore.qq.Questions$1.run(Questions.java:190)
09-09 02:22:38.378: W/System.err(276):  at java.lang.Thread.run(Thread.java:1096)
09-09 02:22:38.378: W/System.err(276): org.json.JSONException: Index 8 out of range [0..5)
09-09 02:22:38.378: W/System.err(276):  at org.json.JSONArray.get(JSONArray.java:263)
09-09 02:22:38.378: W/System.err(276):  at org.json.JSONArray.getJSONArray(JSONArray.java:455)
09-09 02:22:38.378: W/System.err(276):  at com.dimecore.qq.Questions$1.run(Questions.java:190)
09-09 02:22:38.378: W/System.err(276):  at java.lang.Thread.run(Thread.java:1096)
09-09 02:22:38.378: W/System.err(276): org.json.JSONException: Index 9 out of range [0..5)
09-09 02:22:38.378: W/System.err(276):  at org.json.JSONArray.get(JSONArray.java:263)
09-09 02:22:38.378: W/System.err(276):  at org.json.JSONArray.getJSONArray(JSONArray.java:455)
09-09 02:22:38.388: W/System.err(276):  at com.dimecore.qq.Questions$1.run(Questions.java:190)
09-09 02:22:38.388: W/System.err(276):  at java.lang.Thread.run(Thread.java:1096)

实例化runnable的代码:

    lv.setOnScrollListener(new OnScrollListener() {


        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == SCROLL_STATE_IDLE) {
                loadingMore = false;
        }else{
                loadingMore = true;
            }
        }



        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            // what is the bottom item that is visible
            int lastInScreen = firstVisibleItem + visibleItemCount;

            // is the bottom item visible & not loading more already ? Load
            // more !
            if ((lastInScreen == totalItemCount) && !(loadingMore)) {
                Thread thread = new Thread(null, loadMoreListItems);
                thread.start();
            }
        }
    });

    // Load the first 5 items
    Thread thread = new Thread(null, loadMoreListItems1);
    thread.start()

谢谢!!

1 个答案:

答案 0 :(得分:0)

您需要在for时进行测试以退出i > k循环。