0

下面是我的 Table 类,它从 XML 创建一个 tableview AsyncTask

public class Table extends Activity {

    readXML readxml = new readXML();
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        ThreadPolicy tp = ThreadPolicy.LAX;
        StrictMode.setThreadPolicy(tp);

        performBackgroundTask mTask = new performBackgroundTask(
                getBaseContext());
        mTask.execute();
        // readxml.read();

    }


    private void tableView() {



        // TODO Auto-generated method stub
        TableLayout t1;
        TableLayout tl = (TableLayout) findViewById(R.id.main_table);

        TableRow tr_head = new TableRow(this);
        tr_head.setId(10);
        tr_head.setBackgroundColor(Color.parseColor("#afeeee"));
        tr_head.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        TextView label_id = new TextView(this);
        label_id.setId(20);
        label_id.setText("id");
        label_id.setTextSize(18);
        label_id.setTextColor(Color.BLACK);
        label_id.setPadding(5, 5, 5, 5);
        tr_head.addView(label_id);// add the column to the table row here

        TextView labelName = new TextView(this);
        labelName.setId(21);// define id that must be unique
        labelName.setText("                                Name"); // set the
                                                                    // text for
                                                                    // the
                                                                    // header
        labelName.setTextSize(18);
        labelName.setTextColor(Color.BLACK); // set the color
        labelName.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelName); // add the column to the table row

        TextView labelUnit = new TextView(this);
        labelUnit.setId(21);// define id that must be unique
        labelUnit.setText("Unit"); // set the text for the header
        labelUnit.setTextSize(18);
        labelUnit.setTextColor(Color.BLACK); // set the color
        labelUnit.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelUnit); // add the column to the table row

        TextView labelMinimum = new TextView(this);
        labelMinimum.setId(21);// define id that must be unique
        labelMinimum.setText("Minimum"); // set the text for the header
        labelMinimum.setTextSize(18);
        labelMinimum.setTextColor(Color.BLACK); // set the color
        labelMinimum.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelMinimum); // add the column to the table row

        TextView labelMaximum = new TextView(this);
        labelMaximum.setId(21);// define id that must be unique
        labelMaximum.setText("Maximum"); // set the text for the header
        labelMaximum.setTextSize(18);
        labelMaximum.setTextColor(Color.BLACK); // set the color
        labelMaximum.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelMaximum); // add the column to the table row

        TextView labelAccuracy = new TextView(this);
        labelAccuracy.setId(21);// define id that must be unique
        labelAccuracy.setText("Accuracy"); // set the text for the header
        labelAccuracy.setTextSize(18);
        labelAccuracy.setTextColor(Color.BLACK); // set the color
        labelAccuracy.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelAccuracy); // add the column to the table row

        TextView labelSensorType = new TextView(this);
        labelSensorType.setId(21);// define id that must be unique
        labelSensorType.setText("SensorType"); // set the text for the header
        labelSensorType.setTextSize(18);
        labelSensorType.setTextColor(Color.BLACK); // set the color
        labelSensorType.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelSensorType); // add the column to the table row

        TextView labelRegisterAddress = new TextView(this);
        labelRegisterAddress.setId(21);// define id that must be unique
        labelRegisterAddress.setText("RegisterAddress"); // set the text for the
                                                            // header
        labelRegisterAddress.setTextSize(18);
        labelRegisterAddress.setTextColor(Color.BLACK); // set the color
        labelRegisterAddress.setPadding(5, 5, 5, 5); // set the padding (if
                                                        // required)
        tr_head.addView(labelRegisterAddress); // add the column to the table
                                                // row

        TextView labelOffset = new TextView(this);
        labelOffset.setId(21);// define id that must be unique
        labelOffset.setText("Offset"); // set the text for the header
        labelOffset.setTextSize(18);
        labelOffset.setTextColor(Color.BLACK); // set the color
        labelOffset.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelOffset); // add the column to the table row

        TextView labelTimeStamp = new TextView(this);
        labelTimeStamp.setId(21);// define id that must be unique
        labelTimeStamp.setText("TimeStamp"); // set the text for the header
        labelTimeStamp.setTextSize(18);
        labelTimeStamp.setTextColor(Color.BLACK); // set the color
        labelTimeStamp.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(labelTimeStamp); // add the column to the table row

        tl.addView(tr_head, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        // fájl megnyitása hogy tudjuk hány id van

        try {
            File fXmlFile = new File(main.fileWithPath);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder;
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("DAQChannel");

            readxml.TablaEddig = nList.getLength();

        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // fájl vége

        Integer count = 0;
        for (int i = 0; i < readxml.TablaEddig; i++) {
            readxml.read();
            // Create the table row
            TableRow tr = new TableRow(this);
            if (count % 2 != 0)
                tr.setBackgroundColor(Color.LTGRAY);
            else {
                tr.setBackgroundColor(Color.WHITE);
            }
            tr.setId(100 + count);
            tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            // Create 5 columns to add as table data
            TextView labelid = new TextView(this);
            labelid.setId(count);
            labelid.setGravity(Gravity.CENTER);
            labelid.setText(readxml.getID() + " ");
            labelid.setTextColor(Color.BLACK);
            tr.addView(labelid);

            // Create 5 columns to add as table data
            TextView labelname = new TextView(this);
            labelname.setId(count);
            labelname.setGravity(Gravity.CENTER);
            labelname.setText(readxml.getName() + " ");
            labelname.setTextColor(Color.BLACK);
            tr.addView(labelname);

            // Create 5 columns to add as table data
            TextView labelunit = new TextView(this);
            labelunit.setId(count);
            labelunit.setGravity(Gravity.CENTER);
            labelunit.setText(readxml.getUnit() + " ");
            labelunit.setTextColor(Color.BLACK);
            tr.addView(labelunit);

            // Create 5 columns to add as table data
            TextView labelmin = new TextView(this);
            labelmin.setId(count);
            labelmin.setGravity(Gravity.CENTER);
            labelmin.setText(readxml.getMinimum() + " ");
            labelmin.setTextColor(Color.BLACK);
            tr.addView(labelmin);

            // Create 5 columns to add as table data
            TextView labelmax = new TextView(this);
            labelmax.setId(count);
            labelmax.setGravity(Gravity.CENTER);
            labelmax.setText(readxml.getMaximum() + " ");
            labelmax.setTextColor(Color.BLACK);
            tr.addView(labelmax);

            // Create 5 columns to add as table data
            TextView labelacc = new TextView(this);
            labelacc.setId(count);
            labelacc.setGravity(Gravity.CENTER);
            labelacc.setText(readxml.getAccuracy() + " ");
            labelacc.setTextColor(Color.BLACK);
            tr.addView(labelacc);

            // Create 5 columns to add as table data
            TextView labelst = new TextView(this);
            labelst.setId(count);
            labelst.setGravity(Gravity.CENTER);
            labelst.setText(readxml.getSensorType() + " ");
            labelst.setTextColor(Color.BLACK);
            tr.addView(labelst);

            // Create 5 columns to add as table data
            TextView labelra = new TextView(this);
            labelra.setId(count);
            labelra.setGravity(Gravity.CENTER);
            labelra.setText(readxml.getRegisterAddress() + " ");
            labelra.setTextColor(Color.BLACK);
            tr.addView(labelra);

            // Create 5 columns to add as table data
            TextView labeloff = new TextView(this);
            labeloff.setId(count);
            labeloff.setGravity(Gravity.CENTER);
            labeloff.setText(readxml.getOffset() + " ");
            labeloff.setTextColor(Color.BLACK);
            tr.addView(labeloff);

            // Create 5 columns to add as table data
            TextView labeldate = new TextView(this);
            labeldate.setId(count);
            labeldate.setGravity(Gravity.CENTER);
            labeldate.setText(readxml.getTimeStamp() + " ");
            labeldate.setTextColor(Color.BLACK);
            tr.addView(labeldate);

            // finally add this to the table row
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            count++;
        }

    }

    // ASync Task Begin to perform Billing information
    class performBackgroundTask extends AsyncTask<Void, Void, Void> {

        private Context context;

        private ProgressDialog dialog;

        public performBackgroundTask(Context context) {
            performBackgroundTask.this.context = context;
        }

        protected void onPreExecute() {
             super.onPreExecute();
             dialog = ProgressDialog.show(table.this,
             "Feldolgozás",
             "Folyamatban...");

        }

        protected Void doInBackground(Void... params) {

            // tableView();
            return null;
        }

        protected void onPostExecute(Void unused) {
            if (dialog != null)
                dialog.dismiss();
            tableView();
            Toast.makeText(getApplicationContext(), "Hinnyeeeeeee",
                    Toast.LENGTH_LONG).show();

        }

    }
}// oncreate

