Android (Java):在加载表视图数据时显示进度条



我有一个代码块来加载一个表视图。但是对于大数据,我的应用程序崩溃了。我想解决这个问题,也想显示一个进度条加载数据表视图。我尝试使用AsyncTask,但它显示Skipped 98 frames! The application may be doing too much work on its main thread

我的onCreate代码(更新):-

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheet);
myDb = new DBHelper(this);
loadIntentInputs();
setToolbar();
new MyWorker(this).execute();
}

我的AsyncTask代码(更新):-

public class MyWorker extends AsyncTask < String , Context , Void > {
private Context context ;
private ProgressDialog progressDialog;
public MyWorker (Context context) {
this.context = context ;
progressDialog = new ProgressDialog ( context ) ;
progressDialog.setCancelable ( false ) ;
progressDialog.setMessage ( "Retrieving data..." ) ;
progressDialog.setTitle ( "Please wait" ) ;
progressDialog.setIndeterminate ( true ) ;
}
@ Override
protected void onPreExecute ( ) {
progressDialog.show () ;
}
@ Override
protected Void doInBackground ( String ... params ) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showAttendanceTable();
}
});
return null ;
}
@ Override
protected void onPostExecute ( Void result ) {
if(progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss ( ) ;
}
}
}

showAttendanceTable方法代码(已更新):-

private void showAttendanceTable() {
TableLayout tableLayout = findViewById(R.id.table_layout);
int day_in_month = getDayInMonth(month_year);
int row_size = sid_array.length+1;
TableRow[] rows = new TableRow[row_size];
TextView[] rolls_tv = new TextView[row_size];
TextView[] names_tv = new TextView[row_size];
TextView[][] status_tv = new TextView[row_size][day_in_month+1];
for (int i = 0; i < row_size; i++) {
rolls_tv[i] = new TextView(this);
names_tv[i] = new TextView(this);
for (int j = 1; j <= day_in_month; j++) {
status_tv[i][j] = new TextView(this);
}
}
// for excel file
HSSFSheet hssfSheet = null;
try {
hssfSheet = hssfWorkbook.createSheet(month_year);
} catch (IllegalArgumentException e){
e.printStackTrace();
}
HSSFRow hssfRow;
// setting 1st row
rolls_tv[0].setText("Roll");
names_tv[0].setText("Name");
// setting excel file
hssfRow = hssfSheet.createRow(0);
hssfRow.createCell(0).setCellValue("Roll");
hssfRow.createCell(1).setCellValue("Name");
rolls_tv[0].setTextSize(1,22);
names_tv[0].setTextSize(1,22);
rolls_tv[0].setTypeface(rolls_tv[0].getTypeface(), Typeface.BOLD);
names_tv[0].setTypeface(names_tv[0].getTypeface(), Typeface.BOLD);
rolls_tv[0].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
names_tv[0].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
rolls_tv[0].setTextColor(Color.parseColor("#FF000000"));
names_tv[0].setTextColor(Color.parseColor("#FF000000"));
for (int i = 1; i <=day_in_month ; i++) {
String date = String.valueOf(i);
if(i<10) date = "0"+date;
status_tv[0][i].setText(date);
// setting excel file
hssfRow.createCell(i+1).setCellValue(date);
status_tv[0][i].setTextSize(1,22);
status_tv[0][i].setTypeface(status_tv[0][i].getTypeface(), Typeface.BOLD);
status_tv[0][i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
status_tv[0][i].setTextColor(Color.parseColor("#FF000000"));
}
// setting rows after 1st row
for (int i = 1; i < row_size ; i++) {
rolls_tv[i].setText(roll_array[i-1]);
names_tv[i].setText(name_array[i-1]);
// setting excel file
hssfRow = hssfSheet.createRow(i);
hssfRow.createCell(0).setCellValue(roll_array[i-1]);
hssfRow.createCell(1).setCellValue(name_array[i-1]);
rolls_tv[i].setTextSize(1,20);
names_tv[i].setTextSize(1,20);
rolls_tv[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
names_tv[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
rolls_tv[i].setTextColor(Color.parseColor("#FF000000"));
names_tv[i].setTextColor(Color.parseColor("#FF000000"));
int count=0;
for (int j = 1; j <=day_in_month ; j++) {
String day = String.valueOf(j);
String month = month_year.substring(0,3);
String year = month_year.substring(4,8);
String status = myDb.getStatus(sid_array[i-1],day+" "+month+" "+year);
if (status!=null && status.equals("A")) {
status_tv[i][j].setText(status);
status_tv[i][j].setBackgroundColor(Color.parseColor("#EF9A9A"));
// setting excel file
hssfRow.createCell(j+1).setCellValue(status);
} else if (status!=null && status.equals("P")) {
status_tv[i][j].setText(String.valueOf(++count));
status_tv[i][j].setBackgroundColor(Color.parseColor("#A5D6A7"));
// setting excel file
hssfRow.createCell(j+1).setCellValue(String.valueOf(count));
}
status_tv[i][j].setTextSize(1,20);
status_tv[i][j].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
status_tv[i][j].setTextColor(Color.parseColor("#FF000000"));
}
}
for (int i = 0; i < row_size; i++) {
rows[i] = new TableRow(this);
rolls_tv[i].setPadding(20,12,20,12);
names_tv[i].setPadding(20,12,20,12);
if (i==0) {
rows[i].setBackgroundColor(Color.parseColor("#FFD400"));
} else if(i%2==0) {
rows[i].setBackgroundColor(Color.parseColor("#EEEEEE"));
} else {
rows[i].setBackgroundColor(Color.parseColor("#E4E4E4"));
}
rows[i].addView(rolls_tv[i]);
rows[i].addView(names_tv[i]);
for (int j = 1; j <=day_in_month ; j++) {
status_tv[i][j].setPadding(20,12,20,12);
rows[i].addView(status_tv[i][j]);
}
tableLayout.addView(rows[i]);
}
tableLayout.setShowDividers(TableLayout.SHOW_DIVIDER_MIDDLE);
}

问题是你在你的doInBackground方法上运行新线程,你已经把你的showAttendanceTable()代码放在runOnUiThread()里面,这会导致太多的工作消耗主线程。只需复制粘贴下面的代码,将其替换为您的doInBackground()方法,一切都会正常工作。

protected Void doInBackground ( String ... params ) {

showAttendanceTable();
}
return null ;
}

将这部分代码从showAttendanceTable()中复制到onPreExecute()方法中:

TableLayout tableLayout = findViewById(R.id.table_layout);
int day_in_month = getDayInMonth(month_year);
int row_size = sid_array.length+1;
TableRow[] rows = new TableRow[row_size];
TextView[] rolls_tv = new TextView[row_size];
TextView[] names_tv = new TextView[row_size];
TextView[][] status_tv = new TextView[row_size][day_in_month+1];
for (int i = 0; i < row_size; i++) {
rolls_tv[i] = new TextView(this);
names_tv[i] = new TextView(this);
for (int j = 1; j <= day_in_month; j++) {
status_tv[i][j] = new TextView(this);
}
}