类型的自定义Android的显示器接触自定义、显示器、类型、Android

2023-09-04 03:23:29 作者:思念,成了一种惯性

我已导入的联系人列表中的样本...表格此链接...的http://developer.android.com/training/contacts-provider/display-contact-badge.html 这是工作的罚款所有功能.... 现在,在列表中,如果我想只显示一些接触......我应该火的..和如何查询其中...    如果任何人有它的工作...请帮我...      在此先感谢....

I have imported an sample of Contact List...Form this link... http://developer.android.com/training/contacts-provider/display-contact-badge.html this is working fine with all functionality.... Now in list if i want to display only some of the contact...the where should i fire a query for that ..and how... if any one has work on it...please help me... Thanks in advance....

推荐答案

您也可以修改此查询亲爱

you can also modify this query dear

cntx.getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
                    null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

我保持一个接触的包装,所以我有一个数组列表的名称,所有的信息都接触,因此,现在只是让你想要显示的只是检查与原来的并显示多了一个联系人名单只有那些你想

i maintain one contact wrapper so i have all contact in one array list with name and all info so now just make one more contact name list which you want to display just check with original and display only those which you want

package com.rightbackup.parsing;

    import java.util.ArrayList;
    import java.util.HashMap;

    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.graphics.Color;
    import android.os.AsyncTask;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    import android.provider.ContactsContract.Contacts;
    import android.widget.ListView;

    import com.rightbackup.constants.Constant;
    import com.rightbackup.setcontent.ContactsAdapter;
    import com.rightbackup.wrapper.ContactsWrapper;
    import com.rightbackup.wrapper.DataController;

    public class ReadContacts extends AsyncTask<Void, Void, Void>{

        private ListView contactsList;

        private Context cntx;
        private Constant constants;

        static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
            Contacts._ID, // 0
            Contacts.DISPLAY_NAME, // 1
            Contacts.STARRED, // 2
            Contacts.TIMES_CONTACTED, // 3
            Contacts.CONTACT_PRESENCE, // 4
            Contacts.PHOTO_ID, // 5
            Contacts.LOOKUP_KEY, // 6
            Contacts.HAS_PHONE_NUMBER, // 7
        };

        private long contactId;
        private String display_name;
        private String phoneNumber;

        private ArrayList<ContactsWrapper>contactWrap = new ArrayList<ContactsWrapper>();
        private HashMap<Long, ArrayList<ContactsWrapper>>map = new HashMap<Long, ArrayList<ContactsWrapper>>();

        private ContactsAdapter adapter;

        private DataController controller;

        public ReadContacts(Context cntx, ListView contactList) {
            // TODO Auto-generated constructor stub
            this.cntx = cntx;
            constants = new Constant();
            this.contactsList = contactList;

            controller = DataController.getInstance(); 
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            if(!(controller.contactWrapper.size()>0))
                 constants.displayProgressDialog(cntx, "Loading Contacts...", "Please Wait");
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            if(!(controller.contactWrapper.size()>0))
            {
            try {
                String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                        + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                        + Contacts.DISPLAY_NAME + " != '' ))";

                Cursor c = cntx.getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
                        null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

                int colorcounter = 0;
                String[] colorcounter_array = { "#91A46B", "#8BB6B5", "#CAA973", "#8DA6C8","#D19B8D"};

                int color_string;
                for(int i=0;i<c.getCount();i++)
                {
    //              contactWrap.clear();
                    try {
                        contactId = 0;
                        String hasPhone = "";
                        display_name = "";
                        phoneNumber = "";

                        c.moveToPosition(i);

                        contactId =  c.getLong(0);
                        display_name = c.getString(1);
                        hasPhone = c.getString(7);

                        if (hasPhone.equalsIgnoreCase("1"))
                            hasPhone = "true";
                        else
                            hasPhone = "false" ;

                        if (Boolean.parseBoolean(hasPhone)) 
                        {
                            Cursor phones = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                            while (phones.moveToNext()) 
                            {
                                int indexPhoneType = phones.getColumnIndexOrThrow(Phone.TYPE);
                                String phoneType =  phones.getString(indexPhoneType);

                                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

                                String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

                                if (colorcounter < 5) 
                                {
                                    color_string =Color.parseColor(colorcounter_array[colorcounter]);
                                    colorcounter++;
                                } else {
                                    colorcounter = 0;
                                    color_string =Color.parseColor(colorcounter_array[colorcounter]);
                                    colorcounter++;
                                }
                                contactWrap.add(new ContactsWrapper(contactId, display_name, phoneNumber,lookupKey,false,color_string));
                            }
    //                      map.put(contactId, new ArrayList<ContactsWrapper>(contactWrap));
                            phones.close();
                        }
                    } catch (Exception e) {

                        e.printStackTrace();
                    }  
                }
                controller.contactWrapper = contactWrap;
            }
            catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            constants.dismissDialog();

            adapter = new ContactsAdapter(cntx);
            contactsList.setAdapter(adapter);
        }

    }

这是我接触的包装类

and this is my contact wrapper class

package com.rightbackup.wrapper;

public class ContactsWrapper {

    private long contactId;
    private String displayName,displayNumber,lookUp;
    public boolean checked = true;
    int color_string;

    public ContactsWrapper(long contactId, String displayName, String displayNumber, String lookUp, boolean checked,int color_string) {
        // TODO Auto-generated constructor stub
        this.contactId = contactId;
        this.displayName = displayName;
        this.displayNumber = displayNumber;
        this.lookUp = lookUp;
        this.checked = checked;
        this.color_string =color_string;
    }

    public String getLookUp() {
        return lookUp;
    }

    public int getColor_string() {
        return color_string;
    }

    public boolean isChecked() {
        return checked;
    }

    public long getContactId() {
        return contactId;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getDisplayNumber() {
        return displayNumber;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

}