所以问题是:它第一次没有显示进度对话框,但是当它结束时它会显示一秒钟然后立即隐藏它。

我放入了 tableview() void,它在执行后对 UI 做一些事情。

4

1 回答 1

1

你期待它做什么?您在 preInit 中“创建”它(我不确定对 show() 的静态调用是否正确),在 doInBackground() 中什么都不做(立即返回 null),然后在执行后立即将其关闭。

编辑:

你想让它做什么?“正常”的进展是:

在 onCreate 中初始化,如:

        mSpinnerProgress = new ProgressDialog(activity);
        mSpinnerProgress.setIndeterminate(true);
        mSpinnerProgress = new ProgressDialog(MyClass.this);
        mSpinnerProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mSpinnerProgress.setMessage("Working");
        mSpinnerProgress.setCancelable(false);
        mSpinnerProgress.setCanceledOnTouchOutside(false);

在 AsynchTask 中有:

    @Override
    protected void onPreExecute() {
        // show the progress dialog
        super.onPreExecute();
        mSpinnerProgress.show();
    }

在 DoInBackground 中,进行一个或多个调用,例如

@Override
    protected Object doInBackground(String[]... params) {
       while(loop) {
           // loop work
           publishProgress(<something>);
        }

在 OnProgress 中,“做一些事情”来更新你的消息,增加进度,无论什么:

    @Override
    protected void onProgressUpdate(Integer... progress) {
        mSpinnerProgress.setMessage("new mesasge");
             // Or update the progress via .setProgress() whatever.
    }

完成后,将其关闭:

    @Override
    protected void onPostExecute(Object obj) {
        mSpinnerProgress.dismiss();
}

如果您在后台函数中什么都不做,那么您将看到的只是进度条的闪烁(可能,因为它的显示/更新速度很慢,因此在您关闭它之前它甚至可能不会通过 show() 绘制)

于 2013-10-04T14:20:29.583 回答