开发者

android how to notify a activity From backgruond

I have a tab_Host having three activities (Activity1, Activity2, Activity3) and I 开发者_如何转开发am downloading XMLs and icon sized images from the server. I want to refresh the list view for every 50 images download, I need to refresh Activity2 having ListView to show icons and text in List View from the background(not from UI thread)


Can't paste code here but I can give you idea for what we have done.

Assumptions: You are calling the xml data from activity 2 [I will prefer it as B].

Now what you will do is described here

  1. Get an AsyncTask class [TaskA], you are going to download and process your data here. As there are not more details here I can tell you only this much things.

  2. Create an object of TaskA in Activity B with proper arguments.

  3. Start your AsyncTask from activity B by calling taskA.execute.

  4. In your async task you must keep three methods in mind and work with them closely a. doInBackground(Params...) that method will be the entry point of your async task, b. onPostExecute(Result) this will determine that your task is been finished c. onProgressUpdate(Integer... progress) that will update your activity about progress. This is the method you will need after every 50 records are downloaded

  5. doInBackground will be called when you call task.execute from your activity.

  6. When you download and process 50 images call publishProgress() that will call onProgressUpdate(Integer... progress) from where you have to notify your activity that list is going to be updated. If you can access list adapter object from your async task it will be easy. Just edit the list adapter's relative object. call List.notifyDataSetChanged() so it will be updated on your list automatically.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