我有一個ListView,我需要使用後臺線程填充。該列表需要在每個項目被檢索時更新。下面是我如何實現這個的一個非常簡單的例子。AsyncTask訪問Sqlite數據庫導致崩潰
public class DownloadTask extends AsyncTask <MyUserObject, Integer, String>
{
@Override
protected MyUserObject doInBackground(MyUserObject... myUserObj)
{
MyUserObject muo = null;
int nCount = myUserObj.length;
if(nCount > 0)
muo = myUserObj[0];
muo.DownloadStuff();
return muo.getUserName();
}
protected void onPostExecute(String userName)
{
adapter.names.add(userName);
adapter.notifyDataSetChanged();
}
}
public class MyAdapterClass extends BaseAdapter
{
private ArrayList<String>names;
public MyAdapterClass(Context context)
{
names = new ArrayList<String>();
}
public fillList()
{
for(int i=0; i<users.length; i++)
{
DownloadTask task = new DownloadTask();
task.execute(users[i]);
}
}
在上述例子中,「適配器」是類型MyAdapterClass的目的,並且其fillList()方法是什麼啓動線程。在onPostExecute()中調用notifyDataSetChanged()會在數據到達時更新我的ListView。
問題是,我正在訪問我在'doStackackground'中調用的DownloadStuff()中的sqlite數據庫,並且有多個線程訪問數據庫導致它崩潰(如果我註釋掉所有數據庫活動在這裏,那麼它運行良好),下面是我如何努力,以解決這一問題,但它仍然崩潰。作爲數據從後臺線程檢索到的我怎麼能有我的ListView更新有什麼建議?
Semaphore semaphore = new Semaphore(1, true);
public synchronized void DownloadStuff()
{
semaphore.acquire(1);
// ... DB operations ... //
semaphore.release(1);
}
它確實如果你能集中你的異步工作到一個更好的(並會避免同步問題^^ –
謝謝你,我不喜歡多個線程或錯過了進展更新,這工作! –
我的新關注AsyncTask永遠不會被銷燬,即使Activity被銷燬也是如此憤怒,東西告訴我,我不應該使用AsyncTask:s –