[solved] Strings and Cursors

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

[solved] Strings and Cursors

Postby hellshady00 » Fri Dec 25, 2009 12:30 am

Well, im probably trying to do something really noobish here because java definitely isnt my thing, but im not too terrible with C++ so i get the basics....

Anyway, i've started working on rewriting the android phone app using the public APIs (lots of reasons) and yea i know this is a big project for a beginner, but atleast i'll learn a few things.

Ok, heres the code i've got for showing the call log

Code: Select all
       ListView CallLogListView = (ListView)findViewById(R.id.CallLogListView);
       
       Cursor c = getContentResolver().query(
             android.provider.CallLog.Calls.CONTENT_URI,
             null, null, null,
             android.provider.CallLog.Calls.DATE+ " DESC"
       );
       
       startManagingCursor(c);
       
       SimpleCursorAdapter calls = new SimpleCursorAdapter(
                this,
                 R.layout.call_log,
                 c,
                 new String[] {
                      android.provider.CallLog.Calls.CACHED_NAME,
                      android.provider.CallLog.Calls.NUMBER
                      },
                 new int[] {
                      R.id.callerName,
                      R.id.callerNumber
                      });
       
       
       CallLogListView.setAdapter(calls);


And that works just fine, but theres a couple reasons i need to do some basic string operations here -
1) If theres no CACHED_NAME i need to display something like "Unknown Caller"
2) Add hyphens to the phone number (maybe theres a better way to do that)


Doing any kind of string operations like this when defining the SimpleCursorAdapter causes a force close
Code: Select all
       SimpleCursorAdapter calls = new SimpleCursorAdapter(
                this,
                 R.layout.call_log,
                 c,
                 new String[] {
                      android.provider.CallLog.Calls.CACHED_NAME.concat("Test"),
                      android.provider.CallLog.Calls.NUMBER
                      },
                 new int[] {
                      R.id.callerName,
                      R.id.callerNumber
                      });



And if i try to do something like
Code: Select all
if(android.provider.CallLog.Calls.CACHED_NAME == " ") {
    android.provider.CallLog.Calls.CACHED_NAME = "Unknown Caller"
}

I get an error - "The final field CallLog.Calls.CACHED_NAME cannot be assigned"

So, how do i dynamically get the field from the cursor and perform string functions that way?
Maybe theres a tutorial for this, or just a function im missing...not sure what to look for.

If anyone can give me a little tip on how to handle this or maybe point me towards a tutorial that talks about this, that would be great. Thanks in advance :)
Last edited by hellshady00 on Fri Dec 25, 2009 11:45 pm, edited 1 time in total.
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Top

Postby qlimax » Fri Dec 25, 2009 5:23 pm

Hi, and merry Xmas

I suggest you to write your own cursoradapter (extending cursoradapter class).
Then ,you can do your filtering operations in the bindwiew method-

http://blog.cluepusher.dk/2009/11/16/cr ... r-android/


http://almondmendoza.com/2009/08/10/cus ... n-android/


another way is to extend directly the baseadapter class... but the above solution should go well for you...

bye

:xmas:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby hellshady00 » Fri Dec 25, 2009 9:16 pm

Thanks alot! worked like a charm :)
merry Xmas to you too
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests