Project——SQLite数据库 *** 作(采用ListView实现数据列表展现)

Project——SQLite数据库 *** 作(采用ListView实现数据列表展现),第1张

概述1、 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="cn.itcast.db"       android:versionCode="1"       android:versionName="1.0

1、

<?xml version="1.0" enCoding="utf-8"?>
<manifest xmlns:androID="http://schemas.android.com/apk/res/android"
package="cn.itcast.db"
androID:versionCode="1"
androID:versionname="1.0">
<application androID:icon="@drawable/icon" androID:label="@string/app_name">
<uses-library androID:name="androID.test.runner" />
<activity androID:name=".MainActivity"
androID:label="@string/app_name">
<intent-filter>
<action androID:name="androID.intent.action.MAIN" />
<category androID:name="androID.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provIDer androID:name=".PersonContentProvIDer" androID:authoritIEs="cn.itcast.provIDes.personprovIDer"/>
</application>
<uses-sdk androID:minSdkVersion="8" />
<instrumentation androID:name="androID.test.InstrumentationTestRunner"
androID:targetPackage="cn.itcast.db" androID:label="Tests for My App" />
</manifest>

2、

package cn.itcast.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.itcast.domain.Person;
import cn.itcast.service.PersonService;
import androID.app.Activity;
import androID.content.ContentResolver;
import androID.content.ContentValues;
import androID.database.Cursor;
import androID.net.Uri;
import androID.os.Bundle;
import androID.util.Log;
import androID.vIEw.VIEw;
import androID.Widget.AdapterVIEw;
import androID.Widget.button;
import androID.Widget.ListVIEw;
import androID.Widget.SimpleAdapter;
import androID.Widget.SimpleCursorAdapter;
import androID.Widget.Toast;
import androID.Widget.AdapterVIEw.OnItemClickListener;

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private ListVIEw ListVIEw;
private PersonService service;

@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);

button button = (button)this.findVIEwByID(R.ID.button);
button.setonClickListener(new VIEw.OnClickListener() {
@OverrIDe
public voID onClick(VIEw v) {
ContentResolver contentResolver = getContentResolver();
Uri insertUri = Uri.parse("content://cn.itcast.provIDes.personprovIDer/person");
ContentValues values = new ContentValues();
values.put("name","laofang");
values.put("phone","13800029333");
values.put("amount","1000");
contentResolver.insert(insertUri,values);
}
});

ListVIEw = (ListVIEw)this.findVIEwByID(R.ID.ListVIEw);
service = new PersonService(this);
/* List<Person> persons = service.getScrollData(0,5);
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person : persons){
HashMap<String,Object> item = new HashMap<String,Object>();
item.put("name",person.getname());
item.put("phone",person.getPhone());
item.put("amount",person.getAmount());
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.item,
new String[]{"name","phone","amount"},new int[]{R.ID.name,R.ID.phone,R.ID.amount});
ListVIEw.setAdapter(adapter);

ListVIEw.setonItemClickListener(new ItemClickListener());
*/
Cursor cursor = service.getCursorScrollData(0,5);
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,cursor,R.ID.amount});
ListVIEw.setAdapter(cursorAdapter);
ListVIEw.setonItemClickListener(new ItemClickListener());
}

private final class ItemClickListener implements OnItemClickListener{
@OverrIDe
public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {
ListVIEw lVIEw = (ListVIEw)parent;
/*HashMap<String,Object> item = (HashMap<String,Object>)lVIEw.getItemAtposition(position);
Log.i(TAG,item.get("name").toString());
Toast.makeText(MainActivity.this,item.get("name").toString(),1).show();*/
Cursor cursor = (Cursor)lVIEw.getItemAtposition(position);
String name = cursor.getString(cursor.getColumnIndex("name"));
Toast.makeText(MainActivity.this,name,1).show();
}

}
}

3、

package cn.itcast.db;

import cn.itcast.service.DBOpenHelper;
import androID.content.ContentProvIDer;
import androID.content.ContentUris;
import androID.content.ContentValues;
import androID.content.UriMatcher;
import androID.database.Cursor;
import androID.database.sqlite.sqliteDatabase;
import androID.net.Uri;

