Displaying The Phone Number Of A Contact In Android Contact Content Provider
i am trying to write a program that would display the id, name and phone number of a contact in android. So far the Id and Names of the contact are displayed but the number of the
Solution 1:
I got your example working by adding a CustomAdapter MyClassAdapter which extends ArrayAdapter, and populating an ArrayList of Contact objects.
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
publicclassProvider2ActivityextendsListActivity {
ArrayList<Contact> contacts;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provider2);
contacts = newArrayList<Contact>();
UriallContacts= ContactsContract.Contacts.CONTENT_URI;
//declare our cursor
Cursor c;
String[] projection = newString[]
{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
/* >>The second parameter of the managedQuery() method (third parameter for the CursorLoader class)
controls how many columns are returned by the query; this parameter is known as the projection
>>The third parameter of the managedQuery() method (fourth parameter for the CursorLoader class)
enable you to specify a SQL WHERE clause to filter the result of the query
>>The fourth parameter of the managedQuery() method (the fifth parameter for the CursorLoader class)
enables you to specify a SQL ORDER BY clause to sort the result of the query, either in ascending or descending order
* */if (android.os.Build.VERSION.SDK_INT <11) {
//---if the device ids running on OS before Honeycomb//use the managedQuery() of the Activity class to retrieve a managed cursor
c = managedQuery(allContacts, projection, null, null, null);
}
else {
//---Honeycomb and later use the cursor loader class to retrieve managed cursor---CursorLoadercursorLoader=newCursorLoader(
this,
allContacts,
projection,
null,
null ,
null);
c = cursorLoader.loadInBackground();
}
PrintContacts(c);
MyClassAdapter adapter;
//detect the android version again..if (android.os.Build.VERSION.SDK_INT <11) {
//---if it is before Honeycomb---
adapter = newMyClassAdapter(
this, R.layout.row_line, contacts);
}
else {
//---Honeycomb and later---
adapter = newMyClassAdapter(
this, R.layout.row_line, contacts);
}
this.setListAdapter(adapter);
}
privatevoidPrintContacts(Cursor c)
{
ContentResolvercr= getContentResolver();
//---display the contact id and name and phone number----if (c.moveToFirst()) {
do{
//---get the contact id and nameStringcontactID= c.getString(c.getColumnIndex(
ContactsContract.Contacts._ID));
StringcontactDisplayName=
c.getString(c.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
Log.v("Content Providers", contactID + ", " +
contactDisplayName);
StringcontactDisplayPhone="";
//---get phone number---inthasPhone=
c.getInt(c.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone == 1) {
CursorphoneCursor=
getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " +
contactID, null, null);
while (phoneCursor.moveToNext()) {
Log.v("Content Providers",
contactDisplayPhone = phoneCursor.getString(
phoneCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phoneCursor.close();
}
contacts.add(newContact(contactDisplayName, contactID, contactDisplayPhone));
} while (c.moveToNext());
}
}
publicclassContact{
publicStringcontactName="";
publicStringcontactID="";
publicStringcontactNumber="";
publicContact(String name, String id, String number){
contactName = name;
contactID = id;
contactNumber = number;
}
}
publicclassMyClassAdapterextendsArrayAdapter<Contact> {
privateclassViewHolder {
private TextView name;
private TextView id;
private TextView number;
}
publicMyClassAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
super(context, textViewResourceId, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.row_line, parent, false);
viewHolder = newViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.contactName);
viewHolder.id = (TextView) convertView.findViewById(R.id.contactID);
viewHolder.number = (TextView) convertView.findViewById(R.id.contactNumber);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Contactitem= getItem(position);
if (item!= null) {
viewHolder.name.setText(item.contactName);
viewHolder.id.setText(item.contactID);
viewHolder.number.setText(item.contactNumber);
}
return convertView;
}
}
}
row_line.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/contactName"android:textSize="16sp"android:textStyle="bold"android:layout_width="match_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/contactID"android:textSize="16sp"android:layout_width="match_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/contactNumber"android:textSize="16sp"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>activity_provider2.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Provider2Activity">
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</RelativeLayout>
Post a Comment for "Displaying The Phone Number Of A Contact In Android Contact Content Provider"