Synchronization method of ListView data Refresh in Android

Source: Internet
Author: User
Tags final

This example describes the synchronization method for ListView data refreshes in Android. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 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");//=> fetch data Mexpandableadapter = new Expandableadapter (this, main.this); Melv.setadapter (Mexpandableadapter); Asynchronous contrast server grouping and local grouping 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 (msg) {switch (msg.what) {case Status_change://Handling UI Update operations U Pdateui (); Break } }; }; /** * Send messages Update UI/private void Sendmessagetoupdateui () {msg = new message (); msg.what = Status_change; mhandler . SendMessage (msg); Send messages to handler, update UI} private void UpdateUI () {//Detailed update mexpandableadapter.notifydatasetchanged ();//Update Expandablelistv Iew}/** * Asynchronous Refresh grouping Handler * * @author Administrator * */class Updategrouphandler extends Handler {public Updategrouphan Dler () {} public Updategrouphandler (Looper looper) {super (looper);} @Override public void Handlemessage (msg) { Contactsmanagerdbadapter dbadapter = new Contactsmanagerdbadapter (main.this); Dbadapter.open (); =>dosomething ... mgrouparray = grouplist; System.out.println ("Refresh listview========= after ======== data Update"); Sendmessagetoupdateui (); } private class Expandableadapter extends Baseexpandablelistadapter {activity activity; Layoutinflater Layoutinflater; Expandableadapter (activity A, context) {activity = A; Layoutinflater = (layoutinflater) context. Getsystem ServiCE (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 g Roupposition) {return mgrouparray.get (groupposition). Getchildlist (). Size (), public View getchildview (int groupposition, int childposition, Boolean islastchild, View Convertview, ViewGroup parent) {//... Object getgroup (int groupposition) {return mgrouparray.get (groupposition);} public int getgroupcount () {return mgroupar Ray.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; } } }

Code is only part of the extraction, it should not have much effect.

The above set Mgrouparray exist data sharing, the test repeatedly found that there are two kinds of errors:

=>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 isn't modified from a background thread, and but only from the UI thread.

The first question, the data synchronization problem, I got out of the next unresolved.

The second issue, when changing the adapter adapter content, is not in the background thread and must be handled in the UI thread

I extracted the data from the upper thread updategrouphandler using handler to extract the main thread assignment

?

1 Message.obj = grouplist;

Well, after testing many times, found that both problems have been resolved, and found that handler not enough to understand.

To send a changed code snippet:

?

@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"); => Fetch Data Mexpandableadapter = new Expandableadapter (this, main.this); Melv.setadapter (Mexpandableadapter); Asynchronous contrast server grouping and local grouping 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://Process UI Update operations UpdateUI (msg.obj); break;}}; }; /** * Send messages Update UI/private void Sendmessagetoupdateui (arraylist<groupinfo> grouplist) {msg = new message () ; Msg.what = Status_change; Msg.obj = grouplist; Mhandler.sendmessage (msg); Send a message to handler, update UI} @SuppressWarnings ("Unchecked") private void UpdateUI (Object obj) {//verbose update Mgrouparray = (arraylis t<groupinfo>) obj; Mexpandableadapter.notifydatasetchanged (); Update Expandablelistview}/** * Asynchronous Refresh Group Handler * * @author Administrator * */class Updategrouphandler extends Handler {p Ublic Updategrouphandler () {} public Updategrouphandler (Looper looper) {super (looper);} @Override public void Handlemes Sage (Message msg) {Contactsmanagerdbadapter dbadapter = new Contactsmanagerdbadapter (main.this); Dbadapter.open (); >dosomething ... System.out.println ("Refresh listview========= after ======== data Update"); Sendmessagetoupdateui (grouplist); } }

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.