public class PersonContentProvIDer extends ContentProvIDer {
private static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
private static final int PERSON = 2;
private DBOpenHelper dbOpenHelper;

static{
sMatcher.addURI("cn.itcast.provIDes.personprovIDer","person",PERSONS);
sMatcher.addURI("cn.itcast.provIDes.personprovIDer","person/#",PERSON);
}
// content://cn.itcast.provIDes.personprovIDer/person
@OverrIDe
public Uri insert(Uri uri,ContentValues values) {
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
switch (sMatcher.match(uri)) {
case PERSONS:
long rowID = db.insert("person","name",values);
getContext().getContentResolver().notifyChange(uri,null);
return ContentUris.withAppendedID(uri,rowID);
default:
throw new IllegalArgumentException("UnkNown Uri:"+ uri);
}
}
// content://cn.itcast.provIDes.personprovIDer/person 删除表中的所有记录
// content://cn.itcast.provIDes.personprovIDer/person/10 删除表中指定ID的记录
@OverrIDe
public int delete(Uri uri,String selection,String[] selectionArgs) {
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0;
switch (sMatcher.match(uri)) {
case PERSONS:
num = db.delete("person",selection,selectionArgs);
break;
case PERSON:
long personID = ContentUris.parseID(uri);
String where = "personID="+ personID;
if(selection!=null && !"".equals(selection)){
where = where + " and "+ selection;
}
num = db.delete("person",where,selectionArgs);
break;
default:
throw new IllegalArgumentException("UnkNown Uri:"+ uri);
}
getContext().getContentResolver().notifyChange(uri,null);
return num;
}

@OverrIDe
public String getType(Uri uri) {// gif image/gif text/plain
switch (sMatcher.match(uri)) {
case PERSONS:
return "vnd.androID.cursor.dir/person";
case PERSON:
return "vnd.androID.cursor.item/person";
default:
throw new IllegalArgumentException("UnkNown Uri:"+ uri);
}
}


@OverrIDe
public boolean onCreate() {
this.dbOpenHelper = new DBOpenHelper(this.getContext());
return true;
}

// content://cn.itcast.provIDes.personprovIDer/person 获取表中的所有记录
// content://cn.itcast.provIDes.personprovIDer/person/10 获取表中指定ID的记录
@OverrIDe
public Cursor query(Uri uri,String[] projection,
String[] selectionArgs,String sortOrder) {
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
switch (sMatcher.match(uri)) {
case PERSONS:
return db.query("person",projection,selectionArgs,null,sortOrder);

case PERSON:
long personID = ContentUris.parseID(uri);
String where = "personID="+ personID;
if(selection!=null && !"".equals(selection)){
where = where + " and "+ selection;
}
return db.query("person",sortOrder);

default:
throw new IllegalArgumentException("UnkNown Uri:"+ uri);
}
}
// content://cn.itcast.provIDes.personprovIDer/person 更新表中的所有记录
// content://cn.itcast.provIDes.personprovIDer/person/10 更新表中指定ID的记录
@OverrIDe
public int update(Uri uri,ContentValues values,String[] selectionArgs) {
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0;
switch (sMatcher.match(uri)) {
case PERSONS:
num = db.update("person",values,selectionArgs);
break;
case PERSON:
long personID = ContentUris.parseID(uri);
String where = "personID="+ personID;
if(selection!=null && !"".equals(selection)){
where = where + " and "+ selection;
}
num = db.update("person",null);
return num;
}

}

4、

package cn.itcast.db;

import java.util.List;

import cn.itcast.domain.Person;
import cn.itcast.service.OtherPersonService;
import androID.test.AndroIDTestCase;
import androID.util.Log;

public class OtherPersonServiceTest extends AndroIDTestCase {
private static final String TAG = "PersonServiceTest";

public voID testSave() throws Throwable{
OtherPersonService service = new OtherPersonService(this.getContext());
Person person = new Person();
person.setname("zhangxiaoxiao");
person.setPhone("13671555567");
service.save(person);

Person person2 = new Person();
person2.setname("laobi");
person2.setPhone("13679993567");
service.save(person2);

Person person3 = new Person();
person3.setname("lili");
person3.setPhone("13888323567");
service.save(person3);

Person person4 = new Person();
person4.setname("zhaoxiaogang");
person4.setPhone("1367132300");
service.save(person4);
}

public voID testFind() throws Throwable{
OtherPersonService service = new OtherPersonService(this.getContext());
Person person = service.find(1);
Log.i(TAG,person.toString());
}

public voID testUpdate() throws Throwable{
OtherPersonService service = new OtherPersonService(this.getContext());
Person person = service.find(1);
person.setname("liming");
service.update(person);
}

public voID testCount() throws Throwable{
OtherPersonService service = new OtherPersonService(this.getContext());
Log.i(TAG,service.getCount()+"");
}

public voID testScrollData() throws Throwable{
OtherPersonService service = new OtherPersonService(this.getContext());
List<Person> persons = service.getScrollData(0,3);
for(Person person : persons){
Log.i(TAG,person.toString());
}
}

public voID testDelete() throws Throwable{
OtherPersonService service = new OtherPersonService(this.getContext());
service.delete(1);
}
}

5、

package cn.itcast.db;

import java.util.List;

import cn.itcast.domain.Person;
import cn.itcast.service.DBOpenHelper;
import cn.itcast.service.PersonService;
import androID.test.AndroIDTestCase;
import androID.util.Log;

public class PersonServiceTest extends AndroIDTestCase {
private static final String TAG = "PersonServiceTest";

public voID testDBCreate() throws Throwable{
DBOpenHelper dbOpenHelper = new DBOpenHelper(this.getContext());
dbOpenHelper.getWritableDatabase();
}

public voID testSave() throws Throwable{
PersonService service = new PersonService(this.getContext());
Person person = new Person();
person.setname("zhangxiaoxiao");
person.setPhone("13671555567");
person.setAmount(100);
service.save(person);

Person person2 = new Person();
person2.setname("laobi");
person2.setPhone("13679993567");
person2.setAmount(50);
service.save(person2);

Person person3 = new Person();
person3.setname("lili");
person3.setPhone("13888323567");
person3.setAmount(80);
service.save(person3);

Person person4 = new Person();
person4.setname("zhaoxiaogang");
person4.setPhone("1367132300");
person4.setAmount(90);
service.save(person4);
}

public voID testFind() throws Throwable{
PersonService service = new PersonService(this.getContext());
Person person = service.find(1);
Log.i(TAG,person.toString());
}

public voID testUpdate() throws Throwable{
PersonService service = new PersonService(this.getContext());
Person person = service.find(1);
person.setname("liming");
service.update(person);
}

public voID testCount() throws Throwable{
PersonService service = new PersonService(this.getContext());
Log.i(TAG,service.getCount()+"");
}

public voID testScrollData() throws Throwable{
PersonService service = new PersonService(this.getContext());
List<Person> persons = service.getScrollData(0,person.toString());
}
}

public voID testDelete() throws Throwable{
PersonService service = new PersonService(this.getContext());
service.delete(1);
}

public voID testPayment() throws Throwable{
PersonService service = new PersonService(this.getContext());
service.payment();
}

}

