Android开发实战——记账本(2)
<p><strong>开发日志(2)——Bean目录以及数据库</strong></p><p> 首先编写一些自己生成的数据进行测试,看一下能否显示在模拟器上。那前提就是先写出bean目录,这和之前学的Javaweb步骤差不多。bean目录有三个变量事件、时间、花费。所以bean目录很容易就写出</p>
<p>CostBean</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.firstapplication;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.Serializable;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> CostBean <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Serializable {
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String costTitle;
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String costDate;
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String costMoney;
}</span></pre>
</div>
<p>这里面的implements Serializable是之后将数据传给线性表时添加的。一个类只有实现了Serializable接口,它的对象才是可序列化的。因此如果要序列化某些类的对象,这些类就必须实现Serializable接口。而实际上,Serializable是一个空接口,没有什么具体内容,它的目的只是简单的标识一个类的对象可以被序列化。</p>
<p>既然是个记账本,那里面的数据肯定不能丢失,不能出现关机了或者程序退出了数据就消失的现象。所以我们得写一个数据库,将数据保存下来</p>
<p>下面是对数据库进行增删改查操作的sql语句</p>
<p>增:(add)</p>
<div class="cnblogs_code">
<pre>db.execSQL("insert into Users (username,password) values(?,?)",new Object[]{bean.username,bean.password});</pre>
</div>
<p>删:(delete)</p>
<div class="cnblogs_code">
<pre>db.execSQL("delete from Users where username=?",new String[]{username});</pre>
</div>
<p>改:(update)</p>
<div class="cnblogs_code">
<pre>db.execSQL("update Users set password=? where username=?",new Object[]{bean.password,bean.username});</pre>
</div>
<p>查:(select)</p>
<div class="cnblogs_code">
<pre>Cursor c=db.rawQuery("select * from Users where username=?",new String[]{name});</pre>
</div>
<div class="cnblogs_code">
<pre>Cursor c=db.rawQuery("select * from Users",null);</pre>
</div>
<p>数据库类</p>
<p>DatabaseHelper:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.firstapplication;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.ContentValues;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Context;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.Cursor;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.sqlite.SQLiteDatabase;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.sqlite.SQLiteOpenHelper;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.annotation.Nullable;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> DatabaseHelper <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteOpenHelper {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String COST_TITLE = "cost_title"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String COST_DATE = "cost_date"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String COST_MONEY = "cost_money"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String IMOOC_COST = "imooc_cost"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> DatabaseHelper(Context context) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span>(context, "imooc_daily", <span style="color: rgba(0, 0, 255, 1)">null</span>, 1<span style="color: rgba(0, 0, 0, 1)">);
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(SQLiteDatabase db) {
db.execSQL(</span>"create table if not exists imooc_cost(" +
"id integer primary key," +
"cost_title varchar," +
"cost_date varchar ," +
"cost_money varchar)"<span style="color: rgba(0, 0, 0, 1)"> );
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insertCost(CostBean costBean){
SQLiteDatabase database </span>=<span style="color: rgba(0, 0, 0, 1)"> getWritableDatabase();
ContentValues cv </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ContentValues();
cv.put(COST_TITLE,costBean.costTitle);
cv.put(COST_DATE,costBean.costDate);
cv.put(COST_MONEY,costBean.costMoney);
database.insert(IMOOC_COST,</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,cv);
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Cursor getAllCostData(){
SQLiteDatabase database </span>=<span style="color: rgba(0, 0, 0, 1)"> getWritableDatabase();
</span><span style="color: rgba(0, 0, 255, 1)">return</span>database.query(IMOOC_COST,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,"cost_date ASC"<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> deleteAlldata(){
SQLiteDatabase database </span>=<span style="color: rgba(0, 0, 0, 1)"> getWritableDatabase();
database.delete(IMOOC_COST,</span><span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onUpgrade(SQLiteDatabase db, <span style="color: rgba(0, 0, 255, 1)">int</span> oldVersion, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> newVersion) {
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> deleteOne(CostBean costBean) {
SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();
database.delete(IMOOC_COST,</span>"COST_TITLE = ? and COST_MONEY = ? and COST_DATE = ?", <span style="color: rgba(0, 0, 255, 1)">new</span> String[]{""+costBean.costTitle,""+costBean.costMoney,""+<span style="color: rgba(0, 0, 0, 1)">costBean.costDate});
}
}</span></pre>
</div>
<p>接下来就可以在Activity中调用了,将数据保存到数据库中</p>
<p>明天准备生成点数据,编写一下MainActivity,将数据显示出来</p><br><br>
来源:https://www.cnblogs.com/wendi/p/12303910.html
頁:
[1]