Use of cursor

Source: Internet
Author: User

The query results are returned when you query the database.Subclass object encapsulated in a cursor. Cursor Is like a cursor located above the result set.Result setForward, backward, or random access. Cursor itself is an interface class that provides some abstract methods for access to the result set. It has different implementations in its subclass according to different functions. To control the cursor type returned during a query, you can customize a method that inherits from the cursorfactory class to return the required cursor subclass object by implementing its newcursor () method, however, by default, when cursorfactory imports null, the query operation returns an object of sqlitecursor that points to the first row of data.

Describes some common methods in cursor.

 

Common cursor-related methods

In actual application programming, the cursor is linked to the adapter control through the adapter. Android provides an abstract class cursoradapter for cursor to facilitate connection between cursor and the adapter. You only need to create a class inherited from cursoradapter to implement the two Abstract METHODS: BindView () and newview, or you can use this class to construct an adapter that can adapt to cursor by implementing other methods as needed. The following describes the implementation of the BindView () and newview () abstract methods.

  • Public void BindView (view, context, cursor)
    Reuse an existing view to display the data pointed to by the current cursor.
  • Public View newview (context, cursor, viewgroup parent)
    Create a view object for the data pointed to by cursor and display its data.

Note that the cursor result set passed into the cursoradapter must contain a column named _ id. Otherwise, the cursoradapter will not work.

What is the role of adapter? I think you can see it at a glance after reading the original text in the android SDK:
An adapter object acts as a bridge between an adapterview and the underlying data for that view. the adapter provides access to the data items. the adapter is also responsible for making a view for each item in the data set.

We have already introduced several adapters that have been implemented by Android. simplecursoradapter is a convenient adapter class specifically implemented for the cursor object. The following describes the input parameters of its constructor:

Public simplecursoradapter (context contex, int layout, cursor C, string [] From, int [])

  • ContextThe context object of the current program.
  • LayoutIs used to describe how to display the r class reference of the layout file on the adapter control.
  • From, A string array consisting of the column names to be displayed.
  • To, Which is an integer array consisting of the IDs of the child controls in the layout file specified by layout, which corresponds to from.
Based on the preceding descriptions, the following is an example:

Variable to reference in the program:
Textview display;
Spinner S;
...

Code for filling in the spinner control:
S = (spinner) findviewbyid (R. Id. spinner );
Display = (textview) findviewbyid (R. Id. Display );
SimplecursoradapterAdapter = newSimplecursoradapter(This,
Android. R. layout. simple_spinner_item,
C,
New String [] {myhelper. Country },
New int [] {Android. R. Id. text1 });
Adapter.Setdropdownviewresource(Android. R. layout. simple_spinner_dropdown_item );
S.Setadapter(Adapter );
S.Setonitemselectedlistener(New onitemselectedlistener ()
{
Public void onitemselected (adapterview <?> Adapter, view V, int POs, long ID)
{
C. movetoposition (POS );
Display. settext (C. getstring (codeindex ));
}
Public void onnothingselected (adapterview <?> Arg0 ){}
});

Obtain the references of textview and spinner objects by ID respectively, and then construct a simplecursoradapter using the layout of the previously returned cursor and the self-contained spinner sub-Control of Android, and then call setdrop-downviewresource () to display the layout of the subcontrol after clicking on the spinner. Finally, set the adapter of the spinner as the adapter and set a listener for the selected events of its subcontrol. Note that the spinner does not support setting the Click Event listener (onclicklistener). If it is set forcibly, an exception is thrown. The final program running result is shown in:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.