Android给我们提供 Sqlite这种轻量级数据库,并提供了一些数据操作访问的类库.如:SQLiteOpenHelper 主要是处理打开、创建、更新等方法的类库。
有onCreate() onOpen() onUpgarde(db,int ,int) 等方法,还有几个重要的和常用的类也要说一下,就是SqliteDataBase,提供操作SQLite的增、查、删、改等基本方法。还有就是ContentValues ,用来维持当前已打开的数据库的相关数据,当然也提供了增、查、删、改,只不过是在当前数据中。那么先实现创建,和提供的一些自定义的接口,方便我们以后使用,引用方便。
一些数据字段的都放在这个接口中:
publicinterface PublicDataCost {
//定义数据库名称
String DATABASE_NAME ="ZisouBaseDate";
int DATABASE_VERSION =1;
//路径地址
String DB_PATH =
"/data/data/Zisou.Soft.mini/databases";
String PATH ="/databases";
String DB_NAME ="zisoubasedate.db";
//定义共享优先数据及基础字段
String MY_RMBCost ="MY_RMBCost";
String TodayTime ="TodayTime";
//Sqlite数据库基础字段
//公有字段
String ID ="id";
//TodaySpend表结构
String Tb_Name_1 ="TodaySpend";
String Field_1_1 ="Morning";
String Field_1_2 ="Noon";
String Field_1_3 ="Night";
String Field_1_4 ="OtherSpend";
String Field_1_5 ="DailySpend";
String Field_1_6 ="DateTimes";
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View.OnClickListener;
publicclass SqliteCommen extends SQLiteOpenHelper {
//定义增,改,删,清除字段
OnClickListener dbl_add =null;
OnClickListener dbl_update =null;
OnClickListener dbl_delete =null;
public SqliteCommen(Context context) {
super(context, PublicDataCost.DATABASE_NAME, null, PublicDataCost.DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
publicvoid onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql ="CREATE TABLE "+ PublicDataCost.Tb_Name_1 +" ("
+ PublicDataCost.ID +" INTEGER primary key autoincrement, "
+ PublicDataCost.Field_1_1 +" text not null, "
+ PublicDataCost.Field_1_2 +" text not null, "
+ PublicDataCost.Field_1_3 +" text not null, "
+ PublicDataCost.Field_1_4 +" text not null, "
+ PublicDataCost.Field_1_5 +" text not null, "
+ PublicDataCost.Field_1_6 +" text not null "
+");";
db.execSQL(sql);
}
@Override
publicvoid onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
写好简单的基本的数据库的基本方法,在的程序中也要使用到一些必不可少的方法,那可以通用写在Commen类里面。
SqliteCommen sqlc;
privateboolean addflag =false;
privateint spinnerid;
sqlc = new SqliteCommen(this);
SQLiteDatabase db = sqlc.getReadableDatabase();
将dbl_add的监听事件注册,在这里需要考虑什么时候需要添加,什么时候需要修改,那就需要两个条件,一个就是标识,和修改的ID值,当通过Sprinner下拉表传递过来的值将其绑定绑定在EeitView控件上,然后标识Button.setText()值为“修改”。addflag = true 来判断修改和删除。
sqlc.dbl_add =new OnClickListener() {
publicvoid onClick(View v) {
if(addflag ==false)
{
//添加
Cursor c = db.query(PublicDataCost.Tb_Name_1, AllFeild, "DateTimes='"+ str +"'", null, null, null, null);
if(c.getCount()>0)
{
Toast.makeText( todayspend.this , "今天已经添加账单,你可以修改今日账单!", Toast.LENGTH_SHORT).show();
}
else
{
ContentValues cv =new ContentValues();
cv.put( PublicDataCost.Field_1_1 , tv_Morning.getText().toString());
cv.put( PublicDataCost.Field_1_2 , tv_noon.getText().toString());
cv.put( PublicDataCost.Field_1_3 , tv_Night.getText().toString());
cv.put( PublicDataCost.Field_1_4 , tv_OtherSpend.getText().toString());
cv.put( PublicDataCost.Field_1_5 , tv_DailySpend.getText().toString());
cv.put( PublicDataCost.Field_1_6 , str);
SQLiteDatabase db = sqlc.getReadableDatabase();
db.insert(PublicDataCost.Tb_Name_1, null, cv);
db.close();
Toast.makeText( todayspend.this , "添加成功!", Toast.LENGTH_SHORT).show();
onCreate(savedInstanceState);
}
c.close();
}
else
{
//修改
ContentValues cv =new ContentValues();
cv.put( PublicDataCost.Field_1_1 , tv_Morning.getText().toString());
cv.put( PublicDataCost.Field_1_2 , tv_noon.getText().toString());
cv.put( PublicDataCost.Field_1_3 , tv_Night.getText().toString());
cv.put( PublicDataCost.Field_1_4 , tv_OtherSpend.getText().toString());
cv.put( PublicDataCost.Field_1_5 , tv_DailySpend.getText().toString());
String sqlwhere = PublicDataCost.ID +" = "+ spinnerid;
SQLiteDatabase db = sqlc.getReadableDatabase();
db.update(PublicDataCost.Tb_Name_1, cv, sqlwhere ,null);
db.close();
onCreate(savedInstanceState);
Toast.makeText( todayspend.this , "修改成功!", Toast.LENGTH_SHORT).show();
}
}
};
然后就是Spinner的绑定和注册了,先将数据库里面的日期取出来,在SetAdapter中绑定,R.layout.simple_dropdown_item_1line,当然也可以自己定义里面的东西,比如图文结合的下拉列表,在绑定的同时也把Spinner中的setSelection默认选择值给赋值一下。显示从库中出来的最后一条数据。最后再注册事件中将spinnerid = Integer.parseInt(c.getString(0));赋值,以便修改时候用到。
//将事件绑定到按钮
bt_addspend.setOnClickListener(sqlc.dbl_add);
//绑定列表
Cursor c = db.query( PublicDataCost.Tb_Name_1 , new String[] {PublicDataCost.Field_1_6}, null, null, null , null, null);
c.moveToFirst();
CharSequence[] cslist =new CharSequence[c.getCount()];
for(int i=0;i<cslist.length;i++)
{
cslist[i]=c.getString(0);
c.moveToNext();
}
c.close();
Spinner sp = (Spinner)findViewById(R.id.spinner1);
sp.setAdapter(new ArrayAdapter<CharSequence>(this,android.R.layout.simple_dropdown_item_1line,cslist));
sp.setSelection(cslist.length-1);
//注册事件
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
publicvoid onItemSelected(AdapterView<?> parent ,View view, int position, long id){
String todaydatetime = ((Spinner)parent).getSelectedItem().toString();
Cursor c = db.query(PublicDataCost.Tb_Name_1, AllFeild, "DateTimes='"+ todaydatetime +"'", null, PublicDataCost.Field_1_6, null, PublicDataCost.Field_1_6);
c.moveToFirst();
spinnerid = Integer.parseInt(c.getString(0));
tv_Morning.setText(c.getString(1));
tv_noon.setText(c.getString(2));
tv_Night.setText(c.getString(3));
tv_OtherSpend.setText(c.getString(4));
tv_DailySpend.setText(c.getString(5));
c.close();
}
publicvoid onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
然后最后就是将今日和昨日的理财信息给显示出来,我就贴一下全部的代码吧:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import android.app.Activity;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
publicclass todayspend extends Activity {
SqliteCommen sqlc;//SQLiteOpenHelper
privateboolean addflag =false; //标识
privateint spinnerid;
publicvoid onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todayspend);
sqlc =new SqliteCommen(this);
final TextView vt_qdt = (TextView)findViewById(R.id.tv_qdt);
final SQLiteDatabase db = sqlc.getReadableDatabase();
final EditText tv_Morning = (EditText)findViewById(R.id.tv_Morning);
final EditText tv_noon = (EditText)findViewById(R.id.tv_noon);
final EditText tv_Night = (EditText)findViewById(R.id.tv_Night);
final EditText tv_OtherSpend = (EditText)findViewById(R.id.tv_OtherSpend);
final EditText tv_DailySpend = (EditText)findViewById(R.id.tv_DailySpend);
final Button bt_addspend = (Button)findViewById(R.id.bt_addspend);
final Button bt_qiandao2 = (Button)findViewById(R.id.bt_qd2);
final SharedPreferences my_rmb_data = getSharedPreferences(PublicDataCost.MY_RMBCost, 0);
final String nowtime = my_rmb_data.getString(PublicDataCost.TodayTime, "").toString();
TextView jrzkx_0 = (TextView)findViewById(R.id.jrzkx_0);
TextView jrzkx_01 = (TextView)findViewById(R.id.jrzkx_01);
TextView jrzkx_02 = (TextView)findViewById(R.id.jrzkx_02);
TextView jrzkx_03 = (TextView)findViewById(R.id.jrzkx_03);
TextView Zrzkx_0 = (TextView)findViewById(R.id.zrzkx_0);
TextView Zrzkx_01 = (TextView)findViewById(R.id.zrzkx_01);
TextView Zrzkx_02 = (TextView)findViewById(R.id.zrzkx_02);
TextView Zrzkx_03 = (TextView)findViewById(R.id.zrzkx_03);
Time t =new Time();
t.setToNow();
int yesterday = t.monthDay -1 ;
int lastmonth = t.month +1 ;
String laststrday ="";
laststrday= t.year +"年"+ lastmonth +"月"+ yesterday +"日";
final String str = t.year +"年"+ lastmonth +"月"+ t.monthDay +"日";
if(nowtime.equals(str)==true)
{
vt_qdt.setText("日期:"+ nowtime +"已签到!");
bt_qiandao2.setBackgroundResource(R.drawable.yqd);
}
else
{
vt_qdt.setText("日期:"+ str);
bt_qiandao2.setBackgroundResource(R.drawable.qd);
}
//签到功能
bt_qiandao2.setOnClickListener(new OnClickListener() {
publicvoid onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences my_rmb_data = getSharedPreferences(PublicDataCost.MY_RMBCost, 0);
if(my_rmb_data.getString(PublicDataCost.TodayTime, "").toString().equals(str)==true)
{
Toast.makeText( todayspend.this , "今日已签到!", Toast.LENGTH_SHORT).show();
}
else
{
my_rmb_data.edit()
.putString(PublicDataCost.TodayTime, str)
.commit();
vt_qdt.setText("日期:"+ str +"已签到!");
bt_qiandao2.setBackgroundResource(R.drawable.yqd);
Toast.makeText( todayspend.this , "签到成功!", Toast.LENGTH_SHORT).show();
onCreate(savedInstanceState);
}
}
});
final String[] AllFeild =
{
PublicDataCost.ID,
PublicDataCost.Field_1_1,
PublicDataCost.Field_1_2,
PublicDataCost.Field_1_3,
PublicDataCost.Field_1_4,
PublicDataCost.Field_1_5,
PublicDataCost.Field_1_6
};
//实例化增 事件
sqlc.dbl_add =new OnClickListener() {
publicvoid onClick(View v) {
if(addflag ==false)
{
//添加
Cursor c = db.query(PublicDataCost.Tb_Name_1, AllFeild, "DateTimes='"+ str +"'", null, null, null, null);
if(c.getCount()>0)
{
Toast.makeText( todayspend.this , "今天已经添加账单,你可以修改今日账单!", Toast.LENGTH_SHORT).show();
}
else
{
ContentValues cv =new ContentValues();
cv.put( PublicDataCost.Field_1_1 , tv_Morning.getText().toString());
cv.put( PublicDataCost.Field_1_2 , tv_noon.getText().toString());
cv.put( PublicDataCost.Field_1_3 , tv_Night.getText().toString());
cv.put( PublicDataCost.Field_1_4 , tv_OtherSpend.getText().toString());
cv.put( PublicDataCost.Field_1_5 , tv_DailySpend.getText().toString());
cv.put( PublicDataCost.Field_1_6 , str);
SQLiteDatabase db = sqlc.getReadableDatabase();
db.insert(PublicDataCost.Tb_Name_1, null, cv);
db.close();
Toast.makeText( todayspend.this , "添加成功!", Toast.LENGTH_SHORT).show();
onCreate(savedInstanceState);
}
c.close();
}
else
{
//修改
ContentValues cv =new ContentValues();
cv.put( PublicDataCost.Field_1_1 , tv_Morning.getText().toString());
cv.put( PublicDataCost.Field_1_2 , tv_noon.getText().toString());
cv.put( PublicDataCost.Field_1_3 , tv_Night.getText().toString());
cv.put( PublicDataCost.Field_1_4 , tv_OtherSpend.getText().toString());
cv.put( PublicDataCost.Field_1_5 , tv_DailySpend.getText().toString());
String sqlwhere = PublicDataCost.ID +" = "+ spinnerid;
SQLiteDatabase db = sqlc.getReadableDatabase();
db.update(PublicDataCost.Tb_Name_1, cv, sqlwhere ,null);
db.close();
onCreate(savedInstanceState);
Toast.makeText( todayspend.this , "修改成功!", Toast.LENGTH_SHORT).show();
}
}
};
//将事件绑定到按钮
bt_addspend.setOnClickListener(sqlc.dbl_add);
//绑定列表
Cursor c = db.query( PublicDataCost.Tb_Name_1 , new String[] {PublicDataCost.Field_1_6}, null, null, null , null, null);
c.moveToFirst();
CharSequence[] cslist =new CharSequence[c.getCount()];
for(int i=0;i<cslist.length;i++)
{
cslist[i]=c.getString(0);
c.moveToNext();
}
c.close();
Spinner sp = (Spinner)findViewById(R.id.spinner1);
sp.setAdapter(new ArrayAdapter<CharSequence>(this,android.R.layout.simple_dropdown_item_1line,cslist));
sp.setSelection(cslist.length-1);
//注册事件
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
publicvoid onItemSelected(AdapterView<?> parent ,View view, int position, long id){
String todaydatetime = ((Spinner)parent).getSelectedItem().toString();
Cursor c = db.query(PublicDataCost.Tb_Name_1, AllFeild, "DateTimes='"+ todaydatetime +"'", null, PublicDataCost.Field_1_6, null, PublicDataCost.Field_1_6);
c.moveToFirst();
spinnerid = Integer.parseInt(c.getString(0));
tv_Morning.setText(c.getString(1));
tv_noon.setText(c.getString(2));
tv_Night.setText(c.getString(3));
tv_OtherSpend.setText(c.getString(4));
tv_DailySpend.setText(c.getString(5));
c.close();
}
publicvoid onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//理财信息显示及处理
Cursor c2 = db.query( PublicDataCost.Tb_Name_1, AllFeild , "DateTimes='"+ str +"'" , null, null, null, null);
c2.moveToFirst();
if(c2.getCount()>0)
{
int lastspend = Integer.parseInt(c2.getString(1))+Integer.parseInt(c2.getString(2))+Integer.parseInt(c2.getString(3))+Integer.parseInt(c2.getString(4))+Integer.parseInt(c2.getString(5));
jrzkx_01.setText("早:"+ c2.getString(1) +" 中:"+ c2.getString(2)+" 晚:"+c2.getString(3));
jrzkx_02.setText("购置:"+ c2.getString(4)+" 日常:"+c2.getString(5));
jrzkx_03.setText("今日总消费金额:"+ lastspend);
bt_addspend.setText("修 改");
addflag =true;
}
else{
jrzkx_0.setText("今日未记账");
jrzkx_01.setText("");
jrzkx_02.setText("");
jrzkx_03.setText("");
}
c2.close();
if(yesterday>0)
{
Cursor c3 = db.query( PublicDataCost.Tb_Name_1, AllFeild , "DateTimes='"+ laststrday +"'" , null, null, null, null);
c3.moveToFirst();
if(c3.getCount()>0)
{
int lastspend = Integer.parseInt(c3.getString(1))+Integer.parseInt(c3.getString(2))+Integer.parseInt(c3.getString(3))+Integer.parseInt(c3.getString(4))+Integer.parseInt(c3.getString(5));
Zrzkx_01.setText("早:"+ c3.getString(1) +" 中:"+ c3.getString(2)+" 晚:"+c3.getString(3));
Zrzkx_02.setText("购置:"+ c3.getString(4)+" 日常:"+c3.getString(5));
Zrzkx_03.setText("昨日总消费金额:"+ lastspend);
}
else{
Zrzkx_0.setText("昨日未记账");
Zrzkx_01.setText("");
Zrzkx_02.setText("");
Zrzkx_03.setText("");
}
c3.close();
}
else
{
Zrzkx_0.setText("昨日未记账");
Zrzkx_01.setText("");
Zrzkx_02.setText("");
Zrzkx_03.setText("");
}
}
}
代码写好了最后跑起来看一下效果:
哈哈,然后就可以一目了然的看到很清晰的理财数据,操作也方便,也达到了我们预期想要的功能(记录前一天的理财信息,一键添加,修改,进行理财查询和对比)。
接下来就是做一个弹入弹出式菜单列表,来对这些功能进行详细的统一管理存放的栏目菜单。
希望和大家多多相互学习更多的安卓开发方面的相关知识。