0

Currently I am displaying data from an SQLite database in a multiple-column ListView. However, I would like to use calculated columns in my data involving more complicated mathematical functions than those available in SQLite queries. In this thread the following advice was given for this sort of situation:

... if you are putting the Cursor into some sort of CursorAdapter, you could: (1) convert the Cursor into an ArrayList, where Position is some Java class you define with your data (2) close the Cursor, to release the RAM it takes up (3) sort the ArrayList using Arrays.sort() (4) wrap the ArrayList in an ArrayAdapter and use that where you had been using your CursorAdapter

My problem is that I get stuck at step (4), because ArrayAdapter seems to be less flexible than SimpleCursorAdapter. I have been using SimpleCursorAdapter to define the mapping from the database columns to the ListView, but there seems to be no equivalent method defined for an ArrayAdapter.

I have seen references on the web to an Android object called ArrayListCursor. This sounds as if it would do just what I want, but it does not appear in the current Android Reference and it doesn't seem to be recognised by Eclipse.

If ArrayListCursor has been superseded what has replaced it?

1 Answer 1

1

I do not remember ever seeing an ArrayListCursor. There is a MatrixCursor, which allows you to build up a Cursor from rows and cells. You can also implement AbstractCursor to do whatever you want.

Whatever you do, try to minimize the data copying you do, which is why I would recommend either:

  • Creating your own AbstractCursor subclass that wraps your database Cursor and blends in the calculated values
  • Just leave your original Cursor alone and use a CursorAdapter subclass to blend in your calculated values
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks very much for your prompt answer. For my limited skill level I don't think I can tackle your suggestions without a fully worked out example, and I've not been able to find one. Reading the Android reference for Cursor, which specifically says that a Cursor provides access to the results of a database query, made me think of a workaround: why not use my cursor to read my database query line-by-line and write the data to a temporary SQLite table along with the calculated columns, then define a new cursor based on a query that returns everything in the temporary table?
@prepbgg: What you are describing is more difficult to implement than are my suggestions above, and slower as well. Even the MatrixCursor option is better than that.
Yes, but I know how to do what I describe. I recognise it would be inefficient but that's not a big issue in the particular context. I don't know how to "create an AbstractCursor subclass that wraps my database Cursor" or to "use a CursorAdapter subclass". However, I'm always keen to try to get to grips with new concepts. Can you point to any tutorials, please?
The tutorials at thinkandroid.wordpress.com/2010/01/09/… and thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters look as if they may show me how to proceed. I'll have a go!
No, I'm afraid thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters has not helped me. He just seems to use the fields from the underlying database so the example doesn't show me how to modify the data or add new fields. Any other suggestions, please?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.