6、

package cn.itcast.domain;

public class Person {
private Integer ID;
private String name;
private String phone;
private Integer amount;

public Integer getAmount() {
return amount;
}

public voID setAmount(Integer amount) {
this.amount = amount;
}

public Person(){}

public Person(Integer ID,String name,String phone,Integer amount) {
this.ID = ID;
this.name = name;
this.phone = phone;
this.amount = amount;
}

public Person(Integer ID,String phone) {
this.ID = ID;
this.name = name;
this.phone = phone;
}
public Integer getID() {
return ID;
}
public voID setID(Integer ID) {
this.ID = ID;
}
public String getname() {
return name;
}
public voID setname(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public voID setPhone(String phone) {
this.phone = phone;
}

@OverrIDe
public String toString() {
return "Person [amount=" + amount + ",ID=" + ID + ",name=" + name
+ ",phone=" + phone + "]";
}



}

7、

package cn.itcast.service;

import androID.content.Context;
import androID.database.sqlite.sqliteDatabase;
import androID.database.sqlite.sqliteOpenHelper;

public class DBOpenHelper extends sqliteOpenHelper {

public DBOpenHelper(Context context) {
super(context,"itcast.db",2);
}

@OverrIDe
public voID onCreate(sqliteDatabase db) {
db.execsql("CREATE table person (personID integer primary key autoincrement,name varchar(20))");
}

@OverrIDe
public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
db.execsql("ALTER table person ADD phone VARCHAR(12) NulL");
db.execsql("ALTER table person ADD amount integer");
}

}

8、

package cn.itcast.service;

import java.util.ArrayList;
import java.util.List;

import androID.content.Context;
import androID.database.Cursor;
import androID.database.sqlite.sqliteDatabase;

import cn.itcast.domain.Person;

