我有一个问题如何更新使用自定义适配器的ListView
,我尝试执行thread while (true).
Start()以始终监听服务器(不工作)。
我想更新列表(状态(offine,online ..),图像等。)。例如,在我的ListView
:
User 1 offine
User 2, offine
- Waiting, after 3 seconds
User 1 online
User 2, offline
我已经读过这可以通过handler
完成:
android listview update/refresh data without closing the app
https://stackoverflow.com/questions/18030607/update-list-view-in-android
但是有一种方法可以在没有处理程序的情况下从服务器刷新数据吗?
我加载列表(如何始终实现?):
listAdapter1 = getListData();
customListAdapter = new CustomListAdapter(getApplicationContext(), listAdapter1);
user_list.setAdapter(customListAdapter);
egisterForContextMenu(user_list);
我的完整日志猫:
03-17 22:46:07.439: E/AndroidRuntime(6026): FATAL EXCEPTION: main
03-17 22:46:07.439: E/AndroidRuntime(6026): java.lang.IllegalStateException: Observer android.widget.AbsListView$AdapterDataSetObserver@4e9c7894 was not registered.
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.database.Observable.unregisterObserver(Observable.java:69)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.widget.BaseAdapter.unregisterDataSetObserver(BaseAdapter.java:42)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.widget.AbsListView.onDetachedFromWindow(AbsListView.java:2638)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.View.dispatchDetachedFromWindow(View.java:12160)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2570)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2568)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2568)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2568)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2568)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewRootImpl.dispatchDetachedFromWindow(ViewRootImpl.java:2820)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewRootImpl.doDie(ViewRootImpl.java:5162)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.ViewRootImpl.die(ViewRootImpl.java:5145)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.WindowManagerGlobal.removeViewLocked(WindowManagerGlobal.java:359)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:309)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3535)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.app.ActivityThread.access$1200(ActivityThread.java:141)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.os.Looper.loop(Looper.java:137)
03-17 22:46:07.439: E/AndroidRuntime(6026): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-17 22:46:07.439: E/AndroidRuntime(6026): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 22:46:07.439: E/AndroidRuntime(6026): at java.lang.reflect.Method.invoke(Method.java:525)
03-17 22:46:07.439: E/AndroidRuntime(6026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-17 22:46:07.439: E/AndroidRuntime(6026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-17 22:46:07.439: E/AndroidRuntime(6026): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
实际上有一种方法可以实现上述功能。请参阅以下几点,以便您了解这一点。
您需要alarmmanager,broadcastreceiver&为此服务。
AlarmManager将帮助您每3秒检查一次更新。
BroadcastReceiver&服务将有助于处理新来的通知&更新用户界面。
您需要使用" notifyDataSetChanged()"显示更新列表视图的方法。
这4个步骤足以实现上述逻辑。如果您有任何疑问,仍然可以与我讨论。