How to refresh Android listview after syncadapter completes synchronization

I extended abstractthreadedsyncadapter to automatically synchronize data with the server every X minutes or when I manually request synchronization through code. It works perfectly

Therefore, the next step is to automatically update the listview containing the message and another listview containing the assigned job

All the examples I found assume that you are changing the dataset in the same activity, otherwise you can access the database cursor to which the listview is bound. Unfortunately, this is not the case for the Android synchronization adapter. As far as I know, it runs in the background without referencing anything useful

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

}

My timesheet snippet Code:

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

resolvent:

Send such a local broadcast:

Intent intent = new Intent();
intent.setAction("com.your_package.name.REFRESH_LIST");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

Let the fragment with listview declare a 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
        }
    }
}

Register broadcastreceiver, for example, in onattach():

IntentFilter myFilter = new IntentFilter();
myFilter.addAction("com.your_package.name.REFRESH_LIST");
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, myFilter);

And don't forget to log out, for example, in ondetach()

LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);

In this way, as long as the fragment is attached to the activity, it will get the update message

Another option is to use some type of event bus (green robot, Otto...)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>