tags:

views:

18

answers:

1

I have a class A which is responsible for fetching data from web services in android and i have a class B which is responsible for creating and rendering List View in android

Now how can i populate data on List View once the data is fetched successfully. well i have following options but which one is better .. (if someone has anyother then i don't mind)

1) Create an intent of Class B and pass the data that need to be populated

2) Creating a static method int Class B which will accept the data as an argument and call setListAdapter

in class B i have used Holder Patter to create ListActivity. ( for reference List14.java in samples of Android )

+1  A: 

Option #1 is not a good idea, IMHO.

Option #2 is impossible, since setListAdapter() is not a static method.

Class A's responsibility should be to fetch and store data from Web services. Here, "store" could be:

  • A database
  • A flat file
  • Data cached in a custom Application object
  • Data cached in a static data member

Class A can then notify class B to get the data from the store. Here, "notify" could be:

  • a broadcast Intent
  • using the PendingIntent from createPendingResult()
  • some other PendingIntent
  • a callback or listener object registered by Class B on Class A via bindService() and use of the subsequent local service API (assuming class A is a Service)
CommonsWare
well i am planning to store data in static data members for Class A and for class B i think last option i.e. to use call back or listener is better. as my Class B is responsible for creating ListView.so the scenario is to fetch data by class A and newly returned data i.e. title and description want to populate on listview
Hunt