来自服务的 Android ListView notifyDataSetChanged()

标签 android listview android-arrayadapter

我有一个后台服务,它从服务器接收消息,并使用这些消息更新 ListView 中显示的对象的内部属性。

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

出于某种原因,有时 ListView 会刷新,它会向我显示属性更新,但有时不会。

为了测试,我在我的 ListView 中添加了一个“刷新”按钮,当它按下时,执行 listArrayAdapter.notifyOnDataSetChanged()。

每次点击按钮, View 都会完美刷新..

我真的不明白为什么当尝试从服务刷新时它并不总是有效,但我想我可能并不总是在 UIThread 上运行...

我真的很绝望,很乐意得到帮助..

我的代码

ServerConnectionManager.java - 扩展服务

//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 - 一个处理所有应用程序对服务器消息的响应的类:

    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 - 为扩展它的所有 Activity 设置所有默认配置的 Activity

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

}

ListViewActivity.java - 扩展 BaseActivity 并在其中包含一个 ListView,它应该反射(reflect)在 ServerResponseManager 的 public void onUnFriend(FacebookUser facebookUser, boolean isYouRemovedClient) 中所做的 FacebookUser 对象属性的更改。

@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;
}

最佳答案

不要混合业务逻辑。它看起来很复杂,很难阅读。

  1. 在您的服务中,广播一个包含更新信息的 Intent 。
  2. ListView 所在的 Activity 中,为您的更新事件创建并注册 BroadcastReceiverIntentFilter
  3. BroadcastReceiveronReceive 方法中处理更新事件,例如更新列表。

关于来自服务的 Android ListView notifyDataSetChanged(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13145109/

相关文章:

Android - CustomAdapter<MyClass> 不获取对象

java - 如何忽略数组适配器中的第一项

java - getCacheDir() 下的子目录

java - adb 找不到我的设备进行 Android 调试。为什么?

javascript - 使用提要和列表

c# - 带复制粘贴的 ListView

java - Android ListActivity 行颜色基于对象状态

android - "ask, don' t look”如何在Android中应用?

java - 不同语言的 Assets

android - 如何在 ListView 中标记 View ?