android - MVC: who should fetch data, adapter or activity -
I need to bring a list of data from the server, feed on the adapter, and show in the list view.
The question is, what should the backend call, activity or adapter do or?
And please explain why
Please answer in the context of a Model View Controller or similar (I do not know MVC on Android strictly).
Edit:
It is now clear that I have a very bad place to get the adapter data (imagine how flexible this would be. For example, if the same Data has two end points, or I have two data sets, it does not make any sense to bring any adapter data). Other component data should be brought and the adapter should be filled. To do this, activity is a good component.
The accepted answer provides an example of doing this.
anytime will not receive data from any activity because it Block .
You want the doInBackground (override @) the method you want with the AsyncTask parameters and return types and onPostExecute (Override @) method, give it data to call some public method on your activity
Finally, the activity should adapter it with the latest data.
In this way I always get my data and I get the result 100% as I want (Add a progress and make it visible before bringing data and giving it adapter before making it invisible ).
Hope I was clear enough for you. If you want some code as an example, please ask.
Workflow :
1 - Activity -> New MyAsyncTask (...). ); // Get Data @ Override Do Inback Ground
2 - MyAsyncTask.onPostExecute () {myActivity.giveFreshData (...)} // Distribute data
3 - Activity. GiveFreshData (...) {MyListAdapter.giveMyData (...)}
Altough This MVC is not how you should separate the UI from data consumption and data representation.
Comments
Post a Comment