在 AsyncTask 中更新多个视图内容后刷新 Android 中的 Activity

发布于 2024-11-25 14:26:50 字数 8506 浏览 0 评论 0原文

当用户单击按钮时,我的主要活动调用 AsyncTask。此 AsyncTask 执行相当长的操作,涉及进度对话框。其中一些操作涉及将 TableRow 视图添加到我的主要活动中的现有表中。

但是,我的问题是,操作完成后,这些行没有显示在活动 GUI 中。我知道它们正在正确创建,因为如果我按手机上的“菜单”退出我的应用程序,然后再次打开应用程序,TableRow 视图将相应地填充在各自的位置。一切看起来都很完美...UI 线程只是没有重新加载。

因此,我显然缺少在 AsyncTask 履行其职责后调用的某种函数。有谁知道这个刷新功能是什么?

谢谢。

这是代码:

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 tvName2 = 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();
       }

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 tvName2 = 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();
       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2024-12-02 14:26:50

听起来你的屏幕没有被刷新/重画。这可能是在适配器中吗?如果是这样,请在适配器上调用 notifyDataSetChanged()。如果没有,请尝试通过调用 invalidate() 使您的 TableLayout 无效

编辑 您的实现中存在巨大的基本缺陷。您正在非 UI 线程上创建 UI 元素。我有点惊讶你的应用程序没有崩溃,但我想如果你在后台线程中创建它们,它就不会因为你在后台线程上操作它们而大惊小怪,但当涉及到时你就没那么幸运了我 :)。所有与 UI 相关的内容都需要位于 UI 线程上,无论是通过 onProgressUpdate() 还是在 onPostExecute() 中。我可以保证你的问题源于此。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文