SimpleCursorAdapter class and Data Binding

Source: Internet
Author: User

In many cases, you need to display data in database tables in components such as ListView and Gallery. Although the Adapter object can be processed directly, the workload is huge. Therefore, the Android SDK provides an Adapter class dedicated to Data Binding: SimpleCursorAdapter.
SimpleCursorAdapter is similar to SimpleAdapter in usage. You only need to replace the List object with the Cursor object. In addition, the fourth parameter from of the SimpleCursorAdapter class constructor is the field in the Cursor object, and the fourth parameter from of the SimpleAdapter class constructor is the key in the Map object. In addition, these two Adapter classes use the same method.
The following is the SimpleCursorAdapter class constructor definition.
Public SimpleCursorAdapter (Context context, int layout, Cursor c, String [] from, int [] );
In the following example, the SimpleCursorAdapter class is used to bind the database table to the ListView. That is to say, the ListView displays all records of the data table. Before Binding data, you must first compile a subclass of the SQLiteOpenHelper class to operate the database. The Code is as follows:
Java code
Package com. li;
 
Import java. util. Random;
 
Import android. content. Context;
Import android. database. Cursor;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
 
Public class DBService extends SQLiteOpenHelper {
 
Private final static int DATABASE_VERSION = 1;
Private final static String DATABASE_NAME = "test. db ";
 
Public DBService (Context context ){
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
}
 
@ Override
Public void onCreate (SQLiteDatabase db ){
// Create a table
String SQL = "CREATE TABLE [t_test] (" + "[_ id] AUTOINC ,"
+ "[Name] VARCHAR2 (20) not null conflict fail ,"
+ "CONSTRAINT [sqlite_autoindex_te_test_1] primary key ([_ id])";
Db.exe cSQL (SQL );
// Insert 20 records to the test Database
Random random = new Random ();
For (int I = 0; I <20; I ++)
{
String s = "";
For (int j = 0; j <10; j ++)
{
Char c = (char) (97 + random. nextInt (26 ));
S + = c;
}
Db.exe cSQL ("insert into t_test (name) values (?) ", New Object [] {s });

}
}
 
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
 
}
 
// Execute the query statement
Public Cursor query (String SQL, String [] args)
{
SQLiteDatabase db = this. getReadableDatabase ();
Cursor cursor = db. rawQuery (SQL, args );
Return cursor;
}
 
}


In this example, you do not need to upgrade test. db. Therefore, only the oncreate () method in the DBService class contains the code for creating a database table. DBService creates a test. db database file and a t_test table in the file. The table contains the _ id and name fields. Here, _ id is the auto-increment field and is the primary index.
Write the MapsDemo class below. The MapsDemo class is a subclass of ListActivity. Create a DBService object in the oncreate () method of the class, query all records in the t_test table through the query method of the DBService class, and return the Cursor object. The MapsDemo class code is as follows:

Java code
Package com. li;
Import android. app. ListActivity;
Import android. database. Cursor;
Import android. OS. Bundle;
Import android. widget. SimpleCursorAdapter;
Public class MapsDemo extends ListActivity {
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
DBService dbService = new DBService (this );
 
// Query data
Cursor cursor = dbService. query ("select * from t_test", null );
 
// Bind data
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter (this, android. r. layout. simple_expandable_list_item_1, cursor, new String [] {"name"}, new int [] {android. r. id. text1 });
SetListAdapter (simpleCursorAdapter );
}
 
}


The fourth parameter of the SimpleCursorAdapter class constructor indicates that the field name in the Cursor object is returned, and the fifth parameter indicates that the value of this field is to be assigned to that component. This component is defined in the layout file specified in the second parameter.

Note: When binding data, the record set returned by the Cursor object must contain a field named "_ id". Otherwise, data binding cannot be completed. That is to say, the SQL statement cannot be select name from t_contacts. If the "_ id" field does not exist in the data table, you can use other methods.
Someone may ask: where is the data stored? The system creates a database file in the/data/<package name>/databases directory in the mobile phone memory.

Author "myxuee"

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.