Hello all,
I am new to android and java programming. I am trying to display a
list with People and their contact numbers as following:
Name
TYPE
Number
I am using ExpandableListActivity to accomplish that. Everything is
working good when i am using the integer value if People.TYPE..but if
i am trying to convert that to a string representation i get an
unhandled exception. Can someone tell me what i am doing wrong?
My Code:
------------------------------------------------------------------------------------------------------------------------------------------------------------
package org.ListExpandable;
import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleCursorTreeAdapter;
public class ListExpandable extends ExpandableListActivity {
private int mGroupIdColumnIndex;
private String mPhoneNumberProjection[] = new String[] {
People.Phones._ID, People.Phones.NUMBER
};
private ExpandableListAdapter mAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Query for people
Cursor groupCursor = managedQuery(People.CONTENT_URI,
new String[] {People._ID, People.NAME}, null, null,
null);
// Cache the ID column index
mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow
(People._ID);
// Set up our adapter
mAdapter = new MyExpandableListAdapter(groupCursor,
this,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_2,
new String[] {People.NAME}, // Name for group layouts
new int[] {android.R.id.text1},
new String[] { NumberTypeToString(People.TYPE) ,
People.NUMBER}, // Number for child layouts
new int[] {android.R.id.text1,android.R.id.text2});
setListAdapter(mAdapter);
}
private String NumberTypeToString(String underlying_type){
String retValue="Other";
int converted_type=Integer.parseInt(underlying_type);
switch (converted_type)
{
case People.TYPE_HOME: retValue="Home"; break;
case People.TYPE_MOBILE: retValue="Mobile"; break;
case People.TYPE_WORK: retValue="Work"; break;
case People.TYPE_OTHER: retValue="Other"; break;
}
return retValue;
}
public class MyExpandableListAdapter extends
SimpleCursorTreeAdapter {
public MyExpandableListAdapter(Cursor cursor, Context context,
int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo,
String[] childrenFrom,
int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom,
childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
// Given the group, we return a cursor for all the
children within that group
// Return a cursor that points to this contact's phone
numbers
Uri.Builder builder = People.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, groupCursor.getLong
(mGroupIdColumnIndex));
builder.appendEncodedPath
(People.Phones.CONTENT_DIRECTORY);
Uri phoneNumbersUri = builder.build();
// The returned Cursor MUST be managed by us, so we use
Activity's helper
// functionality to manage it for us.
//return managedQuery(phoneNumbersUri,
mPhoneNumberProjection, null, null, null);
return managedQuery(phoneNumbersUri, null, null, null,
null);
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Thank you for your help
Stefan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---