简体   繁体   中英

Android (Java): Show Progress-bar while Loading table view data

I have a block of code to load a table view. But for large data, my app is crashing. I want to fix that and also want to show a progress bar while loading the table view data. I tried with the AsyncTask but it is showing Skipped 98 frames! The application may be doing too much work on its main thread Skipped 98 frames! The application may be doing too much work on its main thread .

My onCreate code (Updated): -

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sheet);

        myDb = new DBHelper(this);
        loadIntentInputs();
        setToolbar();

        new MyWorker(this).execute();
    }

My AsyncTask code (Updated): -

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 method code (Updated): -

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);
    }

The problem is you are running new thread on your doInBackground method and you have placed your showAttendanceTable() code inside runOnUiThread() which is causing too many work to consume main thread.Just copy paste the below code, replace it with your doInBackground() method and everything will work fine.

     protected Void doInBackground ( String ... params ) {
     
                showAttendanceTable();
            }
        return null ;
    }

Copy this much part of your code from showAttendanceTable() to onPreExecute() method:

 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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM