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>