android asynctask在postexecute中更新listview

android asynctask在postexecute中更新listview,android,listview,asynchronous,Android,Listview,Asynchronous,嗨,我在异步操作后刷新listview时遇到问题。 我有一个simplecursoradapter、custon列表视图和一个按钮。最初,当应用程序启动时,它根据从数据库读取的数据设置listview。然后,当用户单击一个按钮时,它启动一个异步代码来下载一些插入数据库的数据。异步任务启动时,我将显示一个progressdialog,我将在postexecute()中取消该对话框。数据下载得很好,但现在在后台工作完成后,如何在主线程上重新查询光标和更新listview 通过菜单按钮调用“refre

嗨,我在异步操作后刷新listview时遇到问题。
我有一个simplecursoradapter、custon列表视图和一个按钮。最初,当应用程序启动时,它根据从数据库读取的数据设置listview。然后,当用户单击一个按钮时,它启动一个异步代码来下载一些插入数据库的数据。异步任务启动时,我将显示一个progressdialog,我将在postexecute()中取消该对话框。数据下载得很好,但现在在后台工作完成后,如何在主线程上重新查询光标和更新listview

通过菜单按钮调用“refreshRemoteData”方法

这就是我的异步任务的样子

public class MyActivity extends ListActivity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  public void onStart() {
    myDBAdapter = new DBAdapter(this);
myDBAdapter.open();
    populateMyList();
  }

  private void populateMyList() {
    myCursor = myDBAdapter.fetchAllItems();
    startManagingCursor(myCursor); 

    getListView().setAdapter(myDBAdapter);        
  }

  private void refreshRemoteData() {
    mPleaseWaitDialog = ProgressDialog.show(ExpirationDateTrackingActivity.this,
    "Data", "Downloading data", true, true);

download_task = new InfoDownloaderTask();
download_task.execute();
 }

  private class InfoDownloaderTask extends AsyncTask<Object, String, Boolean> {
private static final String DEBUG_TAG = "InfoDownloaderTask";

    protected DBAdapter mylocalDBAdapter=null; 

    @Override
    protected void onPreExecute() {
  Log.e(DEBUG_TAG, "onPreExecute: ");
      mylocalDBAdapter = new DBAdapter(this);
  mylocalDBAdapter.open();
    }

    @Override
    protected void onPostExecute(Boolean result) {
      Log.i(DEBUG_TAG, "onPostExecute: " ); 
      mPleaseWaitDialog.dismiss();
      mlocalDBAdapter.close();
    }

    @Override
     protected Boolean doInBackground(Object... arg0) {
        Log.v(DEBUG_TAG, "doInBackground");
        ///...
        //Update the database 
        mylocalDBAdapter.insertData(....);
        return true;
    }
 } //AsyncTask
}
公共类MyActivity扩展了ListActivity{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public void onStart(){
myDBAdapter=新的DBAdapter(此);
myDBAdapter.open();
populateMyList();
}
私有void populateMyList(){
myCursor=myDBAdapter.fetchAllItems();
开始管理游标(myCursor);
getListView().setAdapter(myDBAdapter);
}
私有void refreshRemoteData(){
SampleAseWaitDialog=ProgressDialog.show(ExpirationDateTrackingActivity.this,
“数据”,“下载数据”,对,对);
下载任务=新信息下载任务();
下载_task.execute();
}
私有类InfoDownloaderTask扩展异步任务{
私有静态最终字符串DEBUG_TAG=“InfoDownloaderTask”;
受保护的DBAdapter mylocalDBAdapter=null;
@凌驾
受保护的void onPreExecute(){
Log.e(DEBUG_标记,“onPreExecute:”);
mylocalDBAdapter=新的DBAdapter(此);
mylocalDBAdapter.open();
}
@凌驾
受保护的void onPostExecute(布尔结果){
Log.i(DEBUG_标记,“onPostExecute:”);
mPleaseWaitDialog.Disclose();
mlocalDBAdapter.close();
}
@凌驾
受保护的布尔doInBackground(对象…arg0){
Log.v(DEBUG_标签,“doInBackground”);
///...
//更新数据库
mylocalDBAdapter.insertData(..);
返回true;
}
}//异步任务
}
我没有看到我的listview在异步操作完成后立即使用新的列表数据进行更新。但是如果我调用另一个activity并返回到listview,那么我会看到所有新项目(列表更新)


我遗漏了什么?

您正在通过mylocalDBAdapter插入数据,但您没有告诉myDBAdapter。尝试调用
myDBAdapter.notifyDataSetChanged()
onPostExecute()
的末尾,您正在通过
mylocalDBAdapter
插入数据,但没有告诉
myDBAdapter
。尝试调用
myDBAdapter.notifyDataSetChanged()onPostExecute()

Ted的末尾有code>,但myDBAdapter没有notifyDataSetChanged()方法?Ted的建议有效。但是我必须将listViewAdapter传递给异步任务,并在postExecute().Ted中调用listViewAdapter.notifyDataSetChanged(),但是myDBAdapter没有notifyDataSetChanged()方法?Ted的建议奏效了。但是我必须将listViewAdapter传递给异步任务,并在postExecute()中调用listViewAdapter.notifyDataSetChanged()。