[VIDEO-Tut] - Querying and Displaying the CallLog

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Formatting CallLog Date and Time (Absolute)

Postby iPaul Pro » Sat Mar 28, 2009 4:38 pm

Okay, So I found out that using FormatDate (SimpleDateFormat) is not preferred for loops with a lot of information (that is why my app was taking so long to load)

I have found alternatives.

For absolute date and time, try this (not sure if this is the best way but it works and is fast):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.      long created = c.getLong(dateColumn);
  3.  
  4.         Date nDate = new Date(created);                                        
  5.  
  6.         Calendar cal = Calendar.getInstance();
  7.  
  8.         cal.setTime(nDate);
  9.  
  10.        
  11.  
  12.                 int iMonth = cal.get(Calendar.MONTH) + 1;
  13.  
  14.                 int iDay = cal.get(Calendar.DAY_OF_MONTH);
  15.  
  16.                 int iDayName = cal.get(Calendar.DAY_OF_WEEK);
  17.  
  18.                 int iHour = cal.get(Calendar.HOUR);
  19.  
  20.                 int iMin = cal.get(Calendar.MINUTE);
  21.  
  22.                 int iAmPm = cal.get(Calendar.AM_PM);
  23.  
  24.        
  25.  
  26.                
  27.  
  28.                     String month = Integer.toString(iMonth);
  29.  
  30.                         if(iMonth == 1){
  31.  
  32.                             month = "January";
  33.  
  34.                         }
  35.  
  36.                     if(iMonth == 2){
  37.  
  38.                             month = "February";
  39.  
  40.                     }
  41.  
  42.                     if(iMonth == 3){
  43.  
  44.                             month = "March";
  45.  
  46.                     }
  47.  
  48.                         if(iMonth == 4){
  49.  
  50.                             month = "April";
  51.  
  52.                         }
  53.  
  54.                     if(iMonth == 5){
  55.  
  56.                             month = "May";
  57.  
  58.                     }
  59.  
  60.                     if(iMonth == 6){
  61.  
  62.                             month = "June";
  63.  
  64.                     }
  65.  
  66.                         if(iMonth == 7){
  67.  
  68.                             month = "July";
  69.  
  70.                         }
  71.  
  72.                     if(iMonth == <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" />{
  73.  
  74.                             month = "August";
  75.  
  76.                     }
  77.  
  78.                     if(iMonth == 9){
  79.  
  80.                             month = "September";
  81.  
  82.                     }
  83.  
  84.                         if(iMonth == 10){
  85.  
  86.                             month = "October";
  87.  
  88.                         }
  89.  
  90.                     if(iMonth == 11){
  91.  
  92.                             month = "November";
  93.  
  94.                     }
  95.  
  96.                     if(iMonth == 12){
  97.  
  98.                             month = "December";
  99.  
  100.                         }
  101.  
  102.  
  103.  
  104.             String dayName = Integer.toString(iDayName);
  105.  
  106.                         if(iDayName == 1){
  107.  
  108.                             dayName = "Sunday";
  109.  
  110.                         }
  111.  
  112.                         if(iDayName == 2){
  113.  
  114.                             dayName = "Monday";
  115.  
  116.                         }
  117.  
  118.                         if(iDayName == 3){
  119.  
  120.                             dayName = "Tuesday";
  121.  
  122.                         }
  123.  
  124.                         if(iDayName == 4){
  125.  
  126.                             dayName = "Wednesday";
  127.  
  128.                         }
  129.  
  130.                         if(iDayName == 5){
  131.  
  132.                             dayName = "Thursday";
  133.  
  134.                         }
  135.  
  136.                         if(iDayName == 6){
  137.  
  138.                             dayName = "Friday";
  139.  
  140.                         }
  141.  
  142.                         if(iDayName == 7){
  143.  
  144.                             dayName = "Saturday";
  145.  
  146.                         }
  147.  
  148.                        
  149.  
  150.                 String day = Integer.toString(iDay);
  151.  
  152.                         if(iDay < 10){
  153.  
  154.                             day = "0" + day;
  155.  
  156.                         }
  157.  
  158.        
  159.  
  160.                 String hour = Integer.toString(iHour);
  161.  
  162.                         if(iHour == 0){
  163.  
  164.                             hour = "12";
  165.  
  166.                         }
  167.  
  168.        
  169.  
  170.                 String min = Integer.toString(iMin);
  171.  
  172.                         if(iMin < 10){
  173.  
  174.                             min = "0" + min;
  175.  
  176.                         }
  177.  
  178.        
  179.  
  180.                 String amPm = Integer.toString(iAmPm);
  181.  
  182.                         if(iAmPm == 0){
  183.  
  184.                                 amPm = "AM";
  185.  
  186.                         } else {
  187.  
  188.                                 amPm = "PM";
  189.  
  190.                         }
  191.  
  192.  
  193.  
  194.         String myDate = dayName + ", " + month + " " + day + "," + " " + cal.get(Calendar.YEAR);
  195.  
  196.         String myTime = hour + ":" + min + " " + amPm;
  197.  
  198.                            
  199.  
  200. callList.add(new IconifiedText(callerName, callerPhoneNumber, myDate, myTime, currentIcon));                                
