2014-10-05 74 views
1

我的应用程序正在读取数据库行并将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(); 
}; 

}}

+0

代码的输出是什么?它会崩溃吗? – 2014-10-05 16:31:27

+0

@NadeemIqbal是的,只有创建视图层次结构的原始线程ca ñ触及其观点。 – user3541436 2014-10-05 16:39:05

回答

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; 
}  
+0

谢谢,虽然我还有其他几个错误,但这绝对是正确的。 – user3541436 2014-10-05 16:54:52

+1

不要这样做。整个doInBackground方法在主线程中执行,那么执行异步任务有什么意义? – 2014-10-05 16:55:01

0

AsyncTasks也有onProgressUpdate与可称为形式DoInBackground方法来更新UI(这是在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); ...