Android study notes 7

Source: Internet
Author: User

When you use the sqlite3 tool in the ADB shell to display Chinese characters, the Chinese characters displayed on the console are garbled because of the character set in cmd. solution,
In cmd, enter chcp 65001

① Using sqlitedatabase ② sqliteopenhelper ③ using contentprovider
④ Database file management

①:
Sqlitedatabase SQLite = openorcreatedatabase ("Diary. DB", mode, null );

Use cursor to bind listview data:
For example:
Sqlitedatabase SQLite = openorcreatedatabase ("Diary. DB", mode, null );
Cursor cur = SQLite. Query ("Diary", new string [] {"_ id", "title", "content "},
Null, null, null );
// Use cursor to bind data to listview
Simplecursoradapter curadapter = new simplecursoradapter (
This, Android. R. layout. simple_list_item_2,
Cur, new string [] {"title", "content "},
New int [] {Android. R. Id. text1, Android. R. Id. text2}
);
Diarylist. setadapter (curadapter );

The specified process is similar to simpleadapter;

Use of cursoradapter:
SQLite = openorcreatedatabase ("Diary. DB ",
Mode, null );
Cur = SQLite. Query ("Diary", new string [] {"_ id", "title", "content "},
Null, null, null );

Cursoradapter cadapter = new cursoradapter (this, cur ){

@ Override
Public void BindView (view, context, cursor ){

(Textview) view). settext (cursor. getstring (cursor. getcolumnindex ("title ")));
}

@ Override
Public View newview (context, cursor, viewgroup parent ){
Textview titlelabel = new textview (mainactivity. This );

Return titlelabel;
}

};
Simplecursoradapter, cursoradapter needs to bind a cursor

Database processed in sqliteopenhelper, switch, update
Sqlitedatabase is used for specific database operations

To obtain the sqliteopenhelper object, you need to write an extends sqliteopenhelper class, rewrite the constructor, oncreate, onupgrade method;

Database File Operations: directly specify the location of the database file/sdcard/databases/diary_db
In permission control, the full path must be used to obtain database files in another program;

File Permission: when the external permission is disclosed [rwx], the database file is disclosed;
If the file permission is not disclosed, but the class is in the same package path, the access page is unrestricted.

Contentprovider:
Write a class that inherits the contentprovider class. You need to override the actual method for database operations and configure the <provider/> component in the manifest file;
The value of Android: Name is the class name that inherits the contentprovider class. The Android: Authorities value can be specified at will. Currently, its subclass is placed in
In the same package as the Home Directory, when contentresolver is used to perform the CRUD operation, create a URI object. Content: // <Android: Authorities value>/table name/[#]
# Indicates the record. It can be a specific number that identifies the ID of the record;

 
Advantages: ① separation of business and database ② data sharing, mainly achieved through the authorization mechanism


Use a URI object in the activity to parse the URI address, corresponding to the contentprovider component of the registered provider tag in manifest, find the contentprovider subclass,
Get the getcontentresolver () method in the activity and get the contentresolver object. The commonly used contentprovider methods include intert (), delete (), query (),
Update (): when calling the corresponding contentprovider method, the corresponding method in the contentprovider subclass is actually used. The oncreate method is used to create sqlitedatabase.
Object.

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.