I would like my app to start with a ProgressDialog while it connects to an online resource, grabs the data, parses it, and then fills a spinner with it. DirectoryTransaction.doDepts() in the code below is what connects and parses the data into the String[][] DeptResults. I’ve tried other questions and answers to no success, and would really like to get this to work. As it is now, the application appears to freeze for a few seconds while it does all this work. I’d much rather have it show the dialog instead.
Here’s the code as I have it now:
package com.test.directory;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Directory extends Activity {
public static boolean test = false;
public static int Start = 1;
public static int Page = 1;
public static int ResultsCount = 0;
public static int sdk = new Integer(Build.VERSION.SDK).intValue();
public static String[] Fields = new String[6];
public static String[][] DeptResults;
public static String[][] ReturnResults;
public static String[] ResultsDetails = new String[11];
public EditText fname = (EditText) findViewById(R.id.fname);
public EditText lname = (EditText) findViewById(R.id.lname);
public EditText aim = (EditText) findViewById(R.id.aim);
public EditText phone = (EditText) findViewById(R.id.phone);
public Spinner depts = (Spinner) findViewById(R.id.dept);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchscreen);
DirectoryTransaction.doDepts();
//Sets up and fills the department spinner
final ArrayAdapter<CharSequence> deptsAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
depts.setAdapter(deptsAdapter);
for(int i = 0 ; i < DeptResults.length ; i++){
deptsAdapter.add(DeptResults[i][0]);
}
//Search button
findViewById(R.id.search).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Start = 1;
Page = 1;
//Put all the info from the fields into a string array
Fields[0] = fname.getText().toString();
Fields[1] = lname.getText().toString();
Fields[2] = aim.getText().toString();
Fields[3] = phone.getText().toString();
Fields[4] = DeptResults[(int)depts.getSelectedItemId()][1];
Fields[5] = Integer.toString(Start);
DirectoryTransaction.doSearch(Directory.Start, Fields[0], Fields[1], Fields[2], Fields[4], Fields[3]);
//Shows error if more than 300 results
if(ResultsCount > 300){
Toast.makeText(Directory.this, "Too many results found, please narrow your search.", Toast.LENGTH_LONG).show();
}else{
//Load a new Intent and start the results activity
Intent i = new Intent(v.getContext(), DirectoryResults.class);
startActivity(i);
}
}
});
//Clear Button
findViewById(R.id.clear).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Clears all the fields, sets the Spinner to 0.
fname.setText("");
lname.setText("");
aim.setText("");
phone.setText("");
depts.setSelection(0);
}
});
}
}
Basically, this is what I want to do while a ProgressDialog shows.
DirectoryTransaction.doDepts();
//Sets up and fills the department spinner
final ArrayAdapter<CharSequence> deptsAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
depts.setAdapter(deptsAdapter);
for(int i = 0 ; i < DeptResults.length ; i++){
deptsAdapter.add(DeptResults[i][0]);
}
Any ideas / examples that can show me what I need? I’ve looked at quite a few and haven’t been able to figure it out.
Thanks, I got it working. I’m not sure if I need it to be so I’ll play around with it. Here’s the code:
package com.test.directory;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Directory extends Activity {
public static boolean test = false;
public static int Start = 1;
public static int Page = 1;
public static int ResultsCount = 0;
public static int sdk = new Integer(Build.VERSION.SDK).intValue();
public static String[] Fields = new String[6];
public static String[][] DeptResults;
public static String[][] ReturnResults;
public static String[] ResultsDetails = new String[11];
public ArrayAdapter<CharSequence> deptsAdapter;
ProgressDialog dialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchscreen);
new getAllData().execute(this);
final EditText fname = (EditText) findViewById(R.id.fname);
final EditText lname = (EditText) findViewById(R.id.lname);
final EditText aim = (EditText) findViewById(R.id.aim);
final EditText phone = (EditText) findViewById(R.id.phone);
final Spinner depts = (Spinner) findViewById(R.id.dept);
//Sets up and fills the department spinner
deptsAdapter = new ArrayAdapter<CharSequence>(Directory.this, android.R.layout.simple_spinner_item);
deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
depts.setAdapter(deptsAdapter);
//Search button
findViewById(R.id.search).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Start = 1;
Page = 1;
//Put all the info from the fields into a string array
Fields[0] = fname.getText().toString();
Fields[1] = lname.getText().toString();
Fields[2] = aim.getText().toString();
Fields[3] = phone.getText().toString();
Fields[4] = DeptResults[(int)depts.getSelectedItemId()][1];
Fields[5] = Integer.toString(Start);
DirectoryTransaction.doSearch(Directory.Start, Fields[0], Fields[1], Fields[2], Fields[4], Fields[3]);
//Shows error if more than 300 results
if(ResultsCount > 300){
Toast.makeText(Directory.this, "Too many results found, please narrow your search.", Toast.LENGTH_LONG).show();
}else{
//Load a new Intent and start the results activity
Intent i = new Intent(v.getContext(), DirectoryResults.class);
startActivity(i);
}
}
});
//Clear Button
findViewById(R.id.clear).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Clears all the fields, sets the Spinner to 0.
fname.setText("");
lname.setText("");
aim.setText("");
phone.setText("");
depts.setSelection(0);
}
});
}
private class getAllData extends AsyncTask<Context, Void, Cursor> {
protected void onPreExecute () {
dialog = ProgressDialog.show(Directory.this, "",
"Loading. Please wait...", true);
}
@Override
protected Cursor doInBackground(Context... params) {
DirectoryTransaction.doDepts();
return null;
}
protected void onPostExecute(Cursor c) {
for(int i = 0 ; i < DeptResults.length ; i++){
deptsAdapter.add(DeptResults[i][0]);
}
//update the UI or do soemthing
dialog.dismiss();
}
}
}
Here is how I do it:
I put
ProgressDialog dialog;at the top of my class file.In my onCreate() method I have this:
Since my task takes a bit of time I make it run in a background thread (and if your app is ‘freezing’ I would suggest doing the same). The code for the background thread looks like:
More info on ASync here: http://developer.android.com/reference/android/os/AsyncTask.html
If you were going to use Async, your onPreExecute would display the dialog, the doInBackground() would get whatever you need to get from the web, and then onPostExecute() would stick everything into your spinner.
If you don’t want to do that then you can still do the ProgressDialog.show(…) and dialog.dimiss() on finish