将手机中的所有联系人填充到列表视图中后,如何获取选定的联系人姓名(将其传递给意图)?

下面的代码帮助我将所有联系人检索到活动中的列表视图中,但是我想从用户那里获取选定的联系人行以将其传递到意图中,但我不知道该怎么做。


示例:用户选择了 Suzanne 的联系人,我希望能够将“Suzanne”姓名和号码保存在一个字符串中并传递给一个意图


公共类 SendWhoosh_SelectContact 扩展 ListActivity {


    private static final String TAG = "SendWhoosh";

    ListView listView;

    Cursor cursor;


    @Override

    public long getSelectedItemId()

    {

        // TODO Auto-generated method stub

        return super.getSelectedItemId();

    }


    @Override

    public int getSelectedItemPosition()

    {

        // TODO Auto-generated method stub

        return super.getSelectedItemPosition();

    }


    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_send_whoosh_select_contact);


        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        startManagingCursor(cursor);


        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};


慕标5832272
浏览 160回答 2
2回答

慕婉清6462132

设置您的 onClickListener() 并启动您的意图,如下所示;listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; launchAnotherActivity(position);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}private void launchAnotherActivity(int position) {&nbsp; &nbsp; Intent intent = new Intent(this, EnterWhooshAmount.class);&nbsp; &nbsp; intent.putExtra("The_Position", position);&nbsp; &nbsp; startActivity(intent);}当您到达 EnterWhooshAmount 类时,您将获得意图并检索单击的位置,并且通过该位置,您可以检索名称等。您可以在 EnterWhooshAmount 类中获得意图,如下所示;Intent i = getIntent();if (i != null){&nbsp; &nbsp; int position = i.getIntExtra("The_Position", -1);}因此,位置是您要查找的整数,因此,您可以在保存它的位置检索名称等

慕村225694

listitem onclick 侦听器更改此代码获取单击项名称和编号。listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = ((TwoLineListItem) view).getText1().getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String number = ((TwoLineListItem) view).getText2().getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "SendWhoosh: phone " + name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(SendWhoosh_SelectContact.this, EnterWhooshAmount.class);;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("Name", name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("Number", number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java