开发者

Refreshing an Activity in Android after updating multiple view contents in an AsyncTask

My main activity calls an AsyncTask when a user clicks a button. This AsyncTask performs a fairly long operation, involving a progress dialog. Some of these operations involve adding TableRow views to an existing table in my main activity.

However, my problem is that after the operation is done, these rows are not showing up in the activity GUI. I know that they are being created properly because if I press "Menu" on my phone to get out of my app and then open the app up again, the TableRow views are populated in their respective positions accordingly. Everything looks perfect...the UI thread just isn't getting reloaded.

Therefore, I am apparently missing a function of some kind to call after the AsyncTask does its duty. Does anyone know what this refresh function might be?

Thank you.

Here is the code:

public void buttonClick(View view) {
    if (view.getId() == R.id.button_begin) {
        cr = this.getContentResolver();
        dialog = new ProgressDialog(this);
        dialog.setCancelable(false);
        dialog.setMessage("Checking...");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // reset the bar to the default value of 0
        dialog.setProgress(0);
        // display the progress bar
        dialog.show();
        new mainAsyncTask().execute();
    }

}

    @Override
    protected void onPreExecute() {

        try {
            //create row span so description can overlap columns in the rows
            span = new TableRow.LayoutParams();
            span.span = 3;

            // set the maximum value
            dialog.setMax(numberOfItems.length);
            //initialize facebook
            fb = new FacebookInfo(context, mainActivity, settings, numberOfItems.length, isPaid, settings.getBoolean("checkBoxShareShort", false));
            description = new String[numberOfItems.length];
            factor = new String[numberOfItems.length];
            cool = new String[numberOfItems.length];
            cannot = new String[numberOfItems.length];
            name = new String[numberOfItems.length];
            ivFacebookPub = new ImageView[numberOfItems.length];
            ivShare = new ImageView[numberOfItems.length];
            tableGrid = new TableLayout[numberOfItems.length];

            super.onPreExecute();

            }
            catch (Exception ex) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                ex.printStackTrace(pw);
                table.addView(rowError);
                rowError.addView(tvError, 0, span);
            }
    }


    @Override
    protected Void doInBackground(Void... params) {

        for (int i = 0; i < numberOfItems.length; i++){


            try {

                if (Numbers[i] != null) {


                    if (flag == true) {
                        flag = false;
                    }
                    else if (flag == false) {
                        flag = true;
                    }               
                    fb.addDescription(i, justStuff2[1]);
                    fb.addCannot(i, justStuff2[0]);
                    description[i] = justStuff2[1];
                    cannot[i] = justStuff2[0];
                    //initialize table
                    tableGrid[i] = TableVisuals.createTable(context, flag, i, clickViewContact);    
                    //initialize rows
                    TableRow rowNamePhoto2 = new TableRow(context);
                    TableRow rowCannot2 = new TableRow(context);
                    TableRow rowStuff = new TableRow(context);
                    TableRow rowStuff2 = new TableRow(context);
                    TableRow rowInfo2 = new TableRow(context);
                    TableRow rowShare2 = new TableRow(context);

                    //create rows
                    rowNamePhoto2 = TableVisuals.createRow(context, flag);
                    rowCannot2 = TableVisuals.createRow(context, flag);
                    rowStuff = TableVisuals.createRow(context, flag);

                    //initialize views
                    TextView tvNa开发者_JAVA百科me2 = new TextView(context);
                    ImageView ivPhoto2 = new ImageView(context);
                    ImageView ivArrow2 = new ImageView(context);

                    TextView tvCannot2 = new TextView(context);
                    TextView tvDescription = new TextView(context);

                    //add data to views
                    tvName2 = TableVisuals.getName(context, cr, Numbers[i]);
                    ivPhoto2 = TableVisuals.getPhoto(context, cr, Numbers[i]);
                    ivArrow2 = TableVisuals.getArrowIcon(context, cr, Numbers[i]);
                    tvCannot2 = TableVisuals.getCannot(context, justStuff2[0]);
                    tvDescription = TableVisuals.getStuff(context, justStuff2[1]);

                    //add views to row
                    rowNamePhoto2.addView(ivPhoto2, 0);
                    rowNamePhoto2.addView(tvName2, 1);
                    rowNamePhoto2.addView(ivArrow2, 2);
                    rowCannot2.addView(tvCannot2, 0, span);
                    rowStuff.addView(tvDescription, 0, span);



                    //populate data
                    fb.addName(i, (String)tvName2.getText());
                    name[i] = (String)tvName2.getText();

                    if (isPaid == true) {

                        //create rows
                        rowStuff2 = TableVisuals.createRow(context, flag);
                        rowInfo2 = TableVisuals.createRow(context, flag);

                        //initialize views
                        TextView tvRatio2 = new TextView(context);
                        TextView tvVariance2 = new TextView(context);

                        //add data to views
                        tvRatio2 = TableVisuals.getRatio(context, justStuff2[3]);
                        tvVariance2 = TableVisuals.getVariance(context, justStuff2[2]);

                        //add views to row
                        rowStuff2.addView(tvRatio2, 0, span);
                        rowInfo2.addView(tvVariance2, 0, span);

                        //populate data
                        fb.addCool(i, (String)tvRatio2.getText());
                        fb.addFactor(i, (String)tvVariance2.getText());
                        cool[i] = (String)tvRatio2.getText();
                        factor[i] = (String)tvVariance2.getText();
                        }

                    if (settings.getBoolean("checkBoxSharing", true) == true) {

                        rowShare2 = TableVisuals.createRow(context, flag);
                        ivFacebookPub[i] = TableVisuals.createFacebookPublish(context, i, fb.clickFacebookPublish);
                        //add facebookPublish button
                        rowShare2.addView(ivFacebookPub[i], 0);
                        //add share button
                        ivShare[i] = TableVisuals.createShare(context, i, fb.clickShareResults);
                        rowShare2.addView(ivShare[i], 1);

                        }           
                    //add rows to table
                    tableGrid[i].addView(rowNamePhoto2);
                    tableGrid[i].addView(rowCannot2);
                    if (isPaid) {
                        tableGrid[i].addView(rowStuff2);
                        tableGrid[i].addView(rowInfo2);
                    }
                    tableGrid[i].addView(rowStuff);
                    if (settings.getBoolean("checkBoxSharing", true) == true) {
                    tableGrid[i].addView(rowShare2);
                    }
                    //add table to the main table
                    table.addView(tableGrid[i]);
                    //initialize line
                    TableRow rowLine2 = new TableRow(context);
                    rowLine2 = TableVisuals.createLineRow(context);
                    TextView tvLine2 = new TextView(context);
                    tvLine2 = TableVisuals.createRowLine(context);
                    //add line view
                    rowLine2.addView(tvLine2, 0, span);
                    table.addView(rowLine2);                        
                    }
                publishProgress(i,0);      
    }
            catch (Exception ex) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                ex.printStackTrace(pw);
                publishProgress(i, 1);

            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... value) {
        dialog.setProgress(value[0]);
    }

    @Override
    protected void onPostExecute(Void unused) {
        //close progress bar dialog
        dialog.dismiss();   
         //here is where I try the invalidate and postInvalidate
       table.invalidate();
       for (int i = 0; i < numberOfItems.length; i++) {

           tableGrid[i].invalidate();
       }


Sounds like your screen is not being refreshed/redrawn. Is this perhaps in an Adapter? If so, call notifyDataSetChanged() on the adapter. If not, try invalidating your TableLayout by calling invalidate()

Edit There is a huge fundamental flaw in your implementation. You're creating UI elements on a non-UI thread. I'm a little surprised your app isn't crashing, but I suppose if you make them in the background thread it can't fuss at you for maniputating them on a background thread, but you're not so lucky when it comes to me :). Everything UI-related needs to be on the UI-thread, whether it be through onProgressUpdate() or in onPostExecute(). I can guarantee your problem stems from this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