public class PersonService {
private DBOpenHelper dbOpenHelper;

public PersonService(Context context){
dbOpenHelper = new DBOpenHelper(context);
}

public voID payment(){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
db.beginTransaction();//开启事务
try{
db.execsql("update person set amount=amount-10 where personID=?",new Object[]{1});
db.execsql("update person set amount=amount+10 where personID=?",new Object[]{2});
db.setTransactionSuccessful();//设置事务标志为成功,在结束事务时才会提供事务,否则回滚事务
}finally{
db.endTransaction();//结束事务
}
}

public voID save(Person person){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
db.execsql("insert into person(name,phone,amount) values(?,?,?)",
new Object[]{person.getname(),person.getPhone(),person.getAmount()});
//db.close();
}

public voID update(Person person){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
db.execsql("update person set name=?,phone=?,amount=? where personID=?",person.getAmount(),person.getID()});
}

public voID delete(Integer ID){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
db.execsql("delete from person where personID=?",new Object[]{ID});
}

public Person find(Integer ID){
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawquery("select * from person where personID=?",new String[]{ID.toString()});
if(cursor.movetoFirst()){
int personID = cursor.getInt(cursor.getColumnIndex("personID"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
cursor.close();
return new Person(personID,amount);
}
return null;
}

public List<Person> getScrollData(int offset,int maxResult){
List<Person> persons = new ArrayList<Person>();
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawquery("select * from person limit ?,?",
new String[]{String.valueOf(offset),String.valueOf(maxResult)});
while(cursor.movetoNext()){
int personID = cursor.getInt(cursor.getColumnIndex("personID"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
persons.add(new Person(personID,amount));
}
return persons;
}

public Cursor getCursorScrollData(int offset,int maxResult){
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
return db.rawquery("select personID as _ID,amount from person limit ?,String.valueOf(maxResult)});
}

public long getCount(){
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawquery("select count(*) from person",null);
cursor.movetoFirst();
return cursor.getLong(0);
}
}

9、

package cn.itcast.service;

import java.util.ArrayList;
import java.util.List;

import androID.content.ContentValues;
import androID.content.Context;
import androID.database.Cursor;
import androID.database.sqlite.sqliteDatabase;

import cn.itcast.domain.Person;

public class OtherPersonService {
private DBOpenHelper dbOpenHelper;

public OtherPersonService(Context context){
dbOpenHelper = new DBOpenHelper(context);
}

public voID save(Person person){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",person.getname());
values.put("phone",person.getPhone());
values.put("amount",person.getAmount());
db.insert("person",values); // insert into person(personID) values(NulL)
//db.close();
}

public voID update(Person person){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",person.getAmount());
db.update("person","personID=?",new String[]{person.getID().toString()});
}

public voID delete(Integer ID){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete("person",new String[]{ID.toString()});
}

public Person find(Integer ID){
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person",new String[]{"personID","phone"},
"personID=?",new String[]{ID.toString()},null);
if(cursor.movetoFirst()){
int personID = cursor.getInt(cursor.getColumnIndex("personID"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
cursor.close();
return new Person(personID,int maxResult){
List<Person> persons = new ArrayList<Person>();
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person",offset+ ","+ maxResult);
while(cursor.movetoNext()){
int personID = cursor.getInt(cursor.getColumnIndex("personID"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
persons.add(new Person(personID,amount));
}
return persons;
}

public long getCount(){
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person",new String[]{"count(*)"},null);
cursor.movetoFirst();
return cursor.getLong(0);
}
}

10、

<?xml version="1.0" enCoding="utf-8"?>
<linearLayout
xmlns:androID="http://schemas.android.com/apk/res/android"
androID:orIEntation="horizontal"
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content">

<TextVIEw
androID:layout_wIDth="100dip"
androID:layout_height="wrap_content"
androID:text="姓名"
androID:ID="@+ID/name"
/>

<TextVIEw
androID:layout_wIDth="100dip"
androID:layout_height="wrap_content"
androID:text="电话"
androID:ID="@+ID/phone"
/>

<TextVIEw
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
androID:text="存款"
androID:ID="@+ID/amount"
/>
</linearLayout>
11、

<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.android.com/apk/res/android" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" > <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="往内容提供者添加数据" androID:ID="@+ID/button" /> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content"> <TextVIEw androID:layout_wIDth="100dip" androID:layout_height="wrap_content" androID:text="姓名" /> <TextVIEw androID:layout_wIDth="100dip" androID:layout_height="wrap_content" androID:text="电话" /> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="存款" /> </linearLayout> <ListVIEw androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:ID="@+ID/ListVIEw" /> </linearLayout>

总结

以上是内存溢出为你收集整理的Project——SQLite数据库 *** 作(采用ListView实现数据列表展现)全部内容,希望文章能够帮你解决Project——SQLite数据库 *** 作(采用ListView实现数据列表展现)所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存