简体   繁体   中英

Generic activities for different objects

I'm currently building an app towards an API. Whenever I want to gather a list of objects from the API, the following lifecycle applies:

  1. Start activity
  2. Invoke GET request on BASE_URI/objects
  3. Fill in the objects in a ListView

The activities are built up from the same components: ArrayAdapter, ProgressBar, an asynchronous task etc. The only things that may differ is the activity that gets called when I click on an object in the list, because the objects sometimes have different properties, I want to display them with a different style.

Now I'm wondering: Is it possible to make a generic activity repository? Such as...

public ActivityStore<T> extends ListActivity {
   ...
}

where I pass nothing but the API path and the reference type of the objects to deserialize?

And who would call ActivityStore<Dog> ? Your manifest ? No, you can't do just that.

But, if you got something like DogStoreActivity extends ActivityStore<Dog> , then it's perfectly fine to define Activities like this.

You can keep just one activity with list view and different adapters for different type of data.

Create one AsyncTask to get the data from server and set appropriate adapter to the ListView .

You can also create one base activity that all other activities extend and put your common code there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM