What form / format contains the data in Cursor 'e?

    2 answers 2

    The data in the Cursor class is in a format that is obtained from the database - INTEGER TEXT DATE, and so on. , and in fact, apparently, in text, numeric or byte form, as a certain set of characters \ codes depending on the type. Learn more about SQLite types and their presentation.

    You can extract them and convert them to Java types using the Cursor class methods, for example:
    cursor.getInt() - returns the number of integer type int
    cursor.getFloat() - returns a float real number
    cursor.getString() will return a string

    In this case, the data extracted from the database columns must be of a comparable type, that is, you cannot get the number int from the TEXT field (with text content) and get an exception, but you can extract data from the INTEGER field with a number as a string. will not be a number with which it is possible to perform arithmetic operations, but a string with text in the form of numbers.

    • Thanks to your answer, that “they are in the format obtained from the database”, I noticed that I did not accurately formulate the question, therefore I will note your answer. But still, is the Cursor'a structure Cursor'a a real array? More precisely, from which it inherits / implements its structure, in which data is stored as it is obtained from the database? - TimurVI
    • It seems rather difficult to understand this, since the source code of related classes goes into the calls of native procedures, and Google itself doesn’t specifically cover the internal structure of this class and its data. Maybe if you say why you know this, then you will get a more satisfactory answer. - pavlofff
    • Banal curiosity. - TimurVI

    Cursor should be perceived as a Set for Bd. It contains everything that you put exactly there and then you extract everything from there through the CursorAdapter. Here is some example.

    Creating a view template

     <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tvBody" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Study cursors" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tvPriority" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="3" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> 

    Adapter definition

     public class TodoCursorAdapter extends CursorAdapter { public TodoCursorAdapter(Context context, Cursor cursor) { super(context, cursor, 0); } // The newView method is used to inflate a new view and return it, // you don't bind any data to the view at this point. @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.item_todo, parent, false); } // The bindView method is used to bind all data to a given view // such as setting the text on a TextView. @Override public void bindView(View view, Context context, Cursor cursor) { // Find fields to populate in inflated template TextView tvBody = (TextView) view.findViewById(R.id.tvBody); TextView tvPriority = (TextView) view.findViewById(R.id.tvPriority); // Extract properties from cursor String body = cursor.getString(cursor.getColumnIndexOrThrow("body")); int priority = cursor.getInt(cursor.getColumnIndexOrThrow("priority")); // Populate fields with extracted properties tvBody.setText(body); tvPriority.setText(String.valueOf(priority)); } } 

    Retrieving the cursor

     // TodoDatabaseHandler is a SQLiteOpenHelper class connecting to SQLite TodoDatabaseHandler handler = new TodoDatabaseHandler(this); // Get access to the underlying writeable database SQLiteDatabase db = handler.getWritableDatabase(); // Query for items from the database and get a cursor back Cursor todoCursor = db.rawQuery("SELECT * FROM todo_items", null); 

    Adapter connection to ListView

     // Find ListView to populate ListView lvItems = (ListView) findViewById(R.id.lvItems); // Setup cursor adapter using cursor from last step TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor); // Attach cursor adapter to the ListView lvItems.setAdapter(todoAdapter); 

    CursorAdapter

      // Переключение на новый курсор и обновления содержимого ListView todoAdapter . changeCursor (newCursor);