2014-01-24 78 views
0

我有一个使用自定义游标适配器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的同一个实例?

感谢您的帮助!

+0

您是否在FragmentSavedTripsSection类的Lsitview上设置适配器? –

+0

@amitsingh我添加了一些代码。谢谢! – Anhong

+0

makle“FragmentSavedTripsSection”类中的“public”函数用于通知适配器并从AsyncTask的onPostExecute()中调用该函数。 –

回答

2

notifyDataSetChanged()的典型用法是在处理视图的同一个类中调用它。

例如,在你的 onViewCreated(),你到了ListView参考和适配器,当你有你的数据准备好,一般在onPostExecute()你会打电话notifyDataSetChanged()更新视图。

如果您更喜欢将适配器传递给另一个对象来执行数据操作,那么您可以继续前进并做到这一点,没有什么特别的,因为java通过引用传递对象,所以您的Adapter对象将在两个类中都是同一个对象

编辑:

我的假设是:

  • 你的适配器被称为sta
  • 您的片段称为FragmentSavedTrips
  • 修改数据的对象是电话SavedTripsChanger
  • 您的数据字符串数组是从中调用的。

里面SavedTripsChanger有一个私有变量来保存适配器

私人SavedTripsAdapter mSavedTripsAdapter;

和一个设置器方法(可选getter方法)

public SavedTripsAdapter setSavedTripsAdapter(SavedTripsAdapter mSavedTripsAdapter) { 
    this.mSavedTripsAdapter = mSavedTripsAdapter; 
} 

在创建对象SavedTripsChanger传的mSavedTripsAdapter实例,无论是在构造或使用setter方法。 Inside FragmentSavedTrips

SavedTripsChanger mSavedTripsChanger = new SavedTripsChanger(); 
mSavedTripsChanger.setSavedTripsAdapter(sta); 

现在你已经可以使用它了。修改完的数据,并准备更新内部SavedTripsChanger视图后只需调用notifyDataSetChanged()

mSavedTripsAdapter.notifyDataSetChanged();

+0

当用户选择一行时,它会触发一次旅程被上传到数据库。这个操作是在几个地方触发的,所以我必须把它作为一个分离的类。您能否提供更多关于如何通过适配器的细节?谢谢! – Anhong

+0

看到我上面的编辑 –

+0

我添加了代码,并且我成功地通过了mSavedTripsAdapter,但是为什么列表未被mSavedTripsAdapter.notifyDataSetChanged()更新? – Anhong

0
public class TripUploader extends AsyncTask<>{ 
    private listener; 

    public interface YourListener{ 
     onCompleted(boolean result); 
    } 

    public TripUploader(YourListener listener){ 
     this.listener = listener; 
    } 

    protected onPostExecute(boolean result){ 
     listener.onCompleted(result); 
    } 
} 

在主类中,实现这个监听器和执行方法,所以每当您的AsyncTask完成,您会收到它,你可以让你的类中notifydatasetchange。

public class FragmentSavedTripsSection extends Fragment implements YourListener{ 

    public void onCompleted(boolean result){ 
     // notify data 
    } 
} 

使用的界面,你可以使用这个类的其他活动也没有它连接到只有一个类。

+0

我应该在哪里放置每个代码?我有点困惑;) – Anhong

+0

编辑我的答案,现在应该更清楚了 –

+0

我加了代码,但是我不能做公共类FragmentSavedTripsSection extends Fragment implements YourListener {。 – Anhong

相关问题