Android加载联系人代码优化

时间:2013-05-17 20:22:08

标签: android background-process

我写了一个活动,它会加载手机的所有联系人,并在ListView中一次显示。问题是,加载400多个联系人需要时间,这不是非常用户友好。任何人都可以指出我如何让​​这个活动在后台运行\运行得更快,我试图使用AsyncTask但显然它使用的是一个类而不是一个活动。非常感谢任何建议。

我添加了代码,以便更容易理解现在的工作原理:

这是Contacts类:

public class CustomContact 
{
    private String contactName;
    private String contactPhoneNumber;
    private boolean selected = false;

    public CustomContact() 
    {

    }

    public CustomContact(String contactName, String contactPhoneNumber, boolean selected) 
    {
        super();
        this.contactName = contactName;
        this.contactPhoneNumber = contactPhoneNumber;
        this.selected = selected;
    }

    public String getContactName()
    {
        return contactName;
    }

    public void setContactName(String contactName) 
    {
        this.contactName = contactName;
    }

    public String getContactPhoneNumber() 
    {
        return contactPhoneNumber;
    }

    public void setContactPhoneNumber(String contactPhoneNumber) 
    {
        this.contactPhoneNumber = contactPhoneNumber;
    }

    public boolean isSelected() 
    {
        return selected;
    }

    public void setSelected(boolean selected) 
    {
        this.selected = selected;
    }

    @Override
    public String toString() 
    {
        return contactPhoneNumber + " " + "\u200e" + contactName;
    } 
}

现在使用CustomContacts类的活动代码:

public class ContactSelectActivity extends Activity 
{
          MyCustomAdapter dataAdapter = null;
            public void onCreate(Bundle savedInstanceState)
            {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_contact_select);
               //Generate list View from ArrayList
               displayListView();

               checkButtonClick();

            }

            private void displayListView()
            {

                    //Array list of countries
                    ArrayList<CustomContact> contactsList = new ArrayList<CustomContact>();


                    String[] projection = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
                                                        ContactsContract.Contacts.HAS_PHONE_NUMBER,
                                                        ContactsContract.Contacts.LOOKUP_KEY
                                                        };  
                    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME ;
                    ContentResolver contentResolver = getContentResolver();
                    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, projection,
                                                          ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"+ ("1") + "'", 
                                                          null, sortOrder
                                                         );
                    if (cursor.getCount() > 0) 
                    {
                        while (cursor.moveToNext()) 
                        {
                            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                            {
                        // Query phone here. Covered next
                                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 
                                while (phones.moveToNext()) 
                                { 

                                    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    CustomContact customContact = new CustomContact(name, phoneNumber, false);
                                    contactsList.add(customContact);
                                }

                                phones.close();
                            }
                        }
                    }
                    //create an ArrayAdaptar from the String Array
                    dataAdapter = new MyCustomAdapter(this,R.layout.custom_contact_info, contactsList);
                    ListView listView = (ListView) findViewById(R.id.listView1);
                    // Assign adapter to ListView
                    listView.setAdapter(dataAdapter);

                    listView.setOnItemClickListener(new OnItemClickListener() 
                    {

                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                            {
                                    CustomContact contact = (CustomContact) parent.getItemAtPosition(position);
                            }
                    });
            }

            private class MyCustomAdapter extends ArrayAdapter<CustomContact>
            {

                private ArrayList<CustomContact> contactList;

                public MyCustomAdapter(Context context, int textViewResourceId, 

                ArrayList<CustomContact> stateList) 
                {
                    super(context, textViewResourceId, stateList);
                    this.contactList = new ArrayList<CustomContact>();
                    this.contactList.addAll(stateList);
                }

            private class ViewHolder
            {
              TextView code;
              CheckBox name;
            }

        @Override
       public View getView(int position, View convertView, ViewGroup parent) 
        {
            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null)
            {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = vi.inflate(R.layout.custom_contact_info, null);
                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
                convertView.setTag(holder);
                holder.name.setOnClickListener( new View.OnClickListener() 
                {
                    public void onClick(View v)  
                    {
                        CheckBox cb = (CheckBox) v;
                        CustomContact _contact = (CustomContact) cb.getTag();
                        _contact.setSelected(cb.isChecked());
                    }
                });

            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            CustomContact contact = contactList.get(position);
            holder.code.setText(" (" + contact.getContactName() + ")");
            holder.name.setText(contact.getContactPhoneNumber());
            holder.name.setChecked(contact.isSelected());
            holder.name.setTag(contact);

            return convertView;
        }
            }

        private void checkButtonClick() 
        {
            Button myButton = (Button) findViewById(R.id.findSelected);

            myButton.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    StringBuffer responseText = new StringBuffer();
                    responseText.append("The following were selected...\n");
                    ArrayList<CustomContact> contactList = dataAdapter.contactList;

                                            for(int i=0;i<contactList.size();i++)
                                            {
                                                 CustomContact contact = contactList.get(i);

                                                if(contact.isSelected())
                                                {
                                                    responseText.append("\n" + contact.getContactPhoneNumber());
                                                }
                                            }
                            }
                });
        }
}

0 个答案:

没有答案