简体   繁体   English

不同对象的通用活动

[英]Generic activities for different objects

I'm currently building an app towards an API. 我目前正在针对API构建应用。 Whenever I want to gather a list of objects from the API, the following lifecycle applies: 每当我想从API收集对象列表时,都会应用以下生命周期:

  1. Start activity 开始活动
  2. Invoke GET request on BASE_URI/objects 在BASE_URI /对象上调用GET请求
  3. Fill in the objects in a ListView 在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. 这些活动是由相同的组件构成的:ArrayAdapter,ProgressBar,异步任务等。唯一可能不同的是单击列表中的对象时调用的活动,因为这些对象有时具有不同的属性,因此,想要以不同的样式显示它们。

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? 除了API路径和要反序列化的对象的引用类型之外,我什么都没有传递?

And who would call ActivityStore<Dog> ? 谁会打电话给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. 但是,如果得到类似DogStoreActivity extends ActivityStore<Dog>东西DogStoreActivity extends ActivityStore<Dog> ,那么定义这样的DogStoreActivity extends ActivityStore<Dog>是完全可以的。

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 . 创建一个AsyncTask从服务器获取数据,并将适当的适配器设置为ListView

You can also create one base activity that all other activities extend and put your common code there. 您还可以创建一个其他所有活动都可以扩展的基本活动,并将通用代码放在此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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