联系Picker Code

时间:2014-01-10 21:25:03

标签: list contacts android-contacts

我需要一些有关联系人选择器类的帮助。 该类检索联系人列表并允许我选择一个,但是当我去选择另一个时,它只是替换第一个。 我想在我的应用中制作一个联系人列表,而不仅仅是一个。

谢谢你, 诺姆

守则:

public static final int PICK_CONTACT = 1;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
    btnPickContact.setOnClickListener(new OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case (PICK_CONTACT): {
        if (resultCode == Activity.RESULT_OK) {
            Uri contentUri = data.getData();
            //Phone Name
            Cursor c = managedQuery(contentUri, null, null, null, null);
            c.moveToFirst();
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            //Phone Number
            String contactId = contentUri.getLastPathSegment();
            Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                    null, Phone._ID + "=?", new String[] { contactId },
                    null);// < - Note, not CONTACT_ID!
            startManagingCursor(cursor);
            Boolean numbersExist = cursor.moveToFirst();
            int phoneNumberColumnIndex = cursor
                    .getColumnIndex(Phone.NUMBER);
            String phoneNumber = "";
            while (numbersExist) {
                phoneNumber = cursor.getString(phoneNumberColumnIndex);
                phoneNumber = phoneNumber.trim();
                numbersExist = cursor.moveToNext();
            }
            stopManagingCursor(cursor);
            //Set
            TextView tv = (TextView) findViewById(R.id.txtSelContact);
            tv.setText(name + "-" + phoneNumber);
        }
        break;
    }
    }
}

}

这是on create function:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
    btnPickContact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

    //you may fill it here e.g. from your db
    contactList=new ArrayList<String>();

    arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, contactList);

    final ListView lv = (ListView) findViewById(R.id.ContactListView);
    lv.setAdapter(arrayAdapter);

}

这是layout.xml(出于某种原因,它不允许我发布代码,所以我链接到图像):

https://imagizer.imageshack.us/v2/516x255q90/4/igi5.png

第29-35行:

btnPickContact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

1 个答案:

答案 0 :(得分:0)

据我所知,您正在将结果写入同一文本视图中:

//Set
TextView tv = (TextView) findViewById(R.id.txtSelContact);
tv.setText(name + "-" + phoneNumber);

您可以在布局中指定Listview

<ListView
    android:id="@+id/ContactListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

并使用arrayadapter将结果添加到此Listview:

public class MainActivity extends Activity {
    public static final int PICK_CONTACT = 1;
    private ArrayList<String> contactList;
    private ArrayAdapter<String> arrayAdapter;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);

        Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
        btnPickContact.setOnClickListener(new View.OnClickListener() {
            public void onClick(View _view) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(intent, PICK_CONTACT);
            }
        });

        //you may fill it here e.g. from your db
        contactList=new ArrayList<String>();

        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, contactList);

        final ListView lv = (ListView) findViewById(R.id.contactListView);
        lv.setAdapter(arrayAdapter);

    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case (PICK_CONTACT): {
            if (resultCode == Activity.RESULT_OK) {
                Uri contentUri = data.getData();
                //Phone Name
                Cursor c = managedQuery(contentUri, null, null, null, null);
                c.moveToFirst();
                String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                //Phone Number
                String contactId = contentUri.getLastPathSegment();
                Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                        null, Phone._ID + "=?", new String[] { contactId },
                        null);// < - Note, not CONTACT_ID!
                startManagingCursor(cursor);
                Boolean numbersExist = cursor.moveToFirst();
                int phoneNumberColumnIndex = cursor
                        .getColumnIndex(Phone.NUMBER);
                String phoneNumber = "";
                while (numbersExist) {
                    phoneNumber = cursor.getString(phoneNumberColumnIndex);
                    phoneNumber = phoneNumber.trim();
                    numbersExist = cursor.moveToNext();
                }
                stopManagingCursor(cursor);
                //Set
                arrayAdapter.add(name + "-" + phoneNumber);
                arrayAdapter.notifyDataSetChanged();
            }
            break;
        }
        }
    }
}