记账本APP7

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

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);
                }
            }
        });
    }
}
复制代码

 

posted @ 2021-03-01 21:31  一口獠牙  阅读(62)  评论(0编辑  收藏  举报