记账本APP6
对记账本里的记一笔中所填入的数据添加至Sqlite数据库中,之前写体温APP的时候有用到Sqlite数据库,所以在链接数据库这一方面比较方便
MainActivity.java
1 public class MainActivity extends AppCompatActivity { 2 3 //记一笔 4 private ImageButton mAdd; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 mAdd=findViewById(R.id.main_btn_add); 12 mAdd.setOnClickListener(new View.OnClickListener() { 13 @Override 14 public void onClick(View v) { 15 //跳转到记一笔界面 16 Toast.makeText(MainActivity.this,"跳转记一笔界面成功",Toast.LENGTH_SHORT).show(); 17 Intent intent=null; 18 intent=new Intent(MainActivity.this,AddActivity.class); 19 startActivity(intent); 20 } 21 }); 22 }
MyDataBaseHelper.java
1 import android.content.Context; 2 import android.database.sqlite.SQLiteDatabase; 3 import android.database.sqlite.SQLiteOpenHelper; 4 5 public class MyDatabaseHelper extends SQLiteOpenHelper { 6 7 private Context mContext; 8 public MyDatabaseHelper(Context context){ 9 super(context,"AccountBook.db",null,1); 10 mContext=context; 11 } 12 13 @Override 14 public void onCreate(SQLiteDatabase database){ 15 //创建时的回调 16 String sql="create table " + "AcountBook" + "(type varchar,name varchar,money varchar,time varchar,note varchar)"; 17 database.execSQL(sql); 18 } 19 20 @Override 21 public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ 22 //升级时的回调 23 } 24 25 }
Dao.java
1 import android.content.Context; 2 import android.database.sqlite.SQLiteDatabase; 3 4 public class Dao { 5 6 private final MyDatabaseHelper mHelper; 7 8 public Dao(Context context){ 9 //创建数据库 10 mHelper=new MyDatabaseHelper(context); 11 } 12 13 }
Account.java
1 public class Account { 2 private String money; 3 private String time; 4 private String name; 5 private String type; 6 private String note; 7 8 public String getNote() { 9 return note; 10 } 11 12 public void setNote(String note) { 13 this.note = note; 14 } 15 16 public String getType() { 17 return type; 18 } 19 20 public void setType(String type) { 21 this.type = type; 22 } 23 24 public String getTime() { 25 return time; 26 } 27 28 public void setTime(String time) { 29 this.time = time; 30 } 31 32 public String getMoney() { 33 return money; 34 } 35 36 public void setMoney(String money) { 37 this.money = money; 38 } 39 40 public String getName() { 41 return name; 42 } 43 44 public void setName(String name) { 45 this.name = name; 46 } 47 48 public Account(String type,String name,String note,String time,String money){ 49 this.money=money; 50 this.name=name; 51 this.note=note; 52 this.time=time; 53 this.type=type; 54 } 55 56 }