Reputation: 3530
I'm trying to show Progress Dialog while filling layout:
EDIT (here is the code):
public class CalendarMonth extends Activity {
private ProgressDialog mDialog;
private int mMonth = 0;
private int mYear = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_m);
filterData();
}
private void filterData() {
mDialog = ProgressDialog.show(this, "Loading calendar",
"Please wait...", true, true);
mDialog.setCancelable(false);
mDialog.show();
new GenerateMonth().execute();
}
class GenerateMonth extends AsyncTask {
protected Object doInBackground(Object... arg0) {
generateMonth();
return true;
}
protected void onPostExecute(Object result) {
mDialog.dismiss();
}
private void generateMonth() {
mCalendar = new CalendarInstance(mMonth, mYear);
mMonth = this.mCalendar.getSelectedMonth();
mYear = this.mCalendar.getSelectedYear();
TextView monthName = (TextView) findViewById(R.id.monthName);
monthName.setText(mCalendar.getMonthAndYearAsString());
Button monthPrevious = (Button) findViewById(R.id.monthPrevious);
monthPrevious.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int month = mMonth - 1;
int year = mYear;
if (mMonth == 1) {
month = 12;
year = mYear - 1;
}
mMonth = month;
mYear = year;
filterData();
}
});
Button monthNext = (Button) findViewById(R.id.monthNext);
monthNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int month = mMonth + 1;
int year = mYear;
if (mMonth == 12) {
month = 1;
year = mYear + 1;
}
mMonth = month;
mYear = year;
filterData();
}
});
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(7);
gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridview.setBackgroundColor(Color.BLACK);
gridview.setAdapter(new CalendarMonthAdapter(this, mMonth, mYear));
}
@Override
protected void onResume() {
super.onResume();
filterData();
}
}
Upvotes: 0
Views: 2679
Reputation: 4040
it might be easier to use an AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html
so you would have something like this
public class Temp extends Activity {
private Context mContext;
private ProgressDialog mDialog
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
mDialog = ProgressDialog.show(this, "Filter", "blah", true, true);
mDialog.show();
new FillDataTask.execute();
}
class FillDataTask extends AsyncTask {
@Override
protected Object doInBackground(Object... params) {
try {
filterData();
} catch (ParseException e) {
Log.e("FILTER", e.toString());
e.printStackTrace();
}
return true; // you can return whatever you want here
}
protected void onPostExecute(Object result) {
//update progressDialog here
mDialog.dismiss();
}
}
}
EDIT BELOW
do it like this
public class CalendarMonth extends Activity {
private ProgressDialog mDialog;
private int mMonth = 0;
private int mYear = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_m);
filterData();
}
private void filterData() {
mDialog = ProgressDialog.show(this, "Loading calendar",
"Please wait...", true, true);
mDialog.setCancelable(false);
mDialog.show();
new GenerateMonth().execute();
}
private void genMonth()
{
mCalendar = new CalendarInstance(mMonth, mYear);
mMonth = this.mCalendar.getSelectedMonth();
mYear = this.mCalendar.getSelectedYear();
}
private void layoutUI()
{
TextView monthName = (TextView) findViewById(R.id.monthName);
monthName.setText(mCalendar.getMonthAndYearAsString());
Button monthPrevious = (Button) findViewById(R.id.monthPrevious);
monthPrevious.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int month = mMonth - 1;
int year = mYear;
if (mMonth == 1) {
month = 12;
year = mYear - 1;
}
mMonth = month;
mYear = year;
filterData();
}
});
Button monthNext = (Button) findViewById(R.id.monthNext);
monthNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int month = mMonth + 1;
int year = mYear;
if (mMonth == 12) {
month = 1;
year = mYear + 1;
}
mMonth = month;
mYear = year;
filterData();
}
});
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(7);
gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridview.setBackgroundColor(Color.BLACK);
gridview.setAdapter(new CalendarMonthAdapter(this, mMonth, mYear));
}
}
}
class GenerateMonth extends AsyncTask {
protected Object doInBackground(Object... arg0) {
genMonth();
return true;
}
protected void onPostExecute(Object result) {
layoutUI();
mDialog.dismiss();
}
}
Upvotes: 1
Reputation: 2222
It seems that your problem is in generateMonth function, which is invoked from the doInBackground function. You should not access UI from doInBackground function or from any function called from doInBackground. This function is aimed to do tasks that require lot of time, like calculations or complex DB queries, but MUST NOT access UI. Your UI updates should be done in function onPostExecute. So, things like:
gridview.setNumColumns(7)
are not allowed in doInBackground (or generateMonth which is invoked from there).
Upvotes: 0
Reputation: 18348
The error must be in filterData, if you are updating the UI from filter data it will give that exception. I would recomend using async task, using doInBackground to build the data, and onPostExecute to update the UI.
Upvotes: 0