android asynctask - looking for a way to speed up my loading of my listview -
the layout of activity want apart fact loads : buttons, text views , listview set within scrollview, whole activity scrolls 1 (rather having listview scroll separately think bad design.)
the listview consists of phone contacts, have function, justifylistviewheightbasedonchildren
, determines number of contacts in user's phone , hence length of list show.
the problem is, activity takes few seconds load - because function determining length of list. tried asynctask
putting function in background thread won't work list calculations on has in ui thread. ideas how can may list load faster?
here's code :
//******for phone contacts in listview // load data in background class loadcontact extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... voids) { // perhaps running thread on ui thread has solved issue of app // crashing? listview had not been updating properly, think. // runonuithread(new runnable() { // public void run() { // want delete old selectcontacts listview when activity loads // because may need updated , want user see updated listview, // if user adds new names , numbers phone contacts. selectphonecontacts.clear(); // have here avoid cursor errors if (cursor != null) { cursor.movetofirst(); } try { // handle on content resolver, can query provider, cursor = getapplicationcontext().getcontentresolver() // table query .query(contactscontract.commondatakinds.phone.content_uri, null, null, null, contactscontract.commondatakinds.phone.display_name + " collate localized asc"); int nameidx = cursor.getcolumnindex(contactscontract.commondatakinds.phone.display_name); // column number of number column int phonenumberofcontactidx = cursor.getcolumnindex(contactscontract.commondatakinds.phone.number); cursor.movetofirst(); { system.out.println("=====>in while"); // handle on phone number of contact, string. loop through phone numbers string phonenumberofcontact = cursor.getstring(phonenumberofcontactidx); // handle on display name, string name = cursor.getstring(nameidx); system.out.println(" newcontact.java name--->" + name); system.out.println(" newcontact.java phone number of contact--->" + phonenumberofcontact); //system.out.println("id--->" + contactid + " lookupkey--->" + lookupkey); selectphonecontact selectcontact = new selectphonecontact(); selectcontact.setname(name); //selectcontact.setphone(phonenumber); //selectcontact.setlookup(lookupkey); // selectcontact.setcheckedbox(false); selectphonecontacts.add(selectcontact); } while (cursor.movetonext()); } catch (exception e) { toast.maketext(newcontact.this, "what the...", toast.length_long).show(); e.printstacktrace(); } { } if (cursor != null) { cursor.close(); } return null; } @override protected void onpostexecute(void avoid) { //show result obtained doinbackground super.onpostexecute(avoid); //into each inflate_listview, put name , phone number, details making // our selectcontact, above. , selectcontacts these inflate_listviews // first property of our selectcontactadapter, list // next part, newcontact.this, our context, want list appear adapter = new selectphonecontactadapter(selectphonecontacts, newcontact.this); // need notify listview changes may have been made on // background thread, doinbackground, adding or deleting contacts, // , these changes need reflected visibly in listview. works // in conjunction selectcontacts.clear() adapter.notifydatasetchanged(); listview.setadapter(adapter); //this function measures height of listview, contacts, , loads //size. need because there's problem listview in scrollview. justifylistviewheightbasedonchildren(listview); // select item on listclick listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { // need notify listview changes may have been made on // background thread, doinbackground, adding or deleting contacts, // , these changes need reflected visibly in listview. works // in conjunction selectcontacts.clear() // adapter.notifydatasetchanged(); } }); }} @override protected void onresume() { super.onresume(); // load contacts again, refresh them, when user resumes activity loadcontact loadcontact = new loadcontact(); loadcontact.execute(); // cursor.close(); } //this function call measure height of listview //we need because there problems listview within scrollview public static void justifylistviewheightbasedonchildren (listview listview) { listadapter adapter = listview.getadapter(); if (adapter == null) { return; } viewgroup vg = listview; int totalheight = 0; (int = 0; < adapter.getcount(); i++) { view listitem = adapter.getview(i, null, vg); listitem.measure(0, 0); totalheight += listitem.getmeasuredheight(); } viewgroup.layoutparams par = listview.getlayoutparams(); par.height = totalheight + (listview.getdividerheight() * (adapter.getcount() - 1)); listview.setlayoutparams(par); listview.requestlayout(); system.out.println("the getcount " + adapter.getcount()); } }
Comments
Post a Comment