The code in this article is followed by the previous article to get the contact information. When retrieving contact information, I found that it is slow to traverse Cursor to retrieve information for all contacts. For example, there are less than four hundred contact information on my mobile phone, it takes more than 10 seconds to traverse all data. I checked it and found no good optimization method. I want to display a progress bar on the interface instead of traversing the contact information, so that the user can see that the program is running, rather than mistakenly believing that the program is dead.
In this Code, a ProgressDialog is added based on the Code for obtaining contact information. The code is relatively simple and I will not explain it carefully. Let's look at the code below:
Package com. example. contactlist; import java. util. arrayList; import java. util. list; import android. app. listActivity; import android. app. progressDialog; import android. content. context; import android. database. cursor; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. provider. contactsContract; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. textView; public class Loading extends ListActivity {Context myContext = null; MyListAdapter myListAdapter = null; ViewHolder viewHolder = null; private ProgressDialog ssssdialog = null; // used to store the contact name List
MyContactName = new ArrayList
(); // Used to store the contact phone List
MyContactNumber = new ArrayList
(); // Used to store the total number of contacts int myContactAmount = 0; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); getContactList ();}/*** use Handler to update UI */private Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// use arg1 to determine whether the update progress is continued or whether to display the contact list switch (msg. arg1) {case 1: progressDialog. setProgress (msg. arg2); break; case 2: myListAdapter = new MyListAdapter (Loading. this); setListAdapter (myListAdapter); break ;}};/*** click the button event listener */private void getContactList () {// display ProgressDialogprogressDialog = new ProgressDialog (Loading. this); progressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); progressDialog. setTitle ("getting contact information, please wait:"); progressDialog. setIndeterminate (false); progressDialog. show (); final Cursor cursor = getContentResolver (). query (ContactsContract. contacts. CONTENT_URI, null, null); // obtain the total number of contacts int myContactAmount = cursor. getCount (); progressDialog. setMax (myContactAmount); // new Thread () {public void run () {int I = 0; while (cursor. moveToNext () {I ++; // The IDString id of the contact = cursor. getString (cursor. getColumnIndex (ContactsContract. contacts. _ ID); // contact name String name = cursor. getString (cursor. getColumnIndex (ContactsContract. contacts. DISPLAY_NAME); // contact's phone number String number = ""; // whether the contact has a phone number int isHas = Integer. parseInt (cursor. getString (cursor. getColumnIndex (ContactsContract. contacts. HAS_PHONE_NUMBER); // if a contact has a phone number, it traverses all his phone numbers if (isHas> 0) {Cursor c = getContentResolver (). query (ContactsContract. commonDataKinds. phone. CONTENT_URI, null, ContactsContract. commonDataKinds. phone. CONTACT_ID + "=" + id, null, null); while (c. moveToNext () {number + = c. getString (c. getColumnIndex (ContactsContract. commonDataKinds. phone. NUMBER) + "";} c. close ();} myContactName. add (name); myContactNumber. add (number); Message msg = new Message (); msg. arg1 = 1; msg. arg2 = I; handler. sendMessage (msg);} // After the traversal is complete, close the progress bar progressDialog. dismiss (); cursor. close (); Message msg = new Message (); msg. arg1 = 2; handler. sendMessage (msg );}}. start ();} class MyListAdapter extends BaseAdapter {public MyListAdapter (Context context) {myContext = context;} public int getCount () {// TODO Auto-generated method stubreturn myContactName. size ();} public Object getItem (int position) {// TODO Auto-generated method stubreturn position;} public long getItemId (int position) {// TODO Auto-generated method stubreturn position;} public View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubif (convertView = null) {viewHolder = new ViewHolder (); convertView = LayoutInflater. from (myContext ). inflate (R. layout. list, null); viewHolder. name = (TextView) convertView. findViewById (R. id. name); viewHolder. number = (TextView) convertView. findViewById (R. id. number); convertView. setTag (viewHolder);} else {viewHolder = (ViewHolder) convertView. getTag ();} viewHolder. name. setText (myContactName. get (position); viewHolder. number. setText (myContactNumber. get (position); return convertView;} private static class ViewHolder {TextView name; TextView number ;}}
Layout file:
List. xml
The program running result is as follows: