2012-09-06 100 views
1

我已經編輯我的question.The原因,我編輯我的問題是,無論我做什麼的線程從數據database.Retrieving幾行獲取的所有項目會沒有問題,但如果我的數據集大於它可能會強制關閉我的應用程序,如果應用程序需要很多時間來檢索所有數據並顯示它。如何停止onScrollListener啓動運行的線程?-Android

現在我的活動開始時,我只檢索5個項目並將其顯示在listview.I通過使用「loadMoreItems1」線程實現這一目標。 比用戶向下滾動時從數據庫中檢索5個項目並將其添加到listview.I通過線程「loadMoreItems」完成此任務

通過這樣做,我消除了應用程序獲取強制關閉的可能性。但是我跑進另一個問題,它是在列表中添加5個項目(使用listMoreItems1線程)開始後,當我向下滾動,什麼都不會發生接下來的5items不被added.Also當我向下滾動後,向上滾動,我得到「因德爾越界「錯誤/警告。

任何人都可以請建議我如何解決這個問題?

我已經發布了更新相關的代碼下面這個問題:

下面是我的Runnable的代碼負責獲取來自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); 
    } 
}; 

下面是我的Runnable的代碼負責從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); 
    } 
}; 

倍兒流是其被兩個loadMoreItems和loadMoreItems1更新ListView的可運行「returnRes」代碼:

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

謝謝!

回答

0

你需要把在測試退出for循環時i > k

+0

感謝response.i @techiServices試圖做到這一點,但似乎是線程仍在運行,並再次獲取整個JSONArray陣列和again.Now我不這樣做,雖然之後獲得JSONArray出界異常。 – Viking

+0

@維京。你如何開始執行'Runnable'?你能發佈代碼嗎? – techiServices

+0

@ techiServices謝謝你的回覆。我更新了我的帖子並添加了實例化runnable的代碼。 – Viking