Android联系人显示在列表视图中

时间:2013-11-06 10:14:35

标签: android

我正在制作联系人备份应用,并希望在列表视图中显示联系人。我写的代码是这样做的,但显示的信息格式不正确。我想从列表视图中现在显示的信息中提取名称和联系号码。我这样做的代码如下....任何人都可以请指导我如何从这里生成的联系信息中提取姓名和号码?我不需要明确的代码,只需指出我需要遵循的方向。感谢

public class ContactsBackupMain extends Activity {

Cursor cursor;
ArrayList<String> vCard;
//String vfile;
File vfile;
Button btnCreateBackup;
static Context mContext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_backup_main);
    mContext = ContactsBackupMain.this;
    // vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";

     btnCreateBackup = (Button)findViewById(R.id.btnCreateBackup);

     btnCreateBackup.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

             getContactsBackup();

             Intent intent = new Intent(ContactsBackupMain.this, DisplayListItems.class);
             intent.putExtra("vCard", vCard);
             startActivity(intent);

        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.contacts_backup_main, menu);
    return true;
}

private void getContactsBackup() {

    String path1 = Environment.getExternalStorageDirectory()
            .getPath() + "/external_sd/";

    File dir = new File(path1);
        if(!dir.exists())
            dir.mkdirs();

         vfile = new File(dir, "Contacts.vcf");
         vfile.canWrite();


    vCard = new ArrayList<String>();
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    if(cursor!=null && cursor.getCount()>0)
    {
        cursor.moveToFirst();


        for(int i=0; i<cursor.getCount(); i++)
        {

            get(cursor);

            Toast.makeText(getApplicationContext(), "Contact  " + (i+1) + "VCF String is " + vCard.get(i), Toast.LENGTH_LONG).show();
            cursor.moveToNext();
        }

    }else{

        Toast.makeText(getApplicationContext(), "No Contacts in Your Phone", Toast.LENGTH_LONG).show();
    }


}

1 个答案:

答案 0 :(得分:0)

我的建议是提取联系人并将其保存在数据库中,然后显示到列表视图中..使用以下代码提取联系人

        try {
        Cursor cursor = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null,
                Phone.DISPLAY_NAME + " ASC");
        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String hasPhone = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            String name = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if ("1".equals(hasPhone) || Boolean.parseBoolean(hasPhone)) {
                // You know it has a number so now query it like this
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    String dname = cursor
                            .getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    System.out.println("Name is " + dname);
                    String phoneNumber = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    System.out.println("Phone Number is " + phoneNumber);
                    int itype = phones
                            .getInt(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                    final boolean isMobile = itype == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
                            || itype == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;

                    dbaccess.addContacts(name, phoneNumber);
                }
                phones.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }