我有一个使用自定义游标适配器SavedTripsAdapter填充数据库的列表视图。Android如何调用notifyDataSetChanged刷新AsyncTask中的列表onPostExecute
下面是我如何初始化适配器: allTrips = mDb.fetchAllTrips();
String[] from = new String[] { "purp", "fancystart", "fancyinfo", "endtime", "start", "distance", "status" };
int[] to = new int[] { R.id.TextViewPurpose, R.id.TextViewStart,
R.id.TextViewInfo };
sta = new SavedTripsAdapter(getActivity(),
R.layout.saved_trips_list_item, allTrips, from, to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(sta);
当我点击列表项,我可以再通过调用该功能之后,它在完成的AsyncTask上传。
在FragmentSavedTripsSection:
private void retryTripUpload(long tripId) {
TripUploader uploader = new TripUploader(getActivity());
uploader.execute();
}
在TripUploader:
@Override
protected void onPostExecute(Boolean result) {
try {
if (result) {
Toast.makeText(mCtx.getApplicationContext(),"Trip uploaded successfully.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mCtx.getApplicationContext(),"Cycle Atlanta couldn't upload the trip, and will retry when your next trip is completed.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// Just don't toast if the view has gone out of context
}
}
然而,TripUploader是一个单独的类。旅行上传成功后,数据库被更新,所以我想刷新列表使用savedTripsAdapter.notifyDataSetChanged()
在哪里以及应该如何把savedTripsAdapter.notifyDataSetChanged()?
如果我把它放在列表视图类中,它不会被调用onPostExecute。但是,如果我将它放在TripUploader的onPostExecute函数中,我如何才能访问我在列表视图类中实例化的savedTripsAdapter的同一个实例?
感谢您的帮助!
您是否在FragmentSavedTripsSection类的Lsitview上设置适配器? –
@amitsingh我添加了一些代码。谢谢! – Anhong
makle“FragmentSavedTripsSection”类中的“public”函数用于通知适配器并从AsyncTask的onPostExecute()中调用该函数。 –