1
  • The codes below helped me to retrieve all contacts into a listview in an activity, however i wanted to get the selected row of contact from the user to pass it into the intent but im not sure how to do it.

    Example: User selected Suzanne's contact, i want to be able to save "Suzanne" name and number in a string and pass to a intent

public class SendWhoosh_SelectContact extends 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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ong Suling
  • 11
  • 4

2 Answers2

0

Set your onClickListener() and launch your intent as below;

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);
}

When you get to EnterWhooshAmount class, you will get the intent and retrieve the position clicked and with this position, you can retrieve the name etc. You can get the intent in EnterWhooshAmount class as below;

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

So, position is the integer you are looking for, hence, you can retrieve name etc with in this position where you saved it

  • How do i get the name at the next activity after i pass the position into the intent? – Ong Suling Sep 28 '18 at 00:25
  • So now in EnterWhooshAmount.class to get the position i coded 'Intent intent = getIntent();' 'String EXTRA_POSITION = intent.getStringExtra("EXTRA_POSITION");' how do i access and get the name? – Ong Suling Sep 28 '18 at 00:36
  • EXTRA_POSITION could be any String, I save this string with EXTRA_POSITION, yours could be anything. You are doing something else, you are not writing what I post, I have edited my answer for clarity – Show Young Soyinka Sep 28 '18 at 05:55
0

listitem onclick listener changes this code get click item name and number.

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);

            }
        });