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



我的应用程序正在读取数据库行,并将带有TextView(或多个)的TableRows添加到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();
};

}}

我不确定这段代码是否会对您有所帮助。但是您应该使用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;
}       

AsyncTasks还有一个onProgressUpdate(在UI线程上运行),可以从DoInBackground方法中调用它来更新UI。

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

示例:

 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);。。。

相关内容

  • 没有找到相关文章

最新更新