开发者

Asynchronous view manipulation android

开发者 https://www.devze.com 2023-04-02 15:47 出处:网络
I have a little problem I spend a lot of time now. I hope you can help me. Well, I have an activity witch loads content out of data base in background.开发者_运维技巧 It returns asynchrony and modifie

I have a little problem I spend a lot of time now. I hope you can help me. Well, I have an activity witch loads content out of data base in background.开发者_运维技巧 It returns asynchrony and modifies my data model. Now I'm looking for a good way to inform the controller to update view. I use the notifyDataChanged method but I got an exception.

Android do not allow to modify a view from another thread. In my opinion this is a basic functionality. So there has to be a way to implement such a functionality.

Can you give me an hint.

I also watch the content observer class. But I don't think that the right one.

Thanks


Please provide some code snippets from your Activity. If you are using AsynkTask which is the best way to do background processing on data, you should use the onPostExecute() method to update your UI, because doInBackground() runs on a background thread, not on the UIThread.

A workaround would be to declare a Handler in your Activity and post a new Runnable in that Handler when you want to acccess the UI, something like this:

    mHandler.post(new Runnable() {
        @Override
        public void run() {
            mAdapter.notifyDataSetChanged();
        }
    }); 

But this is somehow a "hack" and I wouldn't recommend using this. The best way would be to use the AsyncTask.

0

精彩评论

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

关注公众号