Android的ListView的notifyDataSetChanged()从服务 [英] Android ListView notifyDataSetChanged() from Service

查看:137
本文介绍了Android的ListView的notifyDataSetChanged()从服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从服务器接收邮件,并与消息会更新对象这显示在ListView内部特性的后台服务。

我总是使用runOnUiThread方法来运行listArrayAdapter.notifyOnDataSetChanged()命令。

从某种原因,有时在ListView被刷新,它给我的属性更新,有时没有。

有关测试我添加了一个刷新按钮,我的ListView和当pressed的listArrayAdapter.notifyOnDataSetChanged()被执行。

按钮每次点击的观点是完全刷新。

我实在无法理解为什么当试图从它并不总是有效的服务来刷新,但我想我也许不会永远运行在UIThread ...

我真的很绝望,并会很乐意帮帮忙..

我的code

ServerConnectionManager.java - 延伸服务

  //例如执行时从服务器接收到一个特定消息的命令:
//应用程序的应用程序变量
    公共无效解除好友关系(INT用户id)
    {
        serverResponseManager.onUnFriend(app.getApplicationFriend(用户ID),FALSE);
    }
 

ServerResponseManager.java - 一类处理所有的应用程序响应服务器消息:

 公共无效onUnFriend(FacebookUser facebookUser,布尔isYouRemovedClient){

         //这是物业打电话的时候,这将影响ListView的看法
         //arrayListAdataper.notifyOnDataSetChanged();
        facebookUser.setApplicationFriend(假);
        。app.getApplicationFriends()删除(facebookUser);
        。app.getDatabaseManager()deleteApplicationFriend(facebookUser.getId());

         //如果应用程序在用户界面中(不是在后台)当前正在运行它将运行BaseActivity内的方法
        如果(app.isApplicationInForeground())
        {
            app.getCurrentActivity()onUnFriend(facebookUser)。
            如果(isYouRemovedClient)
                app.showToast(facebookUser.getName()+已经从你的朋友中删除,真正的);
            其他
                app.showToast(facebookUser.getName()+已经删除你的朋友,真正的);
        }
    }
 

BaseActivity.java - 它设置所有的默认配置,它扩展了这一切活动的活动

  //在本例中BaseActivity方法不执行任何操作,但ListViewActivity.java方法来覆盖它
公共无效onUnFriend(FacebookUser facebookUser)
{

}
 

ListViewActivity.java - 扩展BaseActivity并在其一个ListView这应该反映它正在取得公共无效onUnFriend(FacebookUser facebookUser,布尔isYouRemovedClient)在ServerResponseManager在FacebookUser对象属性的变化

  @覆盖
公共无效onUnFriend(FacebookUser facebookUser)
{
    的UpdateView();
}

私人无效的UpdateView()
{
    runOnUiThread(updateViewRunnable());
}

私人可运行updateViewRunnable()
{
    Runnable运行=新的Runnable(){

        @覆盖
        公共无效的run(){
            listArrayAdapter.notifyDataSetChanged();
        }
    };
    返回运行;
}
 

解决方案

不要将业务逻辑。它看起来太复杂,难以辨认。

  1. 在您的服务,广播有关更新信息的意图。
  2. 活动,其中的ListView 是,创建并注册的BroadcastReceiver 的IntentFilter 为您更新事件。
  3. 在你的的BroadcastReceiver 处理更新事件的的onReceive 的方法,例如更新列表。

I have a background service which receive messages from a server and with those message it updates inner properties of objects which are shown in a ListView.

I always uses the runOnUiThread method to run the listArrayAdapter.notifyOnDataSetChanged() command.

From some reason sometimes the ListView is refreshed and it does show me the property update and sometimes it doesn't..

For testing i've added a "refresh" Button to my ListView and when it pressed the listArrayAdapter.notifyOnDataSetChanged() is executed.

Every click on the button the view is refreshed perfectly..

I can't really understand why when trying to refresh from the service it doesn't always work but i think i maybe not always runs on the UIThread...

I'm really hopeless and will glad to get help..

My Code

ServerConnectionManager.java - extends Service

//example of a command executed when a specific message received from the server:
//app is the Application variable
    public void unFriend(int userId)
    {
        serverResponseManager.onUnFriend(app.getApplicationFriend(userId),false);
    }

ServerResponseManager.java - a class that handle all application responses to server messages:

    public void onUnFriend(FacebookUser facebookUser, boolean isYouRemovedClient) {                 

         //this is the property which will effect the ListView view when calling the  
         //arrayListAdataper.notifyOnDataSetChanged();
        facebookUser.setApplicationFriend(false);        
        app.getApplicationFriends().remove(facebookUser);
        app.getDatabaseManager().deleteApplicationFriend(facebookUser.getId());         

         //if the application is currently running in the UI (not on the background) it will run a method inside the BaseActivity
        if (app.isApplicationInForeground())
        {           
            app.getCurrentActivity().onUnFriend(facebookUser);
            if (isYouRemovedClient)
                app.showToast(facebookUser.getName() + " has removed from your friends", true);
            else
                app.showToast(facebookUser.getName() + " has removed you from friends", true);
        }
    }

BaseActivity.java - an Activity which set all default configuration for all Activities which extends it

//in this exemple the BaseActivity method does nothing but the ListViewActivity.java method override it
public void onUnFriend(FacebookUser facebookUser)
{                   

}

ListViewActivity.java - extends BaseActivity and have a ListView in it which should reflect the change in the FacebookUser object property which being made in public void onUnFriend(FacebookUser facebookUser, boolean isYouRemovedClient) in ServerResponseManager.

@Override
public void onUnFriend(FacebookUser facebookUser)
{       
    updateView();
}

private void updateView()
{
    runOnUiThread(updateViewRunnable());
}

private Runnable updateViewRunnable()
{
    Runnable run = new Runnable() {

        @Override
        public void run() {             
            listArrayAdapter.notifyDataSetChanged();
        }
    };
    return run;
}

解决方案

Don't mix business logic. It looks so complicated that is hard to read.

  1. In your service, broadcast an intent with information about update.
  2. In Activity where ListView is, create and register BroadcastReceiver with IntentFilter for your update events.
  3. In onReceive method of your BroadcastReceiver handle update events, for example update list.

这篇关于Android的ListView的notifyDataSetChanged()从服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