Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Cursor developed by Android

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Cursor for Android development". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Cursor for Android development".

The code to get data from Cursor is roughly as follows:

String uuidString = cursor.getString (

Cursor.getColumnIndex (CrimeTable.Cols.UUID))

String title = cursor.getString (

Cursor.getColumnIndex (CrimeTable.Cols.TITLE))

Long date = cursor.getLong (

Cursor.getColumnIndex (CrimeTable.Cols.DATE))

Int isSolved = cursor.getInt (

Cursor.getColumnIndex (CrimeTable.Cols.SOLVED))

Each time a crime record is taken out of Cursor, the above code is repeated. (this does not include the code to create Crime instances based on these field values.)

Obviously, in this case, we should consider the code reuse principle mentioned earlier. Instead of writing repetitive code mechanically

It is better to create a reusable, dedicated Cursor subclass. Using CursorWrapper, you can quickly and easily create Cursor subclasses. As the name implies, CursorWrapper can encapsulate Cursor objects and allow new useful methods to be added to them.

Refer to the following code to create a new CrimeCursorWrapper class in the database package.

Create a CrimeCursorWrapper class (CrimeCursorWrapper.java)

Public class CrimeCursorWrapper extends CursorWrapper {

Public CrimeCursorWrapper (Cursor cursor) {

Super (cursor)

}

}

As you can see, the above code creates a Cursor wrapper class. This class inherits all the methods of the Cursor class. Note that the purpose of this encapsulation is to customize the new method to facilitate the manipulation of the internal Cursor.

Referring to the following code, add a new getCrime () method to get the value of the relevant field.

New getCrime () method (CrimeCursorWrapper.java)

Public class CrimeCursorWrapper extends CursorWrapper {

Public CrimeCursorWrapper (Cursor cursor) {

Super (cursor)

}

Public Crime getCrime () {

String uuidString = getString (getColumnIndex (CrimeTable.Cols.UUID))

String title = getString (getColumnIndex (CrimeTable.Cols.TITLE))

Long date = getLong (getColumnIndex (CrimeTable.Cols.DATE))

Int isSolved = getInt (getColumnIndex (CrimeTable.Cols.SOLVED))

Return null

}

}

We need to return the Crime with UUID. Add a constructor to Crime.java for this purpose, as shown in the following code.

New Crime constructor (Crime.java)

Public Crime () {

This (UUID.randomUUID ())

MId = UUID.randomUUID ()

MDate = new Date ()

}

Public Crime (UUID id) {

MId = id

MDate = new Date ()

}

Finally, complete the getCrime () method, with the following code

New getCrime () method (CrimeCursorWrapper.java)

Public Crime getCrime () {

String uuidString = getString (getColumnIndex (CrimeTable.Cols.UUID))

String title = getString (getColumnIndex (CrimeTable.Cols.TITLE))

Long date = getLong (getColumnIndex (CrimeTable.Cols.DATE))

Int isSolved = getInt (getColumnIndex (CrimeTable.Cols.SOLVED))

Crime crime = new Crime (UUID.fromString (uuidString))

Crime.setTitle (title)

Crime.setDate (new Date (date))

Crime.setSolved (isSolved! = 0)

Return crime

Return null

}

(Android Studio will let you decide whether to choose java.util.Date or java.sql.Date. Make no mistake, even if we are writing database-related code, we should choose java.util.Date.)

Thank you for reading, the above is the content of "how to use Cursor for Android development". After the study of this article, I believe you have a deeper understanding of how to use Cursor for Android development, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report