• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Need explanation of fillData() from notepad tutorrial

jms_gears1

Lurker
Jun 10, 2010
5
0
Ive been playing around with the notepad tutorial on the http://www.developer.android.com. Most of it i understand, well from the notepadv1 class, the only thing im really having trouble with is fillData().

the code is:

Code:
private void fillData() {
         //I understand that this gets all the notes and stores it in a cursor my main problem is i dont really understand what a cursor is
        mNotesCursor = mDbHelper.fetchAllNotes();

        //Since i dont know what a cursor is i have no idea what this does other than starts to manage this mysterious cursor
        startManagingCursor(mNotesCursor);
        
        //only thing i can seem to figure out about this line is that it puts the key for the title of the note into a string array, though i have no idea why
        String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

        //this looks like it does the same thing as above but it puts the value for text1 into an int array, again not sure why.
        int[] to = new int[]{R.id.text1};
        
        //these last two lines i have no clue at all whats going on here
        SimpleCursorAdapter notes = 
        	    new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
        setListAdapter(notes);
    }

so from going over that code sample the only thing i can figure out a cursor to be is another name for a table.

P.S. In case you didnt already figure it out, the comments are my thoughts or what i can figure out about the line of code.
 
A Cursor controls read/write access to a database. By calling (activity.)startManagingCursor, you are telling the activity to manage the lifecycle of the cursor. You don't need to worry too much about the details of this until you've worked directly with cursors some more, but it's a way of letting Android handle the resources involved with the cursor based on the current Activity.

The "String[]..." line is creating a string array that stores the columns that will be used (just the title, in this case).

The next is an integer array that is just storing the int reference to the view that is going to be used for displaying the data.

The last part is instantiating a SimpleCursorAdapter by passing it "this" (current context), the resource reference to the layout to use for putting the data into, the cursor (basically the connection to the db), the column to use in the database, the the int reference to the view to be used.

Then you set the list adapter to use. Think of this as a way of mapping columns from a database to views for laying out that data.
 
  • Like
Reactions: jms_gears1
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones