Android SQLite transaction processing combined with listview list display function example

This article gives an example of Android SQLite transaction processing combined with listview list display function. Share with you for your reference, as follows:

The previous article introduced the characteristics of transactions, such as atomicity, isolation, consistency and persistence. Let's talk about SQLite in combination with Android. This article will also combine listview. In fact, the transactions in Android are the same as those in our database. It is also used to open transactions, operate and commit transactions. Roll back if there is a problem.

The above code is a simple transaction operation. What we need to pay attention to is to catch exceptions, so that the transaction will be ended, which can save database resources.

The operation of transactions is like this. The following describes the use of listview, which can be understood as a list. The interface is as follows

We can split this interface into two. The main interface only has "user ID", "user name" and "user address", that is, the head of the list. The main interface is as follows

The listview here needs to define an ID for later data binding. The display interface containing content is also relatively simple, that is, several textviews

In this way, the part of the interface is OK, and then the data is read and displayed in listview. Here, two methods are provided to display the data

(1) Method 1

The simpleadapter is used for data binding here. The first thing we need to do is to take out the data one by one and store it in a HashMap, as shown below

The HashMap here stores generic data. The generics of this set cannot be modified arbitrarily. The next work is to treat this set as the generics of the list

Finally, remember to add this map to the collection

about

The "userid" and "address" in the fourth parameter are the keys in the map set. The last parameter is textview, that is, the textview in the data interface. A listener is added behind it. As long as you click textview, the user ID will be displayed, and Android will read the content through the location of textview.

Here, the code that reads the data first is posted first

(2) Cursor adapter

The following is the code for reading data

The alias for the primary key column is used here because Android recommends that the primary key be set to_ ID, but it is not possible that the name of the primary key of each table is_ id

The order of adapter parameters here is a little different from that above, and the "usernam", "address" and 'in the fourth parameter_ ID 'is the column name of the table. It doesn't make much difference in other places, and the "_id" above can't be written elsewhere. Otherwise, an error will occur

For more Android related content, readers who are interested can view the special topics of this site: summary of Android operating SQLite database skills, summary of Android database operating skills, summary of activity operating skills of Android programming, summary of Android file operating skills, introduction and advanced tutorial of Android development, summary of Android resource operating skills Android view skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>