如何从AsyncTask动态绘制TableLayout

时间:2011-12-20 10:07:33

标签: tablelayout

我的问题是标题所说的,当我按下按钮时它会动态绘制一个表格并且它有效。但是由于我需要在绘制表时显示进程对话框,我尝试使用AsyncTask并且它在doInBackground()函数中不断崩溃。

任何人都可以告诉我我做错了吗?

private class TransactionsTask extends AsyncTask<Long, Void, Void>
{
    final TextView[] tx = new TextView[10];
    final TableRow[] tr = new TableRow[10];

    final TableLayout tl = (TableLayout)findViewById(R.id.maintable);
    final ProgressDialog dialog = ProgressDialog.show(TransactionsActivity.this, "", "Retrieving...", true); 

    protected void onPreExecute() 
    {
        this.dialog.show();
    }

    protected Void doInBackground(Long... params) 
    {   
        // A request for all transactions on the specified data is made to the S-Qube here              
        com.example.sqube.SettingsActivity.out.println(c + "0130;EmployeeId=4;LastTXId=0;FirstTXId=0;SYear=11;SMon=" + (mStartMonth + 1) + ";SDay=" + mStartDay + ";EYear=11;EMon=" + (mEndMonth + 1) + ";EDay=" + mEndDay + ";Area=0;View=1;TXType=1;" + d);
        com.example.sqube.SettingsActivity.out.flush();

        while(control == false)
        {
            try 
            {
                Thread.sleep(10);
            } 
            catch (InterruptedException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        tl.removeAllViews();    // The view is first cleared before drawn again

        // This code it used to dynamically draw the transactions table based on the number of transactions received from the S-Qube
        tr[0] = new TableRow(TransactionsActivity.this);
        tr[1] = new TableRow(TransactionsActivity.this);
        tr[2] = new TableRow(TransactionsActivity.this);
        tr[3] = new TableRow(TransactionsActivity.this);
        tx[0] = new TextView(TransactionsActivity.this);
        tx[1] = new TextView(TransactionsActivity.this);
        tx[2] = new TextView(TransactionsActivity.this);
        tx[3] = new TextView(TransactionsActivity.this);
        tx[0].setText("Date             ");
        tx[0].setTextColor(Color.BLACK);
        tx[0].setTypeface(null, 1);
        tr[0].addView(tx[0]);
        tx[1].setText("Device           ");
        tx[1].setTextColor(Color.BLACK);
        tx[1].setTypeface(null, 1);
        tr[0].addView(tx[1]);
        tx[2].setText("Access Type      ");
        tx[2].setTextColor(Color.BLACK);
        tx[2].setTypeface(null, 1);
        tr[0].addView(tx[2]);
        tx[3].setText("Areas        ");
        tx[3].setTextColor(Color.BLACK);
        tx[3].setTypeface(null, 1);
        tr[0].addView(tx[3]);
        tl.addView(tr[0], new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
               LayoutParams.WRAP_CONTENT));
        tl.addView(tr[1], new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
               LayoutParams.WRAP_CONTENT));
        tl.addView(tr[2], new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
               LayoutParams.WRAP_CONTENT));
        tl.addView(tr[3], new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
               LayoutParams.WRAP_CONTENT));
        for(i = 0; i < com.example.sqube.SettingsActivity.Count1; i++)
        {
            tr[i] = new TableRow(TransactionsActivity.this); 

            tx[0] = new TextView(TransactionsActivity.this);
            tx[1] = new TextView(TransactionsActivity.this);
            tx[2] = new TextView(TransactionsActivity.this);
            tx[3] = new TextView(TransactionsActivity.this);
            tx[0].setText(Date[i] + "               ");
            tx[0].setTextColor(Color.BLACK);
            tx[0].setTypeface(null, 1);
            tx[1].setText(Device[i] + "             ");
            tx[1].setTextColor(Color.BLACK);
            tx[1].setTypeface(null, 1);
            tx[2].setText(Access_Type[i] + "                ");
            tx[2].setTextColor(Color.BLACK);
            tx[2].setTypeface(null, 1);
            tx[3].setText(Area[i] + "               ");
            tx[3].setTextColor(Color.BLACK);
            tx[3].setTypeface(null, 1);
            tr[i].addView(tx[0]);
            tr[i].addView(tx[1]);
            tr[i].addView(tx[2]);
            tr[i].addView(tx[3]);

            tl.addView(tr[i], new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                   LayoutParams.WRAP_CONTENT));
        }
        com.example.sqube.SettingsActivity.Count1 = 0;
        control = false;
        return null;
    }

    protected void onPostExecute(final Void unused) 
    {
        dialog.dismiss();
    }

}

0 个答案:

没有答案