zoukankan      html  css  js  c++  java
  • 寒假每日日报46(开发家庭记账本APP——进度七)

    昨天将数据库的程序写好了,今天需要将数据存入数据库,代码如下:

    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     public void insert(String type,String name,String money,String time,String note){
    14         SQLiteDatabase db=mHelper.getWritableDatabase();
    15         String sql="insert into "+"AcountBook"+"(type,name,money,time,note) values(?,?,?,?,?)";
    16         db.execSQL(sql,new Object[]{type,name,money,time,note});
    17         db.close();
    18     }
    19 
    20 }

    AddActivity.java

    public class AddActivity extends AppCompatActivity {
    
        //完成按钮
        private Button mBtnSubmit;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add);
            //完成按钮
            mBtnSubmit = findViewById(R.id.btn_submit);
            mBtnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final RadioButton disburseET=findViewById(R.id.disburse);//获取支出位置
                    final RadioButton incomeET=findViewById(R.id.income);//获取收入位置
                    final EditText nameET=findViewById(R.id.editname);//获取名称位置
                    final EditText moneyET=findViewById(R.id.editmoney);//获取金额位置
                    final EditText timeET=findViewById(R.id.edittime);//获取时间位置
                    final EditText noteET=findViewById(R.id.editnote);//获取备注位置
    
                    String DisData=disburseET.getText().toString();//获取支出位置
                    String IncData=incomeET.getText().toString();//获取收入位置
                    String NameData=nameET.getText().toString();//获取名称位置
                    String MoneyData=moneyET.getText().toString();//获取金额位置
                    String TimeData=timeET.getText().toString();//获取时间位置
                    String NoteData=noteET.getText().toString();//获取备注位置
    
                    //创建数据库
                    MyDatabaseHelper helper=new MyDatabaseHelper(AddActivity.this);
                    helper.getWritableDatabase();
                    Dao dao=new Dao(AddActivity.this);
    
                    int i=0;
    
                    if(NameData.equals("")|| MoneyData.equals("")||TimeData.equals("")||NoteData.equals("")){
                        Toast.makeText(AddActivity.this,"请将信息填写完整",Toast.LENGTH_SHORT).show();
                    }else{
                        do{
                            if(incomeET.isChecked()){
                                dao.insert(IncData,NameData,MoneyData,TimeData,NoteData);
                                Toast.makeText(AddActivity.this,"收入写入成功",Toast.LENGTH_SHORT).show();
                                i=1;
                            }else if (disburseET.isChecked()){
                                dao.insert(DisData,NameData,MoneyData,TimeData,NoteData);
                                Toast.makeText(AddActivity.this,"支出写入成功",Toast.LENGTH_SHORT).show();
                                i=1;
                            }else{
                                Toast.makeText(AddActivity.this,"请将信息填写完整",Toast.LENGTH_SHORT).show();
                                i=0;
                            }
                        }while(i==0);
                        Toast.makeText(AddActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                        Intent intent=null;
                        //返回主页
                        intent=new Intent(AddActivity.this,MainActivity.class);
                        startActivity(intent);
                    }
                }
            });
        }
    }
  • 相关阅读:
    hadoop wordcount
    hadoop map reduce 实例wordcount的使用
    玉髓
    数据类型检测的四种方式
    天猫前端招聘要求
    正则示例1
    字面量和实例创建的区别
    正则表达式
    面试题1
    this关键字
  • 原文地址:https://www.cnblogs.com/miao-com/p/14454219.html
Copyright © 2011-2022 走看看