Generally, listview is used to display the list. Generally, the data in the list comes from the database. Therefore, we assume that a dbservice class has been implemented before, and pagecursorquery (INT offset, int length) exists in it );
For example, dbservice. pagecursorquery (3, 5); indicates that three records are skipped, five records are inserted, and cursor is returned;
The pagecursorquery implementation code is as follows. Note that,The primary key of the query statement must be renamed to "_ id". Otherwise, an error is returned.
public Cursor pageCursorQuery(int low,int high){db = helper.getWritableDatabase();Cursor cursor = db.query("person", new String[]{"id as _id","name","age"}, null, null, null, null, null,low+","+high);return cursor;}
Mainactivity. Java
package org.xiazdong.db;import org.xiazdong.db.service.DBService;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class MainActivity extends Activity {private ListView listView;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView)this.findViewById(R.id.listview); DBService service = new DBService(this); Cursor cursor = service.pageCursorQuery(0, 10); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item,cursor, new String[]{"_id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age}); listView.setAdapter(adapter);}}
Conclusion: The biggest difference between simplecursoradapter and simpleadapter is
(1) The former uses cursor to represent data, and the latter uses list <Map <string, Object> to represent data;
(2) The primary key of the query statement must be"_ Id";