简体   繁体   English

如何将单击的列表项值传递给另一个活动?

[英]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.

相关问题 如何使用onItemClickListener()将被单击项的数据传递给另一个活动 - how to pass clicked item's data with onItemClickListener() to another activity 将列表视图的点击值传递给具有共享首选项的另一个活动 - Pass list view clicked value to another activity with shared preferences 如何将价值从recyclerview项目传递到另一个活动 - How to pass value from recyclerview item to another activity 在网格视图中单击特定项目并将其传递给另一个活动时,如何从Image Adapter检索数据? - How to retrieve data from Image Adapter when a particular item is clicked in a grid view and pass it to another activity? 单击ListView中的项目时在另一个活动中显示列表 - Showing a List in another activity when clicked on an item in ListView 单击另一个活动时如何发送自定义数组列表的列表项的数据? - How to send data of the list item of a custom arraylist when clicked to another activity? 如何将商品ID传递给另一个活动? - How to pass the item id to another activity? 如何将onOptionsItemSelected项目ID从主要活动传递给另一个活动 - how to pass onOptionsItemSelected item id from Main activity to another activity 如何在单击列表视图项目时出现勾选并且在单击下一个项目之前不会消失当 go 到另一个活动时也不会消失 - How to appeared tick when List View item clicked and do not disappeared till the next item clicked also do not disappeared when go to another activity 单击自定义列表视图中的项目时如何启动新活动 - How to start a new activity when item is clicked of a custom list view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM