![](/img/trans.png)
[英]how to pass clicked item's data with onItemClickListener() to another activity
[英]How To Pass Clicked List Item Value to another 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
,還請確保您正確創建了您的意圖,如下所示。
在將發送數據的活動中:
Intent intent = new Intent(getActivity(), SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("phoneNo", phoneNo);
intent.putExtras(bundle);
startActivity(intent);
然后在接收活動上:
Bundle bundle = getIntent().getExtras();
String number = bundle.getString("phoneNo");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.