Need explanation of fillData() from notepad tutorrial


Last Updated:

  1. jms_gears1

    jms_gears1 Member This Topic's Starter

    Joined:
    Jun 10, 2010
    Messages:
    5
    Likes Received:
    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 (Text):
    1. private void fillData() {
    2.          //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
    3.         mNotesCursor = mDbHelper.fetchAllNotes();
    4.  
    5.         //Since i dont know what a cursor is i have no idea what this does other than starts to manage this mysterious cursor
    6.         startManagingCursor(mNotesCursor);
    7.        
    8.         //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
    9.         String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
    10.  
    11.         //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.
    12.         int[] to = new int[]{R.id.text1};
    13.        
    14.         //these last two lines i have no clue at all whats going on here
    15.         SimpleCursorAdapter notes =
    16.                 new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
    17.         setListAdapter(notes);
    18.     }
    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.
     

    Advertisement
  2. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    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.
     
    jms_gears1 likes this.
  3. jms_gears1

    jms_gears1 Member This Topic's Starter

    Joined:
    Jun 10, 2010
    Messages:
    5
    Likes Received:
    0
    ahhh hmm well thank you.
    i guess i continue with the note pad tutorial now, i just didnt want to go anywhere further until i could understand this.
     

Share This Page

Loading...