1

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

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

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

谁能建议我如何解决这个问题?

我已经在下面发布了针对这个问题的更新的相关代码:

下面是我的 Runnables 的代码,它负责从 JSONArray 获取数据并在活动开始时更新列表视图:

// 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 获取数据并在用户向下滚动时更新列表视图:

// 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)

实例化可运行的代码:

    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()

谢谢你!!

4

1 回答 1

0

您需要进行测试以fori > k.

于 2012-09-06T18:47:12.273 回答