Advertisement
Tech

How-to create an Address Book in the Google Android Programming Environment - ListActivity Implementation

Now we know how our application is going to be implement from our past lessons. (Analysis is very important when we are developing applications), it’s now time to implement it. Lets start with the ListActivity.

By Jbeerdev
Desk Tech
Reading time 4 min read
Word count 711
Google android Mobile Guides
How-to create an Address Book in the Google Android Programming Environment -  ListActivity Implementation
Advertisement
Quick Take

Now we know how our application is going to be implement from our past lessons. (Analysis is very important when we are developing applications), it’s now time to implement it. Lets start with the ListActivity.

On this page

ListActivity- ContactList

As we said in the How-to create an Address Book in the Google Android Programming Environment - First Steps this class will be an ListActivity that implements OnItemSelectedListener

public class ContactList extends ListActivity implements OnItemSelectedListener

Advertisement

This OnItemSelectedListener is used to interact with every element of the list. With this, we can get the information of the contact if we click on the row where this contact is placed.

We can fill the list with information in two different ways. In our case, we are going to use Contact Providers, its more useful to work with the Method 2 (Cursor List) than the first one. Anyway, lets see both methods.

Advertisement

Method 1 → Static List

With this method, we have to create an ArrayAdapter class. This ArrayAdapter will be created from a ArrayList, and a widget, and will be used by the ListActivity (our class) to create and display the elements in the screen.

Advertisement

The constructor is that easy:

ArrayAdapterfileList=new ArrayAdapter(this, R.layout.row, elements);

Advertisement

where elements is a ArrayList, with the elements we want to add to our list. We can create an static ArrayList of elements

ArrayList elements = new ArrayList ();

Advertisement

(we are using as data type, but we can use whatever we want)

and we can add strings to our ArrayList in this way:

Advertisement

elements.add(“Juan Ito”);

elements.add(“Bar Tolo”);

Advertisement

In the ArrayListAdapter we have another interesting field: R.layout.row. This is a single xml file, a textView usually. Here we can change the appearance of every row in the List. Its like to say “Every row in our list is going to be like the R.layour.row file”.

Once we have our ArrayLisAdapter, we have to attach it to the ListActivity using:

Advertisement

setListAdapter(fileList);

Method 2 → Cursor List

Advertisement

This is slightly different from the first method. Here were are going to use a Cursor as Adapter.

To retrieve this cursor:

Advertisement

Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);

startManagingCursor(c);

Advertisement

Here we get the Contacts information and we store them in a Cursor. The contact URI we need to is People.CONTENT_URI.

Now, once we have it, lets create the adapter:

Advertisement

ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.main, c, new String[] {People.NAME}, new int[] {R.layout.row});

Where:

Advertisement

R.layout.main is the xml file where the ListView is.

c is the Cursor.

new String[] {People.NAME} this is used to say the adapter that we are going to get the column People.NAME and put it in the list.

New int[] {R.layout.row} this is like the Method 1. Just to say how rows are designed.

Finally, to add this adapter to the ListActivity, just:

setListAdapter(adapter);

Listener

The onItemSelected method is inherent when we are using onItemSelectedListener. The application will enter this method when someone clicks in any of the rows of the list.

onItemSelected (AdapterView parent, View view, int position, long id)

Where

parent The AdapterView where the selection happened

view The view within the AdapterView that was clicked

position The position of the view in the adapter

id The row id of the item that is selected

(Ref: Google)

We can use the “parent.getItemAtPosition(position)” method if we need to the data associated with the selected item (in this case, the cursor/Array)

mNamendex = c.getColumnIndex(People.NAME);

Cursor c = (Cursor) parent.getItemAtPosition(position);

We retrieve the cursor from the list.

Now, we want to launch the other Activity, that shows the information of the contact.

Intent intent = new Intent(this,ContactPage.class);

intent.putExtra(“USER_NAME”, c.getString(mNamendex));

We create an Intent, specifying the class we are going to launch (ContactPage), then we add to this intent, information using the putExtra method. This information could be retrieved in the other side (in the ContacPage class). We put a tag “USER_NAME” to identify data.

Once this, we startActivity with the Intent.

startActivity(intent);

Want more?

In our next lessons we will finish creating this application.Stay Tuned!

This post is part of the series: How to- Develop Applications on Android: Simple Applications

In this 5-articles series we will see how to create simple applications, using the tools and knowledge we have acquiredfrom others articles.

  1. How to Create a Calculator in Google Android: Part I
  2. Guide to Creating a Calculator in Android
  3. Dev Guide to Creating an Android Address Book
  4. How-to create an Address Book in the Google Android Programming Environment - ListActivity Implementation
  5. Dev Guide to Making an Android Address Book
Keep Exploring

More from Tech

Filed under
Google android Mobile
More topics
Guides
Advertisement