我有一個使用自定義遊標適配器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()中調用該函數。 –