How to edit cursor / adapter entities?

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

How to edit cursor / adapter entities?

Postby worry » Fri Dec 26, 2008 11:23 pm

Lets say I have the following (simple People/Phones list):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.       Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
  3.  
  4.       startManagingCursor(c);
  5.  
  6.  
  7.  
  8.       adapter = new SimpleCursorAdapter(this,
  9.  
  10.               android.R.layout.simple_list_item_2, c,
  11.  
  12.                       new String[] { Phones.NAME, Phones.NUMBER },
  13.  
  14.                       new int[] { android.R.id.text1, android.R.id.text2 });
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Each item contain Name, Number.
What if I want to edit all numbers representation (for example format it from XXXXXXX to XXX-XX-XX)?

How can I edit entities in adapter or create new adapter based on current, but modified?

Thanks.
worry
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Nov 28, 2008 12:06 am

Top

Postby kodex83 » Sat Dec 27, 2008 1:45 pm

You can overwrite the SimpleCursorAdapter!
Or you can write your own adapter. A good example ist here: List4
Or the easiest way is to format your Phones.NUMBER String.

I think the best way is write your own adapter.
kodex83
Freshman
Freshman
 
Posts: 9
Joined: Wed Dec 03, 2008 6:00 pm
Location: Germay

Postby worry » Sat Dec 27, 2008 7:51 pm

>You can overwrite the SimpleCursorAdapter!
How? If you go to cursor doc
http://code.google.com/android/referenc ... ursor.html
it states "This interface provides random read-write access to the result set returned by a database query. "
And there are a whole bunch of Get* method (to read) and none Set* or smth (to write).

>Or the easiest way is to format your Phones.NUMBER String.

By this you mean to create function
String FormatNumber (String){..}
which return formated number and then use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. adapter = new SimpleCursorAdapter(this,
  2.               android.R.layout.simple_list_item_2, c,
  3.                       new String[] { Phones.NAME, FormatNumber(Phones.NUMBER) },
  4.                       new int[] { android.R.id.text1, android.R.id.text2 });
  5.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

But it is not working, because it is coloumn name, and not the column content itself


>I think the best way is write your own adapter.
I do not think that this is a case, because I want to introduce a minor change into a built-in adapter.

Thanks.
worry
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Nov 28, 2008 12:06 am

Postby Sparkletron » Thu Jan 08, 2009 5:14 pm

worry wrote:>You can overwrite the SimpleCursorAdapter!
How? If you go to cursor doc
http://code.google.com/android/referenc ... ursor.html
it states "This interface provides random read-write access to the result set returned by a database query. "
And there are a whole bunch of Get* method (to read) and none Set* or smth (to write).

By "overwrite" kodex means override one or more of SimpleCursorAdapter's methods to get the result you want. In your case you probably want to override bindView() or convertToString(). It really helps to have the source code of the class in question. I suggest you subclass SimpleCursorAdapter to make it easier.

-S
Sparkletron
Developer
Developer
 
Posts: 26
Joined: Tue Nov 04, 2008 6:17 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests