ListActivity+sqlite+SimpleCursorAdapter简单实例

ListActivity+sqlite+SimpleCursorAdapter简单实例,第1张

概述下面是一个简单实例,主要演示ListActivity+sqlite+SimpleCursorAdapter的用法。 实例打包下载:http://download.csdn.net/detail/yang_hui1986527/4419708 DBHelper.java package com.example.sqlitedemo.db;import android.content.Contex

下面是一个简单实例,主要演示ListActivity+sqlite+SimpleCursorAdapter的用法。

实例打包下载:http://download.csdn.net/detail/yang_hui1986527/4419708


DBHelper.java

package com.example.sqlitedemo.db;import androID.content.Context;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;public class DBHelper extends sqliteOpenHelper {	/**	 * 数据库名称	 */	private static final String DATABASE_name = "test.db";  		/**	 * 数据库版本	 */	private static final int DATABASE_VERSION = 1;  	/**	 * 表格名称	 */	public static final String table_name = "profile";		/**	 * 列表一,_ID,自动增加	 */	public static final String ColUMN_ID = "_ID";		/**	 * 列表二,名称	 */	public static final String ColUMN_name = "name";		public DBHelper(Context context) {		super(context,DATABASE_name,null,DATABASE_VERSION);	}	@OverrIDe	public voID onCreate(sqliteDatabase db)  throws sqlException {		//创建表格		db.execsql("CREATE table IF NOT EXISTS "+ table_name + "("+ ColUMN_ID +" INTEGER PRIMARY KEY autoINCREMENT," + ColUMN_name +" VARCHAR NOT NulL);");	}	@OverrIDe	public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion)  throws sqlException {		//删除并创建表格		db.execsql("DROP table IF EXISTS "+ table_name+";");		onCreate(db);	}}

MainActivity.java
package com.example.sqlitedemo;import androID.app.ListActivity;import androID.content.ContentValues;import androID.database.Cursor;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.os.Bundle;import androID.vIEw.Menu;import androID.Widget.SimpleCursorAdapter;import com.example.sqlitedemo.db.DBHelper;public class MainActivity extends ListActivity {	private DBHelper helper = null;  	private sqliteDatabase db = null;  	private SimpleCursorAdapter adapter= null;	private Cursor mCursor = null;	@OverrIDe	public voID onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		initData();		initAdapter();	}	public voID initData(){		//获取数据库		helper = new DBHelper(this);		db = helper.getReadableDatabase();		//插入数据		try {			insert("zhang san");			insert("li si");			insert("wang wu");			insert("chen liu");			insert("zhang san");			insert("li si");			insert("wang wu");			insert("chen liu");		} catch (sqlException e) {			e.printstacktrace();		}	}		public voID initAdapter(){		//查询表格,并获得Cursor		mCursor = db.query(DBHelper.table_name,new String[]{DBHelper.ColUMN_ID,DBHelper.ColUMN_name},null);				//设置adapter		adapter = new SimpleCursorAdapter(this,androID.R.layout.simple_List_item_2,mCursor,new int[]{androID.R.ID.text1,androID.R.ID.text2});		setlistadapter(adapter);	}	public voID insert(String name) throws sqlException {		ContentValues values = new ContentValues();		values.put(DBHelper.ColUMN_name,name);		db.insertOrThrow(DBHelper.table_name,values);	}	@OverrIDe	public boolean onCreateOptionsMenu(Menu menu) {		getMenuInflater().inflate(R.menu.activity_main,menu);		return true;	}}


预览效果:

总结

以上是内存溢出为你收集整理的ListActivity+sqlite+SimpleCursorAdapter简单实例全部内容,希望文章能够帮你解决ListActivity+sqlite+SimpleCursorAdapter简单实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1175590.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存