Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop Runnable thread activated by onScrollListener?-Android
    primarykey
    data
    text
    <p>I have edited my question.The reason why i edited my question is that no matter what i do thread was fetching all the items from the database.Retrieving few rows of data would not have been problem but if my dataset is large than it might force close my app if app takes much time to retrieve all the data and display it.</p> <p>Now when my activity starts i retrieve just 5 items and display it on the listview.I accomplish this by using "loadMoreItems1" thread. Than when the user scrolls down it retrieves 5 more items from the database and add it to the listview.I accomplish this task by thread "loadMoreItems"</p> <p>By doing this i eliminated the possibility of app getting force close.But i ran into another problem and that it that after adding 5 items(using listMoreItems1 thread) initially in the list when i scroll down nothing happens the next 5items are not being added.Also when i scroll up after scrolling down i get "inder out of bound" errors/warnings.</p> <p>Can anyone please suggest me how to tackle this problem?</p> <p>I have posted the updated relevant code to this problem below:</p> <p>Below is the code of my Runnables which is responsible for fetch data from JSONArray and updating listview when the activity starts:</p> <pre><code>// 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&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;(); 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 &lt; 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); } }; </code></pre> <p>Below is the code of my Runnables which is responsible for fetch data from JSONArray and updating listview when user scrolls down:</p> <pre><code>// 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&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;(); 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 &lt; 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); } }; </code></pre> <p>Below is the code of Runnable "returnRes" which is used by both loadMoreItems and loadMoreItems1 to update ListView:</p> <pre><code>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; } }; </code></pre> <p>Below is the LogCat messages that i get when i scroll up after scrolling down:</p> <pre><code>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) </code></pre> <p>Code thats instantiate runnable:</p> <pre><code> 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 &amp; not loading more already ? Load // more ! if ((lastInScreen == totalItemCount) &amp;&amp; !(loadingMore)) { Thread thread = new Thread(null, loadMoreListItems); thread.start(); } } }); // Load the first 5 items Thread thread = new Thread(null, loadMoreListItems1); thread.start() </code></pre> <p>Thank You!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload