How to do a query with Cursor inside a thread? (android) -


i started first app college, , part of app want acces contacts of phone using this guide.

in guide onactivityresult looks this:

@override protected void onactivityresult(int requestcode, int resultcode, intent data) {     // check request we're responding     if (requestcode == pick_contact_request) {         // make sure request successful         if (resultcode == result_ok) {             // uri points selected contact             uri contacturi = data.getdata();             // need number column, because there 1 row in result             string[] projection = {phone.number};              // perform query on contact number column             // don't need selection or sort order (there's 1 result given uri)             // caution: query() method should called separate thread avoid blocking             // app's ui thread. (for simplicity of sample, code doesn't that.)             // consider using cursorloader perform query.             cursor cursor = getcontentresolver()                     .query(contacturi, projection, null, null, null);             cursor.movetofirst();              // retrieve phone number number column             int column = cursor.getcolumnindex(phone.number);             string number = cursor.getstring(column);              // phone number...         }     } } 

it says should use thread or cursorloader query far unable find solution this. if place query method in thread can't acces data it:

    runnable r = new runnable() {                     @override                     public void run() {                         cursor cursor = getcontentresolver()                                 .query(contacturi, projection, null, null, null);                     }                 };                 thread querythread = new thread(r);                 querythread.start(); cursor.movetofirst(); int column = cursor.getcolumnindex(contactscontract.commondatakinds.phone.number); 

so in code android studio cannot resolve symbol "cursor" :( , far not find guide understand on how cursorloaders.

you can use asynctask job!

     class workcursor extends asynctask<cursor,object,string> {              string[] projection;             uri contacturi;              public workcursor(string[] projection,uri contacturi){                 this.contacturi = contacturi;                 this.projection = projection;             }               @override             protected string doinbackground(cursor... cursors) {                  //this done in background                  cursor cursor = myactivity.this.getcontentresolver()                         .query(contacturi, projection, null, null, null);                  int column = cursor.getcolumnindex(contactscontract.commondatakinds.phone.number);                 string number = cursor.getstring(column);                  return number;             }              @override             protected void onpostexecute(string number) {                 super.onpostexecute(number);                  //this done on ui thread                 functioncall(number);              }         }          public void functioncall(string number){             //this ui thread             //you can whatever number             toast.maketext(this,"this number: "+number,toast.length_short).show();         } 

and call asynctask so:

new workcursor(projection,contacturi).execute(); 

one other way did work inside thread , run result on ui thread so:

new thread(new runnable() {             @override             public void run() {                 cursor cursor = getcontentresolver()                         .query(contacturi, projection, null, null, null);                  cursor.movetofirst();                 int column = cursor.getcolumnindex(contactscontract.commondatakinds.phone.number);                 final string number = cursor.getstring(column);                 runonuithread(new runnable() {                     @override                     public void run() {                         functioncall(number);                     }                 });             }         }).run(); 

string number must final in order accessible inside other (runnable on ui) thread!

disclaimer: code untested.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -