将元素添加到Async任务内的布局中

时间:2014-10-05 16:25:47

标签: android android-asynctask

我的应用程序正在读取数据库行,并将TableRowsTextView(或多个)添加到TableLayout。由于数据库可能非常大,我需要实现一个进度条,然后执行Async任务,从数据库中读取并添加到布局中。我现在所做的不起作用,因为我将所有进程填入doInBackground。是否有任何元素需要移出异步任务并使其工作?如果没有,我如何安排它们以便我不会在执行时出错?

public class orders extends Activity {
private DataBaseManager dataBase;

//put the table name and column in constants
public static final String TABLE_NAME = "clients";
public static final String COLUMN_ID = "id";
public static final String COLUMN_CLIENTS = "code";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LoadData task = new LoadData();
    task.execute();
}
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
@Override
protected void onPreExecute()
{
    progressDialog = ProgressDialog.show(YourActivity.this, "Progress Dialog Title Text","Process Description Text", true);
};      
@Override
protected Void doInBackground(Void... params)
{   
    setContentView(R.layout.clients);
    TableLayout tl = (TableLayout) findViewById(R.id.clieTable);
    newClientButton = (Button)findViewById(R.id.newClientButton);
    dataBase = DataBaseManager.instance();

    Cursor cursor = dataBase.select("SELECT * FROM " + TABLE_NAME);
    while (cursor.moveToNext()) {

        int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));


            TextView textClieCode = new TextView(this);
            textClieCode.setLayoutParams(new TableRow.LayoutParams(0, 60, 0.04f));
            textClieCode.setBackground(getResources().getDrawable(R.drawable.row_background));
            textClieCode.setTextColor(Color.BLACK);
            textClieCode.setTextSize(18);

            TableRow tr = new TableRow(this);
            tr.addView(textClieCode);
            tl.addView(tr);

            String code = cursor.getString(cursor.getColumnIndex(COLUMN_CLIENTS));

            textClieCode.append(" " + code + "\n");                   
        }

    }
    return null;
}       
@Override
protected void onPostExecute(Void result)
{
    super.onPostExecute(result);
    progressDialog.dismiss();
};

} }

2 个答案:

答案 0 :(得分:0)

我不确定此代码会对您有所帮助。但您应该使用 runOnUiThread

@Override
protected Void doInBackground(Void... params)
{   
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            setContentView(R.layout.clients);


            TableLayout tl = (TableLayout) findViewById(R.id.clieTable);
            newClientButton = (Button)findViewById(R.id.newClientButton);
            dataBase = DataBaseManager.instance();

            Cursor cursor = dataBase.select("SELECT * FROM " + TABLE_NAME);
            while (cursor.moveToNext()) {

                int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
                TextView textClieCode = new TextView(this);
                textClieCode.setLayoutParams(new TableRow.LayoutParams(0, 60, 0.04f));
                textClieCode.setBackground(getResources().getDrawable(R.drawable.row_background));
                textClieCode.setTextColor(Color.BLACK);
                textClieCode.setTextSize(18);

                    TableRow tr = new TableRow(this);
                    tr.addView(textClieCode);
                    tl.addView(tr);

                    String code = cursor.getString(cursor.getColumnIndex(COLUMN_CLIENTS));

                    textClieCode.append(" " + code + "\n");                   
                }
            }

        }
    });
    return null;
}       

答案 1 :(得分:0)

AsyncTasks也有onProgressUpdate(在UI线程上运行),可以通过DoInBackground方法调用来更新UI。

您应该使用要显示的数据调用publishProgress来触发更新(类型取决于您的任务的第二种类型AsyncTask<Void, **String**, Void>,并使用该方法修改您的用户界面。

示例:

 private class MyTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     for (int i = 0; i < 100; i++) {
         publishProgress(i);
     }
     return -1;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }
 }

并且,不要在后台线程中调用setContentView(R.layout.clients); ...