Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ListView and multiple threads that updates the listview
    primarykey
    data
    text
    <p>I have an activity with one listview showing items. This activity has three buttons and a menu with multiple other buttons.</p> <p>When i push one button, a new thread is launched, gets datas from a web service, fills the listview. Then the listview is shown. </p> <p>The only problem is that each time i push one of the buttons, i launch a thread. Depending on how long it takes for the datas to be retrieved, it happens sometimes that the listview is not filled with the good datas (it the user doesn't wait for one thread to be finished and pushes another button)</p> <p>First i tried to cancel the thread but it does not work (async task or with thread.interrupt)</p> <p>How can i stop previous threads so that the listview will be filled with the last datas ?</p> <p>Thanks for the help :)</p> <pre><code>// Items to be displayed in the listview private ArrayList&lt;HashMap&lt;String, Object&gt;&gt; oItems = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;(); // Launch the thread when toggle is checked button.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { LoadListView(); } } }); oMenu.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { LoadListView(); return true; } }); private void LoadListView() { new Thread(ListViewRunnable).start(); } private Runnable ListViewRunnable = new Runnable() { public void run() { while (GetItems() &lt; 1) { } MainHandler.post(new Runnable() { public void run() { } }); } private int GetItems() { try { HashMap&lt;String, Object&gt; oItem; //Get items from web services List&lt;ItemFromService&gt; oItemsFromService = GetItemsFromWebService(); for (oItemFromService : oItemsFromService) { oItem = new HashMap&lt;String, Object&gt;(); oItem.put("ID", oItemFromService.get_iID()); oItem.put("Label", oItemFromService.get_sLabel()); oItems.add(oItem); } MainHandler.sendEmptyMessage(GOT_ITEMS); } catch (Exception e) { } return 1; } } private Handler MainHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == GOT_ITEMS) { adapter.notifyDataSetChanged(); } } }; </code></pre>
    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