Queue and Display Call Log in a Simple List, Names Only

Put your problem here if it does not fit any of the other categories.

Queue and Display Call Log in a Simple List, Names Only

Postby iPaul Pro » Tue Mar 17, 2009 6:50 pm

I am trying to construct a simple app that allows me to see my call log, displaying names only, in a "simple_list_item_1". I am totally new to Java and Android and this is really the first step in creating my first app.

Here's what I've got (go easy, please):

Manifest:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.      package="com.ipaulpro.calls"
  4.      android:versionCode="1"
  5.      android:versionName="1.0.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/
  7. app_name" android:debuggable="true">
  8.         <activity android:name=".Calls"
  9.                  android:label="@string/app_name">
  10.             <intent-filter>
  11.                 <action android:name="android.intent.action.MAIN" />
  12.                 <category
  13. android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>
  16.     </application>
  17.     <uses-permission android:name="android.permission.READ_CONTACTS"></
  18. uses-permission>
  19. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Calls.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.ipaulpro.calls;
  2.  
  3. import android.app.ListActivity;
  4. import android.database.Cursor;
  5. import android.os.Bundle;
  6. import android.provider.CallLog;
  7. import android.widget.ListAdapter;
  8. import android.widget.SimpleCursorAdapter;
  9.  
  10. public class Calls extends ListActivity {
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.           super.onCreate(savedInstanceState);
  14.           setContentView(R.layout.main);
  15.  
  16.         Cursor c = getContentResolver().query(
  17.                        android.provider.CallLog.Calls.CONTENT_URI,
  18.                        null, null, null,
  19.                        android.provider.CallLog.Calls.DATE+ " DESC");
  20.            startManagingCursor(c);
  21.  
  22.            ListAdapter adapter = new SimpleCursorAdapter(
  23.                this,
  24.                android.R.layout.simple_list_item_1,
  25.                c,
  26.                new String[] {CallLog.Calls.CACHED_NAME},
  27.                new int[] {CallLog.Calls.MISSED_TYPE}
  28.        );
  29.  
  30.        setListAdapter(adapter);
  31.     }
  32.  
  33. }
  34.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Main.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
  3. android"
  4.         android:orientation="vertical"
  5.         android:layout_width="fill_parent"
  6.         android:layout_height="fill_parent">
  7.  
  8.      <ListView android:id="@id/android:list"
  9.               android:layout_width="fill_parent"
  10.               android:layout_height="fill_parent"
  11.  
  12.     />
  13.            <TextView  id="@+id/row_number"
  14.                    android:layout_width="wrap_content"
  15.                    android:layout_height="wrap_content"
  16.          />
  17.  
  18. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


So the app starts, but displays an empty list (with borders).

Any helps, tips, links would be very appreciated.

Thanks in advance.
Last edited by iPaul Pro on Wed Mar 25, 2009 7:36 pm, edited 1 time in total.
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Top

Postby iPaul Pro » Thu Mar 19, 2009 9:03 pm

Okay, so I've achieved my intended goal (the first step in my application).

I thought I'd share the code to output a simple list with cached names from the call log (no name displayed for UNKNOWN and PRIVATE numbers -- I'm working on that and will report when I figure it out - again, I'm a newb and know this can be achieved with an "if - then" statement - just don't know how to do it yet).

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.ListActivity;
  2.  
  3. import android.database.Cursor;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import android.widget.SimpleCursorAdapter;
  8.  
  9.  
  10.  
  11. public class Calls extends ListActivity {
  12.  
  13.     /** Called when the activity is first created. */
  14.  
  15.     @Override
  16.  
  17.     public void onCreate(Bundle savedInstanceState) {
  18.  
  19.           super.onCreate(savedInstanceState);
  20.  
  21.                
  22.  
  23.         Cursor c = getContentResolver().query(
  24.  
  25.                        android.provider.CallLog.Calls.CONTENT_URI,
  26.  
  27.                        null,
  28.  
  29.                        null,
  30.  
  31.                        null,
  32.  
  33.                        android.provider.CallLog.Calls.DATE+ " DESC");
  34.  
  35.            startManagingCursor(c);
  36.  
  37.  
  38.  
  39.            // Now create an adapter and set it to display using android.R.layout.simple_list_item_1
  40.  
  41.            SimpleCursorAdapter calls = new SimpleCursorAdapter(
  42.  
  43.                            this,
  44.  
  45.                    android.R.layout.simple_list_item_1,
  46.  
  47.                    c,
  48.  
  49.                    new String[] { android.provider.CallLog.Calls.CACHED_NAME },
  50.  
  51.                    new int[] { android.R.id.text1 });
  52.  
  53.            setListAdapter(calls);
  54.  
  55.          
  56.  
  57.     }
  58.  
  59. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


There's no need to specify xml in your res/layout/ as we are using default Android styles.

The only change to the Manifest is adding the permission to read contact data (call log pulls from contacts)

Put
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

anywhere before or after the <application> tags.

And in case someone is wondering, here how you would output the cached name and number on a two-line list, using the "simple list 2" template.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.ListActivity;
  2.  
  3. import android.database.Cursor;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import android.widget.SimpleCursorAdapter;
  8.  
  9.  
  10.  
  11. public class Calls extends ListActivity {
  12.  
  13.     /** Called when the activity is first created. */
  14.  
  15.     @Override
  16.  
  17.     public void onCreate(Bundle savedInstanceState) {
  18.  
  19.           super.onCreate(savedInstanceState);
  20.  
  21.                
  22.  
  23.         Cursor c = getContentResolver().query(
  24.  
  25.                        android.provider.CallLog.Calls.CONTENT_URI,
  26.  
  27.                        null,
  28.  
  29.                        null,
  30.  
  31.                        null,
  32.  
  33.                        android.provider.CallLog.Calls.DATE+ " DESC");
  34.  
  35.            startManagingCursor(c);
  36.  
  37.  
  38.  
  39.            // Now create an adapter and set it to display using android.R.layout.simple_list_item_2
  40.  
  41.            SimpleCursorAdapter calls = new SimpleCursorAdapter(
  42.  
  43.                            this,
  44.  
  45.                    android.R.layout.simple_list_item_2,
  46.  
  47.                    c,
  48.  
  49.                    new String[] { android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.NUMBER },
  50.  
  51.                    new int[] { android.R.id.text1, android.R.id.text2 });
  52.  
  53.            setListAdapter(calls);
  54.  
  55.                    
  56.  
  57.     }
  58.  
  59. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Hope that helps someone out there. I'll keep this thread updated with my related findings.
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests