Reputation: 37
for part of my application I require a list of all contacts (with phone numbers) to be displayed when the option is selected.
Here is the activity which is called when the button is pressed:
package com.example.prototype01;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
public class nominateContactsActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nominatecontactslayout);
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String contactName, contactTelNumber = "";
String contactID;
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
contactName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
while (pCur.moveToNext()) {
contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
Log.i("name ", contactName + " ");
Log.i("number ", contactTelNumber + " ");
c.moveToNext();
}
}
}
As you can see, this code returns the Name and Phone Number of all contacts stored on the handset. Currently, these are simply echoed to logcat. I can't seem to work out how to list these items in a listview instead, with just the name and number displayed. I have followed a couple of tutorials to no avail and so reluctantly i seek your kind assistance. I say reluctantly as I'm sure this question has been answered many times, I just can't seem to apply the solutions to my code!
Thank you in advance!!
Kind Regards, Antwan
Upvotes: 2
Views: 1366
Reputation: 1785
Here is what I would do to put contacts in a listView
In your activity:
private ListView mContactsListView;
private ListContactItemAdapter mContactsListAdapter;
this.mContactsListAdapter = new ListContactItemAdapter(this, R.layout.contact_row);
// after doing setContentView, assuming you have defined a listview in your layout file
this.mContactsListView= (ListView) this.findViewById(R.id.contactsListView);
// You may want to create a custom adapter, which I wrote below for showing you example
this.mContactsListView.setAdapter(this.mContactsListAdapter);
for (/* each contact */) {
Contact contact = new Contact();
contact.name = contactName;
contact.number = contactNumber;
this.mContactsListAdapter.add(contact);
}
// In order to refresh your list and make data appear
this.mContactsListAdapter.notifyDataSetChanged();
Of course in my example you need a model object containing contact data:
public class Contact {
public String name;
public String number;
}
And this could be your custom adapter using the Contact class above:
public class ListContactItemAdapter extends ArrayAdapter<Contact> {
private int mLineLayout;
private LayoutInflater mInflater;
public ListContactItemAdapter(Context pContext, int pLineLayout) {
super(pContext, pLineLayout);
this.mLineLayout = pLineLayout;
this.mInflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
TextView contactName;
TextView contactNumber;
}
@Override
public View getView(int pPosition, View pView, ViewGroup pParent) {
ViewHolder holder;
if (pView == null) {
pView = this.mInflater.inflate(this.mLineLayout, null);
holder = new ViewHolder();
holder.contactName = (TextView) pView.findViewById(R.id.contactName);
holder.contactNumber = (TextView) pView.findViewById(R.id.contactNumber);
pView.setTag(holder);
} else {
holder = (ViewHolder) pView.getTag();
}
Contact contact = getItem(pPosition);
if (contact != null) {
holder.contactName.setText(contact.name);
holder.contactNumber.setText(contact.number);
}
return pView;
}
}
For that example your need a layout to define rows, of your listview. This could be a contact_row.xml example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:text="Contact name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/contactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/contactName"
android:paddingLeft="6dp"
android:text="number"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
I didn't try but this should work. And anyway, I hope it will make you understand how listviews basically work.
Upvotes: 2
Reputation: 86958
Here is one way to adapt what you already have. I added comments before everything I changed to explain along the way:
public class nominateContactsActivity extends Activity {
// Add a list to keep all the "name: number" strings
private List<String> mNameNumber = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nominatecontactslayout);
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String contactName, contactTelNumber = "";
String contactID;
// You only need to find these indices once
int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
int hasNumberIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
// This is simpler than calling getCount() every iteration
while(c.moveToNext()) {
contactName = c.getString(nameIndex);
contactID = c.getString(idIndex);
// If this is an integer ask for an integer
if (c.getInt(hasNumberIndex)) > 0) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
while (pCur.moveToNext()) {
contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Store the "name: number" string in our list
mNameNumber.add(contactName + ": " + contactTelNumber);
}
}
}
// Find the ListView, create the adapter, and bind them
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mNameNumber);
listView.setAdapter(adapter);
}
}
Upvotes: 1