gpt4 book ai didi

android - 如何在 SyncAdapter 完成 Sync 后刷新 Android ListView

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:43 24 4
gpt4 key购买 nike

我扩展了一个 AbstractThreadedSyncAdapter 并让它每隔 x 分钟或当我通过代码手动请求同步时自动与我的服务器同步数据。效果很好。

所以现在下一步是自动更新一个包含消息的 ListView 和另一个包含分配的作业的 ListView。

我发现的示例都假定您正在从同一 Activity 中更改数据集,或者您可以访问 ListView 绑定(bind)到的数据库游标。不幸的是,对于 Android 同步适配器,情况并非如此。它在后台运行,据我所知没有任何有用的引用。

我的同步适配器:

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);
}

}

我的日程 fragment 代码:

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);

FragmentListView 声明一个 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...)。

关于android - 如何在 SyncAdapter 完成 Sync 后刷新 Android ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40867687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com