android - 无法使用同步的 Gmail 帐户获取联系人

标签 android listview contacts

我正在开发一个应用程序,我需要从通讯录中获取所有联系人并显示。我希望用户选择一些联系人并将它们添加到保存在数据库中的组中。

我已经创建了一个自定义 ListView - contactitem.xml-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">


 <TextView

    android:id="@+id/contactname"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:layout_marginLeft="20dp"
    android:ellipsize="end"        
   android:singleLine="true"
    android:clickable="true"/>

 <TextView
    android:id="@+id/contactnum"
    android:layout_width="wrap_content"
     android:textColor="@color/White"
     android:clickable="true"
     android:layout_gravity="center_vertical"
     android:layout_height="wrap_content"/>

  <Button
    android:id="@+id/add"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:text="@string/add_contacts"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

</LinearLayout>

我有一个用于从通讯录中获取联系人的 SelectContact 类-

   public class SelectContacts extends Activity implements OnClickListener{

    private String groupName;
    private Button back;
    private Button home;
    private List<Contact> list = new ArrayList<Contact>();

    private ListView contactList;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.selectcontacts);

            Bundle bundle = getIntent().getExtras();
            groupName=bundle.getString("groupName");

            back=(Button)findViewById(R.id.back_button);
            back.setOnClickListener(this);

            home=(Button)findViewById(R.id.home_button);
            home.setOnClickListener(this);

            contactList=(ListView)findViewById(R.id.contactsListView);

            ContentResolver cr = getContentResolver();



            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

            cur.moveToFirst();

            if (cur.getCount() > 0) {

                do{

                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

                    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                    String[] fields={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};    

                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, fields, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);

                    pCur.moveToFirst();

                     do { 

                        String number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        Contact c=new Contact(name,number);

                        list.add(c);

                        }while (pCur.moveToNext());

                        pCur.close();
                    }               



                   }while (cur.moveToNext());

                ContactAdapter contactAdapter=new ContactAdapter(this, R.layout.contactitem, list, contactList,groupName);



                contactList.setAdapter(contactAdapter);


            }


        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent= new Intent();

            if(v==back){

                Bundle bundle = new Bundle();
                bundle.putString("groupName", groupName);
                intent.putExtras(bundle);

                intent.setClass(SelectContacts.this, GroupDetails.class);
                startActivity(intent);

            }
            if(v==home){
                intent.setClass(SelectContacts.this, Welcome.class);
                startActivity(intent);

            }

        }
}

并实现了一个自定义适配器 - ContactAdapter-`

public class ContactAdapter extends ArrayAdapter<HashMap<String, Contact>>{

private ArrayList<HashMap<String, Contact>> items;

private LayoutInflater mInflater ; 

public ContactAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Contact>> items) {

        super(context, textViewResourceId, items);

        this.items = items;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

            mInflater = LayoutInflater.from(getContext());

            ViewHolder holder;

       if(convertView==null){

            convertView = mInflater.inflate(R.layout.contactitem, parent, false);

            holder = new ViewHolder();

            holder.name = (TextView) convertView.findViewById(R.id.contactname);

            holder.number = (TextView) convertView.findViewById(R.id.contactnum);

            holder.add=(Button)convertView.findViewById(R.id.add);

            convertView.setTag(holder);

        }

       else{

            holder=(ViewHolder)convertView.getTag();

        }

       String name=items.get(position).get(""+position).getContactName();

       String number=items.get(position).get(""+position).getContactNum();

       holder.name.setText(name);

       holder.number.setText(number);

        return convertView;
}



static class ViewHolder{

    TextView name;

    TextView number;

    Button add;

}

here Contact is a simple POJO-



public class Contact {
    private String contactName;
    private String contactNum;

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

    public String getContactNum() {
        return contactNum;
    }
    public void setContactNum(String contactNum) {
        this.contactNum = contactName;
    }

}

我是安卓新手..

以上代码在模拟器上运行良好,并获取联系人姓名和号码以显示在列表中。但是当我在同步了 Gmail 帐户的手机上测试此代码时,它因以下错误而崩溃..

CursorIndexOutOfBoundsException:请求索引 0,大小为 0

我不知道为什么会这样。请给些建议...

最佳答案

以上代码提供给您的是 android 联系人数据,而不是 gmail 或任何其他联系人,因为您的手机与您的 gmail 联系人或任何其他联系人同步,仅允许手机联系人,而不是所有 gmail 或 skype 联系人。

关于android - 无法使用同步的 Gmail 帐户获取联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8799154/

相关文章:

android - 如何将 SearchView 的搜索图标移动到右侧?

java - 字符串太大,无法使用 UTF-8 进行编码,而改为 'STRING_TOO_LARGE'

Android ListView 项目以编程方式突出显示

Android 通讯录不显示我的自定义 account_type

php - 联系表单脚本未运行,白页

python - 将联系人添加到 Google SystemGroup

java - 字符串已定义但未解析?

android - 如何设置饼图的标签颜色?

listview - 如何隐藏 ListView 中的向右箭头?

android - android中如何以表格形式显示ListView