代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
SQLite保存数据 和更新显示
作者:
泺清
/ 发布于
2015/5/12
/
744
package com.example.save; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import com.dao.ProductDao; import com.entity.Product; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText edtname,edtprice,edtcount,edtaddress; private Button btnSave,btnSelect; private ProductDao dao; private Product product; List<Product> proList; public void init(){ edtname=(EditText) findViewById(R.id.edtname); edtprice=(EditText) findViewById(R.id.edtprice); edtcount=(EditText) findViewById(R.id.edtcount); edtaddress=(EditText) findViewById(R.id.edtaddress); btnSave=(Button) findViewById(R.id.btnSave); btnSelect=(Button) findViewById(R.id.btnSelect); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); dao=new ProductDao(getApplicationContext(), null, null, 0); btnSave.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { product=new Product(); product.setProName(edtname.getText().toString()); product.setProPrice(Float.parseFloat(edtprice.getText().toString())); product.setProCount(Integer.parseInt(edtcount.getText().toString())); product.setProAddr(edtaddress.getText().toString()); dao.add(product); Toast toast=Toast.makeText(getApplicationContext(), "保存成功", 2000); toast.show(); } }); btnSelect.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Intent intent=new Intent(); intent.setClass(getApplicationContext(), SelectActivity.class); startActivity(intent); } }); } } package com.example.save; import java.util.ArrayList; import java.util.List; import junit.runner.Version; import com.dao.ProductDao; import com.entity.Product; import android.app.Activity; import android.content.Intent; import android.opengl.Visibility; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.*; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class SelectActivity extends Activity { private ListView listinfo; private ProductDao dao; private List<Product> proList; private MyAdapter adapter; public void init(){ listinfo=(ListView) findViewById(R.id.listinfo); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.selectinfo); init(); dao=new ProductDao(getApplicationContext(), null, null, 1); adapter=new MyAdapter(); listinfo.setAdapter(adapter); } public class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub proList=dao.query(); return proList.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub View v=getLayoutInflater().inflate(R.layout.item, null); TextView txtid=(TextView) v.findViewById(R.id.txtid); TextView txtname=(TextView) v.findViewById(R.id.txtname); TextView txtprice=(TextView) v.findViewById(R.id.txtprice); TextView txtcount=(TextView) v.findViewById(R.id.txtcount); TextView txtaddresss=(TextView) v.findViewById(R.id.txtaddresss); Button but1=(Button)v.findViewById(R.id.but1); Button but2=(Button)v.findViewById(R.id.but2); final Product product=proList.get(arg0); txtid.setText(product.get_id()+""); txtname.setText(product.getProName()+""); txtprice.setText(product.getProPrice()+""); txtcount.setText(product.getProCount()+""); txtaddresss.setText(product.getProAddr()+""); //txtid.setVisibility(View.INVISIBLE); but1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Toast toast=Toast.makeText(getApplicationContext(), "正在删除"+product.getProName(), 1000); toast.show(); dao.delById(product.get_id()); notifyDataSetChanged(); } }); but2.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Intent intent=new Intent(); Bundle b=new Bundle(); b.putString("id", product.get_id()+""); b.putString("proName", product.getProName()+""); b.putString("proPrice", product.getProPrice()+""); b.putString("proCoune", product.getProCount()+""); b.putString("proAddr", product.getProAddr()+""); intent.putExtras(b); intent.setClass(getApplicationContext(), UpdateActivity.class); startActivity(intent); } }); return v; } } } package com.example.save; import java.util.List; import com.dao.ProductDao; import com.entity.Product; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class UpdateActivity extends Activity { private EditText edtname,edtprice,edtcount,edtaddress; private Button btnSave,btnSelect; private ProductDao dao; private Product product; List<Product> proList; public void init(){ edtname=(EditText) findViewById(R.id.etname); edtprice=(EditText) findViewById(R.id.etprice); edtcount=(EditText) findViewById(R.id.etcount); edtaddress=(EditText) findViewById(R.id.etaddress); btnSave=(Button) findViewById(R.id.bnSave); btnSelect=(Button) findViewById(R.id.bnSelect); } private void show() { // TODO Auto-generated method stub Bundle bundle=this.getIntent().getExtras(); String name=bundle.getString("proName"); proList=dao.queryByName(name); edtname.setText(proList.get(0).getProName()); edtprice.setText(proList.get(0).getProName()); edtcount.setText(proList.get(0).getProCount()); edtaddress.setText(proList.get(0).getProAddr()); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.update); init(); dao=new ProductDao(this, null, null, 0); show(); btnSave.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub product=new Product(); product.setProName(edtname.getText().toString()); product.setProPrice(Float.parseFloat(edtprice.getText().toString())); product.setProCount(Integer.parseInt(edtcount.getText().toString())); product.setProAddr(edtaddress.getText().toString()); dao.updateByName(product); } }); btnSelect.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(UpdateActivity.this,SelectActivity.class)); } }); } } package com.entity; public class Product { private int _id; private String proName; private double proPrice; private int proCount; private String proAddr; public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public String getProName() { return proName; } public void setProName(String proName) { this.proName = proName; } public double getProPrice() { return proPrice; } public void setProPrice(double proPrice) { this.proPrice = proPrice; } public int getProCount() { return proCount; } public void setProCount(int proCount) { this.proCount = proCount; } public String getProAddr() { return proAddr; } public void setProAddr(String proAddr) { this.proAddr = proAddr; } } package com.dao; import java.util.ArrayList; import java.util.List; import com.entity.Product; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class ProductDao extends SQLiteOpenHelper{ private static final String DataName="data.db"; private static final int VERSIN=1; public ProductDao(Context context, String name, CursorFactory factory, int version) { super(context, DataName, factory, VERSIN); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sqlstr="create table Product(_id integer primary key autoincrement," + "proName text,proPrice real,proCount integer,proAddr text)"; String sql0="create table product(_id int identity(1,1) primary key ,proName varchar(20),proPrice double,proCount int,proAddr varchar(20))"; String sql2="CREATE TABLE [product](" +"[_id] AUTOINC," +"[proName] varchar(20) not null on conflict fail," +"[proPrice] double" +"[proCount] int" +"[proAddr] varchar(20)" +"constraint[sqllite_autoindex_t_contacts_1 primary key([_id]))"; String sql = "create table product(_id integer primary key autoincrement," + "proName text,proPrice real,proCount integer,proAddr text)"; db.execSQL(sqlstr); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { // TODO Auto-generated method stub String sql="drop table if exists product"; db.execSQL(sql); db.close(); } //增 public void add(Product p){ String sql="insert into product(proName,proPrice,proCount,proAddr) values(?,?,?,?)"; SQLiteDatabase db=getWritableDatabase(); db.execSQL(sql, new Object[]{ p.getProName(), p.getProPrice(), p.getProCount(), p.getProAddr() }); db.close(); } //通过id删 public void delById(int id){ String sql="delete from product where _id =?"; SQLiteDatabase db=getWritableDatabase(); db.execSQL(sql, new Object[]{id}); db.close(); } //查所有 public List<Product> query(){ List<Product> list=new ArrayList<Product>(); String sql="select * from product"; SQLiteDatabase db=getReadableDatabase(); Cursor cursor=db.rawQuery(sql,null); while(cursor.moveToNext()){ Product p=new Product(); p.set_id(cursor.getInt(0)); p.setProName(cursor.getString(1)); p.setProPrice(cursor.getDouble(2)); p.setProCount(cursor.getInt(3)); p.setProAddr(cursor.getString(4)); list.add(p); } db.close(); cursor.close(); return list; } //通过proname删除 public void delbyName(String proName) { String sql="delete from product where proName =?"; SQLiteDatabase db=getWritableDatabase(); db.execSQL(sql, new Object[]{proName}); db.close(); } //通过proName更新 public void updateByName(Product p){ String sql="update product set proPrice=?,proCount=?,proAddr=? where proName = ?"; SQLiteDatabase db=getWritableDatabase(); db.execSQL(sql, new Object[]{ p.getProPrice(), p.getProCount(), p.getProAddr(), p.getProName(), }); db.close(); } //通过name查 public List<Product> queryByName(String name){ List<Product> list=new ArrayList<Product>(); String sql="select * from product"; SQLiteDatabase db=getReadableDatabase(); Cursor cursor=db.rawQuery(sql,new String []{name}); while(cursor.moveToNext()){ Product p=new Product(); p.set_id(cursor.getInt(0)); p.setProName(cursor.getString(1)); p.setProPrice(cursor.getDouble(2)); p.setProCount(cursor.getInt(3)); p.setProAddr(cursor.getString(4)); list.add(p); } db.close(); cursor.close(); return list; } }
试试其它关键字
SQLite
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
泺清
贡献的其它代码
(
14
)
.
错误重定向和终止进程
.
数据库连接池
.
scala通用dao
.
多父类虚函数
.
压缩css
.
全组合算法
.
简单的RandomUtil
.
随机生成指定字数的简体汉字
.
SQLite保存数据 和更新显示
.
谷歌超链接
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3