This class will extend from an Activity. We need to create a .xml file with the appearance of the page (how contact information is going to be displayed). This will be left to your imagination, we know how to do this with Droid Draw, so there is not much difficulty. The important thing in this page is:
-How to get the information from the Intent
-How to set the contact information to the UI
If we go back, in my last article, I wrote about Intents and how to send information from one Activity to another. In that case, we created an Intent, and we sent the “User name” to the Contact Page Activity. Its time to get this information in the new Activity that has been created.
In the ContactPage Activity, we need to do the following:
Intent intent = getIntent().
This will get the Intent we sent from the ContactList ListActivity, and with the following line, we retrieve the information from the Intent.
String userName = intent.getStringExtra("USER_NAME ");
(We are using the same string we used in the ListActivity: USER_NAME)
Now, in userName we have the name of the user we have clicked on the ListView (we could have retrieved the User ID, or the user Phone...)
Now we need to get the following example elements from the .xml file.
nameView = (TextView) findViewById(R.id.name);
imageView = (ImageView) findViewById(R.id.picture);
callButton = (ImageButton) findViewById(R.id.call);
smsButton = (ImageButton) findViewById(R.id.sms);
Lets suppose that we have created an TextView with the “name” id, a ImageView with the id “picture” and two ImageButtons with the followings ids: “call” and “sms”. Yo can add whatever information you want, this is just an example to show how to deal with them.
Now, we have the the User Name, we can get the full information, getting them from the Content Provider. Lets do it again, once more.
Cursor c = getContentResolver().query(People.CONTENT_URI, null, People.NAME +”='” +userName+”'”, null, null);
Select * from people where name = “Pepe Botijo”
(To people who have never deal with SQL, this is the language its used to manage Data Bases)
Once we get the cursor, we move trough it
if (c.moveToFirst()) {
idColumn = c.getColumnIndex(People._ID);
nameColumn = c.getColumnIndex(People.NAME);
phoneColum = c.getColumnIndex(People.NUMBER);
do {
String id = c.getString(idColumn);
String name = c.getString(nameColumn);
String phoneNumber = c.getString(phoneColumn);
Uri peopleURI = ContentUris.withAppendedId(People.CONTENT_URI, new Long(id));
Bitmap contactPicture = People.loadContactPhoto(this,peopleURI, R.drawable.default_image, null); imageView.setImageBitmap(contactPicture);
nameView.setText(name);
} while (c.moveToNext());
}
Lets explain a bit this code.
Once we have the cursor, we move through it to retrieve the information we want. To get the picture of the contact, we need to use a special method: People.loadContactPhoto. And give this method the URI of the contact we want to get the picture (Uri appended with the witAppendedID method and the ID of the contact). To finish, we set the values we have retrieved to the view, adding the texts and the image.