Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ProgressDialog Progress Bar doing things in the right order
    primarykey
    data
    text
    <p>I just about got this, but I have a small problem in the order of things going off. Specifically, in my thread() I am setting up an array that is used by a Spinner. Problem is the Spinner is all set and done basically before my thread() is finished, so it sets itself up with a null array.</p> <p>How do I associate the spinners ArrayAdapter with an array that is being loaded by another thread?</p> <p>I've cut the code down to what I think is necessary to understand the problem, but just let me know if more is needed. The problem occurs whether or not refreshData() is called.</p> <p>Along the same lines, sometimes I want to call loadData() from the menu. Directly following loadData() if I try to fire a toast on the next line this causes a forceclose, which is also because of how I'm implementing ProgressDialog.</p> <p>THANK YOU FOR LOOKING</p> <pre><code>public class CMSHome extends Activity { private static List&lt;String&gt; pmList = new ArrayList&lt;String&gt;(); // Instantiate helpers PMListHelper plh = new PMListHelper(); ProjectObjectHelper poc = new ProjectObjectHelper(); // These objects hold lists and methods for dealing with them private Employees employees; private Projects projects; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Loads data from filesystem, or webservice if necessary loadData(); // Capture spinner and associate pmList with it through ArrayAdapter spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;( this, android.R.layout.simple_spinner_item, pmList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); //---the button is wired to an event handler--- Button btn1 = (Button)findViewById(R.id.btnGetProjects); btn1.setOnClickListener(btnListAllProjectsListener); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); } private void loadData() { final ProgressDialog pd = ProgressDialog.show(this, "Please wait", "Loading Data...", true, false); new Thread(new Runnable(){ public void run(){ employees = plh.deserializeEmployeeData(); projects = poc.deserializeProjectData(); // Check to see if data actually loaded, if not then refresh if ((employees == null) || (projects == null)) { refreshData(); } // Load up pmList for spinner control pmList = employees.getPMList(); pd.dismiss(); } }).start(); } private void refreshData() { // Refresh data for Projects projects = poc.refreshData(); poc.saveProjectData(mCtx, projects); // Refresh data for PMList employees = plh.refreshData(); plh.savePMData(mCtx, employees); } } </code></pre> <p>&lt;---- EDIT -----> I tried changing onCreate() to the following after Jims suggestion. Not sure if I did this right, still doesn't work:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mCtx = this; // Loads data from filesystem, or webservice if necessary // Would like to extend this to update if files are over x days old final ProgressDialog pd = ProgressDialog.show(this, "Please wait", "Loading Data...", true, false); new Thread(new Runnable(){ public void run(){ employees = plh.deserializeEmployeeData(); projects = poc.deserializeProjectData(); // Check to see if data actually loaded, if not then refresh if ((employees == null) || (projects == null)) { refreshData(); } pd.dismiss(); runOnUiThread(new Runnable() { public void run(){ // Load up pmList for spinner control pmList = employees.getPMList(); } }); } }).start(); spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;( this, android.R.layout.simple_spinner_item, pmList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); //---the button is wired to an event handler--- Button btn1 = (Button)findViewById(R.id.btnGetProjects); btn1.setOnClickListener(btnListAllProjectsListener); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); } </code></pre> <hr> <p>Wow, this took forever for me to find a solution, but Im overjoyed to have finally gotten this to work.</p> <p>Updating the Spinner with a background thread can be accomplished by utilizing a handler. The handler is called after the main work of the thread is completed.</p> <pre><code> mProgressDlg = ProgressDialog.show(this, "App_Name", "Loading data...", true, false); new Thread(new Runnable(){ public void run() { /*Load Data, set pmList in my case*/ mProgressDlg.dismiss(); hRefresh.sendEmptyMessage(REFRESH); } }).start(); Handler hRefresh = new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case REFRESH: spinner = (Spinner) findViewById(R.id.spinner); final ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;( mCtx, android.R.layout.simple_spinner_item, pmList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); break; } } }; </code></pre> <p>Credit to bhatt4982 and his answer on <a href="https://stackoverflow.com/questions/1458190/how-can-i-display-a-progress-at-start-up-application-in-android">this thread</a></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.
 

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