0

我正在制作一个联系人备份应用程序,并希望在列表视图中显示联系人。我编写的代码正在执行此操作,但显示的信息格式不正确。我想从现在在列表视图中显示的信息中提取姓名和联系电话。我这样做的代码如下......谁能指导我如何从这里生成的联系信息中提取姓名和号码?我不需要明确的代码,只需指出我需要遵循的方向。谢谢

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();
    }


}
4

1 回答 1

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();
    }
于 2013-11-06T10:35:56.827 回答