我正在尝试从电话簿中获取联系人并将该号码放入editText。我想在同一活动中执行4次此操作。因此,每个editText都有其自己的添加按钮。我创建了一个onActivityresult来执行此操作,但是它不起作用。有人可以帮我提供这个代码吗?
Intent contactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent intent)
{
super.onActivityResult(reqCode, resultCode, intent);
if (resultCode == Activity.RESULT_OK)
{
Uri contactData1 = intent.getData();
Cursor c1 = getContentResolver().query(contactData1, null, null, null, null);
c1.moveToFirst();
String cNumber1 = c1.getString(c1.getColumnIndex("data1"));
TextView tv1;
tv1 = (TextView)findViewById(R.id.number1);
tv1.setText(cNumber1);
}
if (resultCode == Activity.RESULT_OK)
{
Uri contactData2 = intent.getData();
Cursor c2 = getContentResolver().query(contactData2, null, null, null, null);
c2.moveToFirst();
String cNumber2 = c2.getString(c2.getColumnIndex("data1"));
TextView tv2;
tv2 = (TextView)findViewById(R.id.number2);
tv2.setText(cNumber2);
}
if (resultCode == Activity.RESULT_OK)
{
Uri contactData3 = intent.getData();
Cursor c3 = getContentResolver().query(contactData3, null, null, null, null);
c3.moveToFirst();
String cNumber3 = c3.getString(c3.getColumnIndex("data1"));
TextView tv3;
tv3 = (TextView)findViewById(R.id.number3);
tv3.setText(cNumber3);
}
if (resultCode == Activity.RESULT_OK)
{
Uri contactData4 = intent.getData();
Cursor c4 = getContentResolver().query(contactData4, null, null, null, null);
c4.moveToFirst();
String cNumber = c4.getString(c4.getColumnIndex("data1"));
TextView tv4;
tv4 = (TextView)findViewById(R.id.number4);
tv4.setText(cNumber);
}
}
到底什么不起作用?
而且您正在做的事情是您在此处可以清楚看到的4倍:
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = intent.getData();
Cursor cursor = getContentResolver().query(contactData1, null, null, null, null);
cursor.moveToFirst();
String contactNumber = cursor.getString(cursor.getColumnIndex("data1"));
TextView tv1 = (TextView)findViewById(R.id.number1);
tv1.setText(contactNumber);
TextView tv2 = (TextView)findViewById(R.id.number2);
tv2.setText(contactNumber);
TextView tv3 = (TextView)findViewById(R.id.number3);
tv3.setText(contactNumber);
TextView tv4 = (TextView)findViewById(R.id.number4);
tv4.setText(contactNumber);
}
我只是把它缩短了。实际上是相同的代码。这样,您将在每个TextView
中使用相同的电话号码。
编辑:
这可能是一个解决方案。
为了区分几个TextView,您应该声明一些final request constants
public static final int CONTACT_PICKER_REQUEST_FIRST = 0, CONTACT_PICKER_REQUEST_SECOND = 1, CONTACT_PICKER_REQUEST_THIRD = 2, CONTACT_PICKER_REQUEST_FOURTH = 3;
在按钮的onClick方法中放置如下内容:
public void onClick(View v) {
int request_code = -1;
switch(v.getId()) {
case R.id.number1:
request_code= CONTACT_PICKER_RESULT_FIRST;
break;
case R.id.number2:
request_code= CONTACT_PICKER_RESULT_SECOND;
break;
case R.id.number3:
request_code= CONTACT_PICKER_RESULT_THIRD;
break;
case R.id.number4:
request_code= CONTACT_PICKER_RESULT_FOURTH;
break;
}
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_UR);
startActivityForResult(contactPickerIntent, request_code);
}
最后在您的onActivityResult()
中,这样的方法应该起作用:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
TextView toFill;
switch (requestCode) {
case CONTACT_PICKER_REQUEST_SECOND:
toFill = (TextView) findViewById(R.id.number2);
break;
case CONTACT_PICKER_REQUEST_THIRD:
toFill = (TextView) findViewById(R.id.number3);
break;
case CONTACT_PICKER_REQUEST_FOURTH:
toFill = (TextView) findViewById(R.id.number4);
break;
default:
toFill = (TextView) findViewById(R.id.number1);
break;
}
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
toFill.setText(phoneNumber);
}
}
我在此处而不是在我的ADT中写下的内容可能有一些错别字。
只是一个更新,@ Endzeit给了我很大的帮助。在我的情况下,我要填写4个EditText。每个EditText都有与之关联的自己的getContact按钮。因此,我决定与那些遇到这种情况的人分享我的工作代码:
protected void onActivityResult(int reqCode, int resultCode, Intent intent)
{
super.onActivityResult(reqCode, resultCode, intent);
switch (reqCode) {
case ADDRESS_REQUEST2:
EditText edittext2 = (EditText) findViewById(R.id.number2);
Uri uri2 = intent.getData();
Cursor cursor2 = getContentResolver().query(uri2, null, null, null, null);
cursor2.moveToFirst();
String phoneNumber2 = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
edittext2.setText(phoneNumber2);
break;
case ADDRESS_REQUEST3:
EditText edittext3 = (EditText) findViewById(R.id.number3);
Uri uri3 = intent.getData();
Cursor cursor3 = getContentResolver().query(uri3, null, null, null, null);
cursor3.moveToFirst();
String phoneNumber3 = cursor3.getString(cursor3.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
edittext3.setText(phoneNumber3);
break;
case ADDRESS_REQUEST4:
EditText edittext4 = (EditText) findViewById(R.id.number4);
Uri uri4 = intent.getData();
Cursor cursor4 = getContentResolver().query(uri4, null, null, null, null);
cursor4.moveToFirst();
String phoneNumber4 = cursor4.getString(cursor4.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
edittext4.setText(phoneNumber4);
break;
default:
EditText edittext1 = (EditText) findViewById(R.id.number1);
Uri uri1 = intent.getData();
Cursor cursor1 = getContentResolver().query(uri1, null, null, null, null);
cursor1.moveToFirst();
String phoneNumber1 = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
edittext1.setText(phoneNumber1);
break;
}