SyncAdapter完成同步后如何刷新Android ListView
作者:互联网
我扩展了AbstractThreadedSyncAdapter并使其每隔x分钟或当我手动通过代码请求同步时自动与服务器同步数据.它运作完美.
因此,下一步是自动更新包含消息的ListView和另一个包含分配的作业的ListView.
我发现的所有示例都假定您正在同一Activity中更改数据集,否则您可以访问ListView绑定到的数据库游标.不幸的是,对于Android同步适配器,情况并非如此.据我所知,它在后台运行,没有引用任何有用的东西.
我的SyncAdapter:
public class VttSyncAdapter extends AbstractThreadedSyncAdapter {
private final AccountManager mAccountManager;
public VttSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
mAccountManager = AccountManager.get(context);
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.d("Vtt", "onPerformSync for account[" + account.name + "]");
try
{
//GET SOME DATA FROM WEBSERVICE AND INSERT INTO SQLITE DB
} catch (IOException e) {
e.printStackTrace();
}
//WHAT WOULD ONE DO HERE TO ALERT THE LISTVIEW THAT IT SHOULD REFRESH?
} catch (Exception e) {
e.printStackTrace();
}
public String getsharedresourcestring(String key)
{
Context context = getContext();
SharedPreferences sharedPref = context.getSharedPreferences(context.getString(R.string.preference_file_key), MODE_PRIVATE);
return sharedPref.getString(key,null);
}
}
我的时间表片段代码:
public class ScheduleFragment extends Fragment {
private ListView listView;
private List<DeliveryScheduleEntryModel> schedules;
public ScheduleFragment() {
// Required empty public constructor
}
public static ScheduleFragment newInstance(String param1, String param2) {
ScheduleFragment fragment = new ScheduleFragment();
Bundle args = new Bundle();
//args.putString(ARG_PARAM1, param1);
//args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
//mParam1 = getArguments().getString(ARG_PARAM1);
//mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View ret = inflater.inflate(R.layout.fragment_schedule, container, false);
listView = (ListView) ret.findViewById(R.id.listViewSchedule);
//GET OUR DATA
Activity activity = this.getActivity();
ContentResolver contentResolver = activity.getContentResolver();
schedules = getSchedule(contentResolver);
DeliveryScheduleEntryModelList customList = new DeliveryScheduleEntryModelList(activity, schedules);
listView.setAdapter(customList);
return ret;
}
public List<DeliveryScheduleEntryModel> getSchedule(ContentResolver cr)
{
Context context = getContext();
VttDataSource db = new VttDataSource(context);
db.open();
List<DeliveryScheduleEntryModel> ret = db.getAllDeliveryScheduleEntryModel();
db.close();
return ret;
}
}
解决方法:
//WHAT WOULD ONE DO HERE TO ALERT THE LISTVIEW THAT IT SHOULD REFRESH?
发送这样的本地广播:
Intent intent = new Intent();
intent.setAction("com.your_package.name.REFRESH_LIST");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
让带有ListView的Fragment声明一个BroadcastReceiver:
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String sAction = intent.getAction();
if ("com.your_package.name.REFRESH_LIST".equals(sAction) )
{
// update the ListView here
}
}
}
注册BroadcastReceiver,例如在onAttach()中:
IntentFilter myFilter = new IntentFilter();
myFilter.addAction("com.your_package.name.REFRESH_LIST");
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, myFilter);
并且不要忘记注销例如在onDetach()中
LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
这样,只要将Fragment附加到Activity,它将获得更新消息.
另一种选择是使用某种类型的事件总线(greenrobot,Otto …).
标签:listview,synchronization,android 来源: https://codeday.me/bug/20191112/2023911.html