简体   繁体   English

在后台更新上一个活动的ListView

[英]Update ListView of previous activity in background

I have question regarding my previous ListView activity. 我对以前的ListView活动有疑问。

I have my ListView which is continue updating using socket connection, now when I click on any of the list row i can go to my next screen now when i come back to my previous ListView screen my ListView is start updating again but i want to keep updating my ListView in a background when i am on my nextscreen. 我的ListView正在使用套接字连接继续更新,现在,当我单击任何一个列表行时,现在我可以回到下一个屏幕,当我返回到上一个ListView屏幕时,我的ListView将再次开始更新,但是我想保留当我在nextscreen上时,在后台更新ListView。

Something like i want to keep my previous screen alive when i am on my nextscreen. 当我在下一个屏幕上时,我想保持上一个屏幕的状态。

Sounds to me like your the code you are using to load the data for your ListView is tied to your Activity . 在我看来,您用于加载ListView数据的代码与Activity绑定在一起。 For instance you have an AsyncTask or Thread in your Activity that cointains your ListView and you use it to download data, or do whatever is needed to get the data to populate the list. 例如,您的Activity中有一个AsyncTaskThreadListView然后您可以使用它来下载数据,或执行获取数据以填充列表所需的任何操作。 I also assume you start it in one of the Activity lifecycle methods eg onCreate() . 我还假设您在Activity生命周期方法之一(例如onCreate()启动它。

If that is the case then you should consider seperating the code used for getting the data for the list from your activity code. 如果是这种情况,那么您应该考虑从活动代码中分离用于获取列表数据的代码。

One way to do this is to use a Service which will be able to run independantly of the rest of your application and do all the heavy lifting involed with fetching the data for your list. 一种实现方法是使用Service ,该Service将能够独立于应用程序的其余部分运行,并完成所有繁重的工作,从而获取列表数据。 You can communicate with the service from anywhere in your application. 您可以在应用程序中的任何位置与服务进行通信。 The Service can save the data to a database. Service可以将数据保存到数据库。 Then all you have to do in your Activity is query the database and populate the adapter to get the latest data without much delay. 然后,您在“活动”中要做的就是查询数据库并填充适配器以获取最新数据而没有太多延迟。

You can read more about services here: http://developer.android.com/reference/android/app/Service.html 您可以在此处阅读有关服务的更多信息: http : //developer.android.com/reference/android/app/Service.html

You could (and probably should) do what Paul suggested or you could change to way you switch your screens by using Fragments or a ViewFlipper . 您可以(也许应该)执行Paul的建议, 或者可以使用FragmentsViewFlipper更改切换屏幕的ViewFlipper This way you still run your AsyncTask or Thread while doing something else on a different page. 这样,您仍然可以在其他页面上执行其他操作时仍运行AsyncTaskThread

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

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