我认为int index=data.getIntExtra("recordindex", 0)
的值应为60,但我得到值0,为什么?
我犯了什么错误?谢谢!
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_tel_number);
Button btnadd = (Button) findViewById(R.id.btnadd);
btnadd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
i.putExtra("recordindex", 60);
startActivityForResult(i, 50);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 50:
if (resultCode == RESULT_OK) {
if (data == null) {
return;
}
//------------------------------------------------
ContentResolver reContentResolverol = getContentResolver();
Uri contactData = data.getData();
int index=data.getIntExtra("recordindex", 0); //The value should be 60, but it's 0, why
Cursor cursor = managedQuery(contactData, null, null, null, null);
if (cursor.moveToFirst()) {
String username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = reContentResolverol.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phone.moveToFirst()) {
String usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(), index + " B " + usernumber, Toast.LENGTH_SHORT).show();
}
}
//------------------------------------------------
}
break;
default:
break;
}
}
}
答案 0 :(得分:3)
在这里,您为Intent i
创建了ACTION_PICK
,其中您传递了额外的值“recordindex”。当然,你可以使用Intents传递价值
但是你在onActivityResult(int requestCode, int resultCode, Intent data)
内收到的意图不是你的意图。它是一个不同的,不包含额外的字段。
查看您的代码,因为您需要在同一活动中使用“recordindex”并且不保持持久性,只需将其用作变量即可。
int recordindex=0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_tel_number);
Button btnadd = (Button) findViewById(R.id.btnadd);
btnadd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
recordindex=60;
Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, 50);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 50:
if (resultCode == RESULT_OK) {
if (data == null) {
return;
}
//------------------------------------------------
ContentResolver reContentResolverol = getContentResolver();
Uri contactData = data.getData();
int index=recordindex; //Here it is
.........
.........
}