0

Hi I am developing an android SMS app. I hav an activity which loads the contacts into mPeopleList ArrayList as in below code which is called in doInBackground

public void PopulatePeopleList()
{
    mPeopleList.clear();

    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    if(people.getCount() == 0)
    {
        //No contacts found
    }

    while (people.moveToNext())
    {
        String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if ((Integer.parseInt(hasPhone) > 0))
        {

            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
            while (phones.moveToNext())
            {

                //store numbers and display a dialog letting the user select which.
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String numberType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                Map<String, String> NamePhoneType = new HashMap<String, String>();

                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);

                if(numberType.equals("0"))
                {
                    NamePhoneType.put("Type", "Work");
                }
                else if(numberType.equals("1"))
                {
                    NamePhoneType.put("Type", "Home");
                }
                else if(numberType.equals("2"))
                {
                    NamePhoneType.put("Type",  "Mobile");
                }
                else
                {
                    NamePhoneType.put("Type", "Other");
                }
                //Then add this map to the list.
                mPeopleList.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();

    startManagingCursor(people);
}

and on PostExecute setting it to adapter

mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview ,new String[] { "Name", "Phone" , "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType });
mTxtPhoneNo.setAdapter(mAdapter);

I am calling the MyBackgroundTask() onCreate().

The Second activity is loading very slowly. Not sure why there is this slow loading.

Please Help how do I load the activity faster.

Thanks!

sanjana
  • 641
  • 2
  • 15
  • 36
  • Perhaps you can explain more about what was *loading very slowly*. Was it showing black screen for a while before loading the data? Or was the data loaded very slowly? In any case, it's better to also include the code related to the creation of second activity (e.g. `onCreate()`, ...) – Andrew T. Feb 11 '14 at 05:42
  • Yes it shows black screen for some time. I am calling the MyBackgroundTask() onCreate(). – sanjana Feb 11 '14 at 06:18
  • If it's showing black screen, then the layout creation (Main/UI thread) was blocked by some heavy processes. Is your `MyBackgroundTask` the one that fills the adapter, or a different task? If possible, post your full code for `onCreate()` on second activity. – Andrew T. Feb 11 '14 at 06:40
  • Are you using Android Studio? – AA Shakil May 26 '18 at 07:46

1 Answers1

0

Disk operations are always "slow" by comparison. You really should not be making database queries on the main thread. Learn to use Loaders instead; there is no longer any need to make direct queries and use startManagingCursor (it's deprecated anyway). Rather than iterating the cursor and closing it, you can use a CursorAdapter instead.

If you still need to iterate the cursor, at least do it this way:

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
    // etc
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104