Parsed in 0.187 seconds, using GeSHi 1.0.8.4


This is will ouput the date like so: Saturday, March 28, 2009
and the time: 11:30 AM

IMPORTANT - I have modified the IconifiedTextListAdapter.java, IconifiedText.java, and IconifiedTextView.java to accept the extra "myDate" and "myTime" views. This must be done first, if you wish to implement my date and time fix (HINT - just 'duplicate' the existing TextViews).

If anyone needs help on making this work, feel free to ask me how to do it. I promise to answer, whether or not I know how to help.

I have a solution for relative dates and times, however it requires a ViewBinder and SimpleCursorAdapter to be used, and I have yet to figure out how to incorporate with the IconifiedTextListAdapter.

Hope this helps someone.
- iPaul Pro
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Top

Set an ACTION_CALL Intent to onListItemClick

Postby iPaul Pro » Sat Mar 28, 2009 10:10 pm

Hello again,

I'm trying to make an onListItemClick that would call the Calls.NUMBER of the selected list row. Here's what I have (after onCreate):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onListItemClick(ListView l, View v, int position, long id) {
  5.  
  6.                 super.onListItemClick(l, v, position, id);
  7.  
  8.                
  9.  
  10.                 Intent i = new Intent(Intent.ACTION_CALL);
  11.  
  12.                
  13.  
  14.                 Cursor c = (Cursor) ((IconifiedTextListAdapter)getListAdapter()).getItem(position);
  15.  
  16.                 long phoneNum = c.getLong(c.getColumnIndex(Calls.NUMBER));
  17.  
  18.                
  19.  
  20.                 i.setData(ContentUris.withAppendedId(
  21.  
  22.                     android.provider.CallLog.Calls.CONTENT_URI, phoneNum));
  23.  
  24.                
  25.  
  26.                 this.startActivity(i);
  27.  
  28.         }
  29.  
  30.  
Parsed in 0.140 seconds, using GeSHi 1.0.8.4


Any help, hints, or links would be greatly appreciated.

Thanks
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby iPaul Pro » Tue Apr 07, 2009 5:58 pm

How can I set and onListItemClick to load an intent, bundling the callerName variable?

Here's what I've tried:

1.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onListItemClick(ListView l, View v, int position, long id) {
  5.  
  6.                 super.onListItemClick(l, v, position, id);
  7.  
  8.  
  9.  
  10.                 Intent intent = new Intent(this, MyActivity.class);
  11.  
  12.                 Cursor cursor = (Cursor) itla.getItem(position);
  13.  
  14.                  Bundle b = new Bundle();
  15.  
  16.                  String cCallerName = cursor.getString(cursor.getColumnIndex(Calls.CACHED_NAME));
  17.  
  18.         b.putString("CallerName", cCallerName);
  19.  
  20.         intent.putExtras(b);
  21.  
  22.                 startActivity(intent);
  23.  
  24.         }
Parsed in 0.145 seconds, using GeSHi 1.0.8.4


- Force close,

2.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onListItemClick(ListView l, View v, int position, long id) {
  5.  
  6.                 super.onListItemClick(l, v, position, id);
  7.  
  8.  
  9.  
  10.                 Intent intent = new Intent(this, MyActivity.class);
  11.  
  12.                 Cursor cursor = (Cursor) ((IconifiedTextListAdapter)getListAdapter()).getItem(position);
  13.  
  14.                  Bundle b = new Bundle();
  15.  
  16.                  String cCallerName = cursor.getString(cursor.getColumnIndex(Calls.CACHED_NAME));
  17.  
  18.         b.putString("CallerName", cCallerName);
  19.  
  20.         intent.putExtras(b);
  21.  
  22.                 startActivity(intent);
  23.  
  24.         }
  25.  
  26.  
Parsed in 0.107 seconds, using GeSHi 1.0.8.4


- again, force close.

3.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onListItemClick(ListView l, View v, int position, long id) {
  5.  
  6.                 super.onListItemClick(l, v, position, id);
  7.  
  8.  
  9.  
  10.                 Intent intent = new Intent(this, MyActivity.class);
  11.  
  12.                 c = (Cursor) ((IconifiedTextListAdapter)getListAdapter()).getItem(position);
  13.  
  14.                  Bundle b = new Bundle();
  15.  
  16.                  String cCallerName = c.getString(c.getColumnIndex(Calls.CACHED_NAME));
  17.  
  18.         b.putString("CallerName", cCallerName);
  19.  
  20.         intent.putExtras(b);
  21.  
  22.                 startActivity(intent);
  23.  
  24.         }
