Hello!

I want to write a ListAdapter to serve data from a database that look like 
this:

CREATE TABLE notes (_id integer primary key, 
                    content text);
CREATE TABLE tags (_id integer primary key,
                   name text,
                   pos integer
                   noteid integer,
                   foreign key(noteid) references notes(_id));

I use this database to store notes and their associated tags. One 
requirement of the ListAdapter is that it must be able to update the 
ListView contents if the underlying data changes. I can query all the notes 
in the database with this query:

select notes._id as id, notes.content, tags.pos, tags.name 
  from notes left join tags on id = tags.noteid
  order by id, tags.pos;

Which will give me results looking like this (null values shown for 
clarity):

0|foo bar baz|0|x
1|hello world|null|null
2|one more nn|0|yy
2|one more nn|1|y

As you can see, note with mote than one tag will be located on more than one 
row in the result. This means that I can't look at the cursor size to 
determine the number of notes, I need to traverse the entire Cursor to get a 
count. I don't want to do that.

The solution I have come up with so far is to use two cursors: one with the 
query mentioned above and one with a query containing the number of rows in 
the notes table (select count(*) from notes;). In the constructor I call 
intializeCursorsI():

    private void initializeCursors() {
        notesCursor.moveToFirst();
        countCursor.moveToFirst();
        count = countCursor.getInt(0);
    }

I have implemented  getItem() like this:

    public Note getItem(int position) {
        if (position < notes.size()) { // notes is a List of notes that we 
have already read.
            return notes.get(position);
        }

        int cursorPosition = notes.size();
        while (cursorPosition <= position) {
            Note note = NotesDb.noteFrom(notesCursor); // Creates a note by 
reading the correct number of rows.
            notes.add(note);
            ++cursorPosition;
        }
        return notes.get(position);
    }

The adapter assumes that the cursors are being managed by some activity that 
has called startManagingCursor() on them.

So far so good, I guess. The problem is now how to handle the cursor being 
requeried. Since I have two cursors I need to register listeners for both of 
them and when I have received onChange() for both of them I can 
initializeCursors() and notify any listeners registered to my ListAdapter of 
a change in the its data.

This is the best I have so far. I want to check the sanity of this approach 
with this group. :-) Is this way to complicated? Perhaps I have missed some 
part of the API that solves this nicely for me?

Thanks in advance!

:.:: mattias

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to