ExpandableListActivity issue

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ExpandableListActivity issue

Postby stefanegon » Wed Oct 21, 2009 8:28 pm

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.ListExpandable;
  3.  
  4.  
  5.  
  6. import android.app.ExpandableListActivity;
  7.  
  8. import android.content.ContentUris;
  9.  
  10. import android.content.Context;
  11.  
  12. import android.database.Cursor;
  13.  
  14. import android.net.Uri;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.provider.Contacts.People;
  19.  
  20. import android.widget.ExpandableListAdapter;
  21.  
  22. import android.widget.SimpleCursorTreeAdapter;
  23.  
  24.  
  25.  
  26. public class ListExpandable extends ExpandableListActivity {
  27.  
  28.        
  29.  
  30.                 private int mGroupIdColumnIndex;
  31.  
  32.  
  33.  
  34.             private String mPhoneNumberProjection[] = new String[] {
  35.  
  36.                     People.Phones._ID, People.Phones.NUMBER
  37.  
  38.             };
  39.  
  40.  
  41.  
  42.             private ExpandableListAdapter mAdapter;
  43.  
  44.  
  45.  
  46.        
  47.  
  48.     /** Called when the activity is first created. */
  49.  
  50.     @Override
  51.  
  52.     public void onCreate(Bundle savedInstanceState) {
  53.  
  54.         super.onCreate(savedInstanceState);
  55.  
  56.        
  57.  
  58.         // Query for people
  59.  
  60.         Cursor groupCursor = managedQuery(People.CONTENT_URI,
  61.  
  62.                 new String[] {People._ID, People.NAME}, null, null, null);
  63.  
  64.  
  65.  
  66.         // Cache the ID column index
  67.  
  68.         mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow(People._ID);
  69.  
  70.  
  71.  
  72.         // Set up our adapter
  73.  
  74.         mAdapter = new MyExpandableListAdapter(groupCursor,
  75.  
  76.                 this,
  77.  
  78.                 android.R.layout.simple_expandable_list_item_1,
  79.  
  80.                 android.R.layout.simple_expandable_list_item_2,
  81.  
  82.                 new String[] {People.NAME}, // Name for group layouts
  83.  
  84.                 new int[] {android.R.id.text1},
  85.  
  86.                 new String[] { NumberTypeToString(People.TYPE) , People.NUMBER}, // Number for child layouts
  87.  
  88.                 new int[] {android.R.id.text1,android.R.id.text2});
  89.  
  90.         setListAdapter(mAdapter);
  91.  
  92.        
  93.  
  94.     }
  95.  
  96.    
  97.  
  98.    
  99.  
  100.     private String NumberTypeToString(String underlying_type){
  101.  
  102.         String retValue="Other";
  103.  
  104.        
  105.  
  106.         int converted_type=Integer.parseInt(underlying_type);
  107.  
  108.        
  109.  
  110.         switch (converted_type)
  111.  
  112.         {
  113.  
  114.         case People.TYPE_HOME: retValue="Home"; break;
  115.  
  116.         case People.TYPE_MOBILE: retValue="Mobile"; break;
  117.  
  118.         case People.TYPE_WORK: retValue="Work"; break;
  119.  
  120.         case People.TYPE_OTHER: retValue="Other"; break;
  121.  
  122.         }
  123.  
  124.         return retValue;
  125.  
  126.     }
  127.  
  128.    
  129.  
  130.    
  131.  
  132.     public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
  133.  
  134.  
  135.  
  136.         public MyExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
  137.  
  138.                 int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
  139.  
  140.                 int[] childrenTo) {
  141.  
  142.             super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,
  143.  
  144.                     childrenTo);
  145.  
  146.         }
  147.  
  148.  
  149.  
  150.         @Override
  151.  
  152.         protected Cursor getChildrenCursor(Cursor groupCursor) {
  153.  
  154.             // Given the group, we return a cursor for all the children within that group
  155.  
  156.  
  157.  
  158.             // Return a cursor that points to this contact's phone numbers
  159.  
  160.             Uri.Builder builder = People.CONTENT_URI.buildUpon();
  161.  
  162.             ContentUris.appendId(builder, groupCursor.getLong(mGroupIdColumnIndex));
  163.  
  164.             builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
  165.  
  166.             Uri phoneNumbersUri = builder.build();
  167.  
  168.  
  169.  
  170.             // The returned Cursor MUST be managed by us, so we use Activity's helper
  171.  
  172.             // functionality to manage it for us.
  173.  
  174.             //return managedQuery(phoneNumbersUri, mPhoneNumberProjection, null, null, null);
  175.  
  176.             return managedQuery(phoneNumbersUri, null, null, null, null);
  177.  
  178.         }
  179.  
  180.  
  181.  
  182.     }
  183.  
  184.  
  185.  
  186. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Thank you for your help
Stefan
stefanegon
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 21, 2009 8:18 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests