android中ListView数据刷新时的同步方法
程序员文章站
2022-05-27 10:05:51
本文实例讲述了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程序设计有所帮助。
上一篇: PS给瓷器加上龟裂的纹理效果
推荐阅读
-
Android中Json数据读取与创建的方法
-
Android笔记之:在ScrollView中嵌套ListView的方法
-
vue单页应用在页面刷新时保留状态数据的方法
-
Android中ListView下拉刷新的实现方法实例分析
-
Android中ListView的item点击没有反应的解决方法
-
android中ListView多次刷新重复执行getView的解决方法
-
C#实现读取DataSet数据并显示在ListView控件中的方法
-
Android中判断listview是否滑动到顶部和底部的实现方法
-
Android数据持久化之读写SD卡中内容的方法详解
-
android中ListView数据刷新时的同步方法