我扩展了一个AbstractThreadedSyncAdapter,让它每隔x分钟自动与我的服务器同步数据,或者当我通过代码手动请求同步时。它运作得很好。
现在,下一步是自动更新包含消息的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;
}
}
答案 0 :(得分:3)
//有什么人愿意提醒他们应该放松一下吗?
发送本地广播,如下所示:
Intent intent = new Intent();
intent.setAction("com.your_package.name.REFRESH_LIST");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
让Fragment
与ListView
声明为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
,class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil) { (note) in
// put a breakpoint on the next line
Swift.print(note)
}
}
}
就会收到更新消息。
另一种选择是使用某种类型的事件总线(greenrobot,Otto ......)。