在模拟器上显示的联系人不在真实设备上

时间:2014-02-21 07:37:39

标签: java android android-contacts android-contentresolver

模拟器联系人显示在列表视图上但不在真实设备上。任何人,帮助我 我需要将我的Android手机的联系人显示在列表视图上。

这是我的活动代码

public class MainActivity extends Activity {

    ListView listView;
    CustomAdapter customAdapter;
    ArrayList<String> nameList = new ArrayList<String>();
    ArrayList<String> numberList = new ArrayList<String>();

    ArrayList<String> selectedNameList = new ArrayList<String>();
    ArrayList<String> selectedNumberList = new ArrayList<String>();
    private LinkedHashMap<String, Contact> allContacts = new LinkedHashMap<String, Contact>();
    private static ArrayList<Contact> contacts = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView =(ListView) findViewById(R.id.list);
        customAdapter = new CustomAdapter();
        new ContactsBackground().execute();
    }

     private class ContactsBackground extends AsyncTask<String ,Integer,Void>{

            ProgressDialog dialog;
            @Override
            protected void onPreExecute() {
            // TODO Auto-generated method stub
            dialog = ProgressDialog.show(MainActivity.this, "", "Loading . . .", true);
            }
            @Override
        protected Void doInBackground(String... params) {
                System.out.println("=======one========");


                contacts = new ArrayList<Contact>();
                // Obtain contacts
                getContacts();


            return null;
        }
            @Override
            protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
                dialog.dismiss();
                customAdapter = new CustomAdapter();
                listView.setAdapter(customAdapter);
                customAdapter.notifyDataSetChanged();
            }



     }

        @SuppressLint("InlinedApi")
        private void getContacts() {

            ContentResolver cr = getContentResolver();

            Cursor cur = cr.query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID, Data.MIMETYPE, Email.ADDRESS,
                    Contacts.DISPLAY_NAME, Phone.NUMBER,Contacts.Photo.PHOTO }, null, null, Contacts.DISPLAY_NAME);

            Contact contact;

            if (cur.getCount() > 0) {

                while (cur.moveToNext()) {
                    System.out.println("heyyyyyyyyy"+cur.getCount());

                    String id = cur.getString(cur.getColumnIndex(Data.CONTACT_ID));
                    System.out.println("idddddddddd"+id);

                    String mimeType = cur.getString(cur.getColumnIndex(Data.MIMETYPE));

                    if (allContacts.containsKey(id)) {
                        // update contact
                        contact = allContacts.get(id);
                    } else {
                        contact = new Contact();
                        allContacts.put(id, contact);
                        // set photoUri
                        //contact.setContactPhotoUri(getContactPhotoUri(Long.parseLong(id)));
                    }

                    if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE))
                        // set name
                        contact.setContactName(cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME)));

                    if (mimeType.equals(Phone.CONTENT_ITEM_TYPE))
                        // set phone munber
                        contact.setContactNumber(cur.getString(cur.getColumnIndex(Phone.NUMBER)));

                    if (mimeType.equals(Email.CONTENT_ITEM_TYPE))
                        // set email
                        contact.setContactEmail(cur.getString(cur.getColumnIndex(Email.ADDRESS)));

                }
            }

            cur.close();
            // get contacts from hashmap
            contacts.clear();
            contacts.addAll(allContacts.values());

            // remove slef contact
            for (Contact _contact : contacts) {

                if (!(_contact.getContactNumber() == null))
                {
                    nameList.add(_contact.getContactName());
                    numberList.add(_contact.getContactNumber());
                }
                if (_contact.getContactName() == null && _contact.getContactNumber() == null
                        && _contact.getContactEmail() == null) {
                    contacts.remove(_contact);
                    break;


                }



            }

        }


         static class ViewHolder{

                protected TextView userNameText;

                protected TextView numberText;
                protected CheckBox check;

            }
        public class CustomAdapter extends BaseAdapter{

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return nameList.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return nameList.get(position);
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                ViewHolder holder;
                holder=new ViewHolder();
                LayoutInflater inflater =  MainActivity.this.getLayoutInflater();
                convertView =inflater.inflate(R.layout.contact_list, null);
                holder.userNameText =  (TextView) convertView.findViewById(R.id.nameID);
                holder.numberText =  (TextView) convertView.findViewById(R.id.numberID);
                holder.check =  (CheckBox) convertView.findViewById(R.id.checkboxID);

                    holder.userNameText.setText(" "+nameList.get(position).toString());
                    holder.numberText.setText(" "+numberList.get(position).toString());



                    for(int i=0;i<selectedNumberList.size();i++){
                        if(numberList.get(position).toString().equals(selectedNumberList.get(i).toString())){
                            holder.check.setChecked(true);
                        }
                        }
                     holder.check.setOnCheckedChangeListener(new CheckedChangeListner());
                     convertView.setTag(holder);

                     holder.check.setTag(position);

                    return convertView;
            }
        }

            class CheckedChangeListner implements OnCheckedChangeListener{

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                int pos = (Integer) buttonView.getTag();
                if(isChecked){

                    selectedNumberList.add(numberList.get(pos).toString());
                    selectedNameList.add(nameList.get(pos).toString()); 
                    System.out.println("======"+nameList.get(pos).toString()+" added");
                }
                else
                {

                    selectedNumberList.remove(numberList.get(pos).toString());
                    selectedNameList.remove(nameList.get(pos).toString());

                    System.out.println("======"+nameList.get(pos).toString()+" removed");
                }
            }
        }


}

0 个答案:

没有答案