Bean包中
package com.bwie.www.bean;
import java.io.Serializable;
public class Goods implements Serializable{
private int id;
private int picture;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPicture() {
return picture;
}
public void setPicture(int picture) {
this.picture = picture;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Goods(int id, int picture, String name, int price) {
super();
this.id = id;
this.picture = picture;
this.name = name;
this.price = price;
}
public Goods() {
super();
// TODO Auto-generated constructor stub
}
}在Dao包中
package com.bwie.www.dao;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.bwie.www.bean.Goods;
import com.bwie.www.db.MySqliteOpenHelper;
public class GoodsDao {
private SQLiteDatabase db;
public GoodsDao(Context context) {
MySqliteOpenHelper helper=new MySqliteOpenHelper(context);
db = helper.getWritableDatabase();
}
//查询
public ArrayList queryAll(){
ArrayList list=null;
Cursor cursor = db.query("goods", null, null, null, null, null, null);
while(cursor.moveToNext()){
if(list==null){
list=new ArrayList();
}
Goods g=new Goods();
g.setId(cursor.getInt(cursor.getColumnIndex("_id")));
g.setPicture(cursor.getInt(cursor.getColumnIndex("picture")));
g.setName(cursor.getString(cursor.getColumnIndex("name")));
g.setPrice(cursor.getInt(cursor.getColumnIndex("price")));
list.add(g);
}
return list;
}
//模糊查询
public ArrayList query(String name){
ArrayList list=null;
Cursor cursor = db.query("goods", null, "name like ?", new String[]{"%"+name+"%"}, null, null, null);
while(cursor.moveToNext()){
if(list==null){
list=new ArrayList();
}
Goods g=new Goods();
g.setId(cursor.getInt(cursor.getColumnIndex("_id")));
g.setPicture(cursor.getInt(cursor.getColumnIndex("picture")));
g.setName(cursor.getString(cursor.getColumnIndex("name")));
g.setPrice(cursor.getInt(cursor.getColumnIndex("price")));
list.add(g);
}
return list;
}
//删除
public int delete(int id){
int i = db.delete("goods", "_id=?", new String[]{""+id});
return i;
}
//修改
public int update(Goods g){
ContentValues values=new ContentValues();
values.put("name", g.getName());
values.put("price", g.getPrice());
int i = db.update("goods", values, "_id=?", new String[]{""+g.getId()});
return i;
}
}创建数据库
package com.bwie.www.db;
import com.bwie.www.R;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteOpenHelper extends SQLiteOpenHelper {
public MySqliteOpenHelper(Context context) {
super(context, "week2.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库表
db.execSQL("create table goods(_id integer primary key autoincrement,picture integer,name vaarchar(30),price integer)");
//插入数据
insertSql(db);
}
private void insertSql(SQLiteDatabase db) {
for(int i=0;i<10;i++){
int ii=(int) (Math.random()*100+1);
ContentValues values=new ContentValues();
values.put("picture", R.drawable.ic_launcher);
values.put("name", "英雄.MP3"+"----"+i);
values.put("price", ii);
db.insert("goods", null, values);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}在Activity中
package com.bwie.www.db;
import com.bwie.www.R;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteOpenHelper extends SQLiteOpenHelper {
public MySqliteOpenHelper(Context context) {
super(context, "week2.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库表
db.execSQL("create table goods(_id integer primary key autoincrement,picture integer,name vaarchar(30),price integer)");
//插入数据
insertSql(db);
}
private void insertSql(SQLiteDatabase db) {
for(int i=0;i<10;i++){
int ii=(int) (Math.random()*100+1);
ContentValues values=new ContentValues();
values.put("picture", R.drawable.ic_launcher);
values.put("name", "英雄.MP3"+"----"+i);
values.put("price", ii);
db.insert("goods", null, values);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}在UpDateActivity中
package com.bwie.www;
import java.io.Serializable;
import com.bwie.www.bean.Goods;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class UpdateMainActivity extends Activity {
private EditText et_name;
private EditText et_price;
private Button update;
private Goods g;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_main);
//获取意图对象
Intent intent=getIntent();
g = (Goods) intent.getSerializableExtra("good");
//找控件
et_name = (EditText) findViewById(R.id.et_name);
et_price = (EditText) findViewById(R.id.et_price);
update = (Button) findViewById(R.id.update);
et_name.setText(g.getName());
et_price.setText(g.getPrice()+"");
//为修改添加监听
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString();
String price = et_price.getText().toString();
//将得到的值输入进数据中
Goods goods=new Goods(g.getId(), R.drawable.ic_launcher, name, Integer.parseInt(price));
Intent intent=new Intent(UpdateMainActivity.this,MainActivity.class);
intent.putExtra("goods", goods);
setResult(2,intent);
finish();
}
});
}
}