Listview and SQLite are combined to realize Notepad function

Android Notepad's demo is searched a lot on the Internet, but the demo written by the great God often has too many functions, which makes it difficult for novices to start and bite; The demo written by some novices is often patchwork. Many of the code is copied by others and is directly used in the project. I don't know what the code does. Often the code is particularly ugly, and there are more repetitive codes.

The author recently learned here, and after understanding it, he still plans to write a demo for novices to learn. The code is not elegant, but in my opinion, I have tried my best to make it easy to understand. (the source code is at the end of the article)

In order to facilitate novice learning, here are also the knowledge points involved: 1. Basic use of SQLite, addition, deletion, query and modification 2. Listview, basic use of adapeter 3. Activity life cycle 4. Transfer parameters of intent and bundle 5. Basic use of alertdialog

In addition, there are some bits and pieces of knowledge that can be found in Baidu.

Problems encountered:

One problem with SQLite is that the primary key cannot be sorted automatically. For example, there are 4 records with a primary key ID of 1 2 3 4. Delete 2, 3, and 1, 4 records. When you insert them again, the ID will become 5 instead of 2. Suppose you delete all four records on the basis of the initial four records. When you insert them again, the ID will be 5. The author also spent a long time on this. Originally, in order to simplify the code, the idea was to use arg2 in listview to operate directly through the ID recorded in the database, However, due to the problem of SQLite, this method has a problem. Finally, the author uses the content search method, obtains the content from each item of listview, then searches the record through the content in the database, and finally operates it.

effect:

MainActivity:

NoteDateBaseHelper:

noteEdit:

activity_ main:

edit:

item:

Finally, attach the source code: Notepad

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>