I have a RecycleView and I want to loop through the all the rows in the RecycleView to find the rows in which the items have not been inserted to the MySQL database yet. I want to upload them automatically into the database if internet connection is available. If upload is successful I want to change the status to inserted. How do I do it?
-------------------------
*Item* | *Status* |
-------------------------
Apple | Not Inserted |
-------------------------
Egg | Inserted |
-------------------------
Orange| Not Inserted |
-------------------------
You must separate your presentation from your business logic. Your recyclerView's responsibility is just displaying your data model. So all you have to do is changing your data model and asking recyclerView to update UI. So you must loop through your data model not your recyclerView rows.
Solution:
You can loop through all of your data models that your adapter has and then put them in a queue and upload them one by one.
for example you can pass the queue to an intentService and start uploading. after finishing you can notify activity or fragment by using eventBus, Otto, LocalBroadcast. then you update the data model state to inserted and notify the adapter that you have done.
But note that your user may navigate from that activity so persisting your data model in a database and loading and updating it is a cleanest solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With