如何将单击的列表项值传递给另一个 Activity?
How To Pass Clicked List Item Value to another Activity?
我正在制作一个 SMS 应用程序,我有一个联系人列表,我希望能够点击联系人并将该联系人号码传递到新的 activity 以便我可以向该联系人号码发送消息,
我知道我们如何将单击的列表项的值传递给新的 Activity,但不知何故我无法在我的场景中使用该 Intent.putExtra 方法。
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {
id
},
null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
}
您可以使用 Bundle 来做到这一点。您提到您无法按您的意图调用 putExtra
,请确保它不是打字错误,因为它应该是 putExtras
,还要确保您正确地创建了您的意图,如下所示.
在将发送数据的activity中:
Intent intent = new Intent(getActivity(), SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("phoneNo", phoneNo);
intent.putExtras(bundle);
startActivity(intent);
然后在接收activity:
Bundle bundle = getIntent().getExtras();
String number = bundle.getString("phoneNo");
我正在制作一个 SMS 应用程序,我有一个联系人列表,我希望能够点击联系人并将该联系人号码传递到新的 activity 以便我可以向该联系人号码发送消息, 我知道我们如何将单击的列表项的值传递给新的 Activity,但不知何故我无法在我的场景中使用该 Intent.putExtra 方法。
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {
id
},
null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
}
您可以使用 Bundle 来做到这一点。您提到您无法按您的意图调用 putExtra
,请确保它不是打字错误,因为它应该是 putExtras
,还要确保您正确地创建了您的意图,如下所示.
在将发送数据的activity中:
Intent intent = new Intent(getActivity(), SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("phoneNo", phoneNo);
intent.putExtras(bundle);
startActivity(intent);
然后在接收activity:
Bundle bundle = getIntent().getExtras();
String number = bundle.getString("phoneNo");