[英]How To Pass Clicked List Item Value to another Activity?
I'm making a SMS app, I have a contact list, I want to be able to tap on contact and pass that contact number into new activity so that I could send message to that contact number, I know How can we pass Value of clicked List item to new Activity, but somehow I'm not able to use that Intent.putExtra method in my scenario.我正在制作一个短信应用程序,我有一个联系人列表,我希望能够点击联系人并将该联系人号码传递给新活动,以便我可以向该联系人号码发送消息,我知道我们如何传递价值单击列表项到新活动,但不知何故我无法在我的场景中使用该 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();
}
}
You can use a Bundle to do this.您可以使用Bundle来执行此操作。 You mentioned that you somehow couldn't call putExtra
on your intent, make sure it isn't a typo as it is supposed to be putExtras
, also make sure you are creating your intent correctly as shown below.您提到您无法以某种方式调用putExtra
您的意图,请确保它不是拼写错误,因为它应该是putExtras
,还请确保您正确创建了您的意图,如下所示。
In the activity that will send the data:在将发送数据的活动中:
Intent intent = new Intent(getActivity(), SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("phoneNo", phoneNo);
intent.putExtras(bundle);
startActivity(intent);
Then on the receiving activity:然后在接收活动上:
Bundle bundle = getIntent().getExtras();
String number = bundle.getString("phoneNo");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.