Parsed in 0.060 seconds, using GeSHi 1.0.8.4


- you guessed it.

4.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onListItemClick(ListView l, View v, int position, long id) {
  5.  
  6.                 super.onListItemClick(l, v, position, id);
  7.  
  8.  
  9.  
  10.                 Intent intent = new Intent(this, MyActivity.class);
  11.  
  12.                 c = (Cursor) itla.getItem(position);
  13.  
  14.                  Bundle b = new Bundle();
  15.  
  16.         b.putString("CallerName", callerName);
  17.  
  18.         intent.putExtras(b);
  19.  
  20.                 startActivity(intent);
  21.  
  22.         }
Parsed in 0.057 seconds, using GeSHi 1.0.8.4


- Nope.

Now I am still new to Java/Android and do not fully understand how to debug using my LogCat output.

When I try to bundle the variable, without getting the item postition, it works, but only display the name from the first database result (oldest call):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onListItemClick(ListView l, View v, int position, long id) {
  5.  
  6.                 super.onListItemClick(l, v, position, id);
  7.  
  8.  
  9.  
  10.                 Intent intent = new Intent(this, MyActivity.class);
  11.  
  12.                  Bundle b = new Bundle();
  13.  
  14.         b.putString("CallerName", callerName);
  15.  
  16.         intent.putExtras(b);
  17.  
  18.                 startActivity(intent);
  19.  
  20.         }
Parsed in 0.057 seconds, using GeSHi 1.0.8.4


So my issue definitely lies in my attempt to getItem(position) of the IconifiedTextListAdapter.

I've tried many things beyond whats posted above like closing the first cursor, trying to addExtra to the intent without a bundle. Nothing works.

Please, any help would be greatly appreciated.
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby iPaul Pro » Sun Apr 12, 2009 8:20 am

Is there anyone that can help me figure out how to correctly bundle cursor items from the IconifiedTextListAdapter?

I'd like to know how to pass the "cached name" of the selected list item to a sub-activity using an onListItemClick.

I have no problem bundling static string between activities. It is only when I try to pass the position of the list adapter that I run into issues.

The second activity could literally just display the bundled string(s) in a simple, un-styled text view.

Please, I have been stuck on this for days. Any help or links greatly appreciated.

Please..
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby iPaul Pro » Fri Apr 17, 2009 7:49 pm

I figured it out. This topic title should be changed to the "iPaul Pro Trial and Error Thread" :P

To pass the position of the cursor items from the IconifiedTextListAdapter to a sub-activity (Pass the chosen Name):

In CallLogDisplayer.java:

before OnCreate
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private Cursor c = null;
Parsed in 0.056 seconds, using GeSHi 1.0.8.4

(then remove the Cursor declaration at the query)

after OnCreate
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onListItemClick(ListView l, View v, int position, long id) {
  5.  
  6.                 super.onListItemClick(l, v, position, id);
  7.  
  8.                
  9.  
  10.                 c.moveToPosition(position);
  11.  
  12.                 Intent i = new Intent(this, SubActivity.class);
  13.  
  14.                 i.putExtra("CallerName", c.getString(nameColumn));
  15.  
  16.                 startActivity(i);
  17.  
  18.         }
Parsed in 0.058 seconds, using GeSHi 1.0.8.4


and in your SubActivity.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String name = getIntent().getStringExtra("CallerName");
Parsed in 0.058 seconds, using GeSHi 1.0.8.4


That's it! I was so close - but that's how these things go.

Feel free to ask for clarification.
- iPaul Pro
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

isFirst() is returning false always!!!!!!!!!!!

Postby sheshi85 » Wed Jun 10, 2009 11:30 am

I tried other methods like moveTofirst.but it is not showing the output in the emulator.can anyone please address this error!!!
thanks in advance!!!!!!!!
sheshi85
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jun 08, 2009 11:17 am

Top

Re: [VIDEO-Tut] - Querying and Displaying the CallLog

Postby chico » Thu Sep 17, 2009 9:27 pm

Hi Plusminus,
great tutorial,
I had one question
I want to only checked for the missed calls that has NOT yet been checked by the user (new missed calls). The way you have the code right now it will always count ALL the missed calls that are present in the call log. What I mean is if there is a missed call and I see a missed call icon on my status bar. Once I look at the call log , the status bar icon is cleared. But when we use the code provided by you it will still notify the already "viewed missed call" as missed call.

Thanks in advance for your help

Chico
chico
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Sep 17, 2009 9:20 pm

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: pskink and 2 guests