简体   繁体   English

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

[英]Android Contacts display in list view

i am making a contacts backup app and want to display the contacts in a list view. 我正在制作联系人备份应用,并希望在列表视图中显示联系人。 The code that i have written is doing this, but the format of the info displayed is not right. 我编写的代码正在执行此操作,但是显示的信息格式不正确。 I want to extract just the name and contact number from the info being displayed now in the list view. 我只想从现在在列表视图中显示的信息中提取姓名和联系电话。 My code for doing this is as follows....Can anyone please just guide me how to extract the name and number from the contact info that is being generated here? 我执行此操作的代码如下...。有人可以指导我如何从此处生成的联系信息中提取姓名和电话号码吗? i do not need the definite code, just point me to the direction i need to follow. 我不需要确定的代码,只需将我指向需要遵循的方向即可。 Thanks 谢谢

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


}

My suggestion is to extract the contacts and save it in a database and then display to your listview.. use the below code to extract the contacts 我的建议是提取联系人并将其保存在数据库中,然后显示到您的列表视图中。使用以下代码提取联系人

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM