android中ListView数据刷新时的同步方法
程序员文章站
2023-02-02 11:07:30
本文实例讲述了android中listview数据刷新时的同步方法。分享给大家供大家参考。具体实现方法如下:
public class main extends...
本文实例讲述了android中listview数据刷新时的同步方法。分享给大家供大家参考。具体实现方法如下:
public class main extends baseactivity { private static final string tag = "tag"; private static final int status_change = 0; expandablelistview melv; arraylist<groupinfo> mgrouparray; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); melv = (expandablelistview) findviewbyid(r.id.contact_list); mstatus = (textview) findviewbyid(r.id.setstatus); mgrouparray = getintent().getparcelablearraylistextra("grouparray");// => 取数据 mexpandableadapter = new expandableadapter(this, main.this); melv.setadapter(mexpandableadapter); // 异步对比服务器分组和本地分组 handlerthread handlerthread = new handlerthread("handler_thread"); handlerthread.start(); updategrouphandler myhandler = new updategrouphandler( handlerthread.getlooper()); message message = myhandler.obtainmessage(); message.sendtotarget(); mhandler = new handler() { public void handlemessage(message msg) { switch (msg.what) { case status_change: // 处理ui更新等操作 updateui(); break; } }; }; } /** * 发送消息更新ui */ private void sendmessagetoupdateui() { message msg = new message(); msg.what = status_change; mhandler.sendmessage(msg); // 向handler发送消息,更新ui } private void updateui() { // 详细的更新 mexpandableadapter.notifydatasetchanged(); // 更新expandablelistview } /** * 异步刷新分组的handler * * @author administrator * */ class updategrouphandler extends handler { public updategrouphandler() { } public updategrouphandler(looper looper) { super(looper); } @override public void handlemessage(message msg) { contactsmanagerdbadapter dbadapter = new contactsmanagerdbadapter( main.this); dbadapter.open(); // =>dosomething... mgrouparray = grouplist; system.out.println("========数据更新后,刷新listview========="); sendmessagetoupdateui(); } } private class expandableadapter extends baseexpandablelistadapter { activity activity; layoutinflater layoutinflater; public expandableadapter(activity a, context context) { activity = a; layoutinflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); } public object getchild(int groupposition, int childposition) { return mgrouparray.get(groupposition).getchildlist() .get(childposition); } public long getchildid(int groupposition, int childposition) { return childposition; } public int getchildrencount(int groupposition) { return mgrouparray.get(groupposition).getchildlist().size(); } public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { // ..... return vi; } public object getgroup(int groupposition) { return mgrouparray.get(groupposition); } public int getgroupcount() { return mgrouparray.size(); } public long getgroupid(int groupposition) { return groupposition; } public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { groupinfo groupinfo = mgrouparray.get(groupposition); string string = groupinfo.getname(); convertview = (view) layoutinflater.inflate(r.layout.group_layout, null); final textview textview = (textview) convertview .findviewbyid(r.id.groupname); if (textview != null) { textview.settext(string); } return convertview; } public boolean hasstableids() { return true; } public boolean ischildselectable(int groupposition, int childposition) { return true; } } }
代码只是提取的部分,应该没多大影响.
上面集合mgrouparray存在数据共享,测试多次发现报错有两种:
=>1.java.lang.indexoutofboundsexception: invalid location 3, size is 3
=>2.the content of the adapter has changed but listview did not receive a notification. make sure the content of your adapter is not modified from a background thread, but only from the ui thread.
第一个问题,数据同步问题,我弄了下没解决.
第二个问题,改变适配器adapter内容时不要在后台线程中,必须在ui线程中处理
我将上面子线程updategrouphandler里的数据更新利用handler提取到了主线程赋值
message.obj = grouplist;
额,改好后测试多次,发现这两问题都解决了,发现还是对handler理解的不够.
发下更改的代码段:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); melv = (expandablelistview) findviewbyid(r.id.contact_list); mstatus = (textview) findviewbyid(r.id.setstatus); mgrouparray = getintent().getparcelablearraylistextra("grouparray"); // => 取数据 mexpandableadapter = new expandableadapter(this, main.this); melv.setadapter(mexpandableadapter); // 异步对比服务器分组和本地分组 handlerthread handlerthread = new handlerthread("handler_thread"); handlerthread.start(); updategrouphandler myhandler = new updategrouphandler( handlerthread.getlooper()); message message = myhandler.obtainmessage(); message.sendtotarget(); mhandler = new handler() { public void handlemessage(message msg) { switch (msg.what) { case status_change: // 处理ui更新等操作 updateui(msg.obj); break; } }; }; } /** * 发送消息更新ui */ private void sendmessagetoupdateui(arraylist<groupinfo> grouplist) { message msg = new message(); msg.what = status_change; msg.obj = grouplist; mhandler.sendmessage(msg); // 向handler发送消息,更新ui } @suppresswarnings("unchecked") private void updateui(object obj) { // 详细的更新 mgrouparray = (arraylist<groupinfo>) obj; mexpandableadapter.notifydatasetchanged(); // 更新expandablelistview } /** * 异步刷新分组的handler * * @author administrator * */ class updategrouphandler extends handler { public updategrouphandler() { } public updategrouphandler(looper looper) { super(looper); } @override public void handlemessage(message msg) { contactsmanagerdbadapter dbadapter = new contactsmanagerdbadapter( main.this); dbadapter.open(); // =>dosomething... system.out.println("========数据更新后,刷新listview========="); sendmessagetoupdateui(grouplist); } }
希望本文所述对大家的android程序设计有所帮助。
推荐阅读
-
Android中判断listview是否滑动到顶部和底部的实现方法
-
Android数据持久化之读写SD卡中内容的方法详解
-
android中ListView数据刷新时的同步方法
-
Android开发实现ListView异步加载数据的方法详解
-
Android编程实现在Activity中操作刷新另外一个Activity数据列表的方法
-
Android对ListView 进行局部刷新的方法教程
-
Android中Json数据读取与创建的方法
-
jquery ajax多次请求数据时 不刷新问题的解决方法
-
Layui实现数据表格中鼠标悬浮图片放大效果,离开时恢复原图的方法
-
vue单页应用在页面刷新时保留状态数据的方法