Getting started with Android: listview (simplecursoradapter implementation)

Source: Internet
Author: User

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";

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.