簡體   English   中英

在將電話中的所有聯系人填充到列表視圖之后,如何獲取選定的聯系人姓名(將其傳遞到意圖)?

[英]How to get the selected Contact Name (to pass it into an intent) after populating all contacts from phone into a listview?

  • 下面的代碼幫助我將所有聯系人檢索到活動的列表視圖中,但是我想從用戶那里獲取所選的聯系人行,以將其傳遞到意圖中,但是我不確定如何做到這一點。

    示例:用戶選擇了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};
        int[] to = {android.R.id.text1, android.R.id.text2};

        SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, from, to);
        setListAdapter(listadapter);

        listView = getListView();
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {

                String phone = ContactsContract.CommonDataKinds.Phone.NUMBER;
                Log.d(TAG, "SendWhoosh: phone " + phone);


                    Intent intent = new Intent(SendWhoosh_SelectContact.this, EnterWhooshAmount.class);;
                    intent.putExtra("Name", name);
                    intent.putExtra("Number", phone);

                    startActivity(intent);

            }
        });
    } 


----------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/whooshbg"
    android:id="@+id/activitywhoosh_screenarea"
    tools:context=".SendWhoosh_SelectContact">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/li1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/back"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:layout_marginTop="15dp"
            android:textColor="@color/white"
            android:text="Whoosh to "/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:orientation="vertical">

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>



</RelativeLayout>

如下設置您的onClickListener()並啟動您的意圖;

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            launchAnotherActivity(position);
        }
    });
}

private void launchAnotherActivity(int position) {
    Intent intent = new Intent(this, EnterWhooshAmount.class);
    intent.putExtra("The_Position", position);
    startActivity(intent);
}

當您進入EnterWhooshAmount類時,您將獲得意圖並檢索單擊的位置,並在該位置處可以檢索名稱等。您可以在EnterWhooshAmount類中獲得意圖,如下所示;

Intent i = getIntent();
if (i != null){
    int position = i.getIntExtra("The_Position", -1);
}

因此,位置是您要查找的整數,因此,可以在保存該位置的位置檢索名稱等

listitem onclick偵聽器會更改此代碼,以獲取單擊項的名稱和編號。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String name = ((TwoLineListItem) view).getText1().getText().toString();
                String number = ((TwoLineListItem) view).getText2().getText().toString();



                Log.d(TAG, "SendWhoosh: phone " + name);
                Intent intent = new Intent(SendWhoosh_SelectContact.this, EnterWhooshAmount.class);;
                intent.putExtra("Name", name);
                intent.putExtra("Number", number);

                startActivity(intent);

            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM