1、成果java
點擊添加帳目android
選擇帳目類型sql
點擊輸入日期數據庫
點擊肯定,輸入其它信息app
點擊添加ide
這裏覺得第一次沒截圖到上面的添加成功佈局
查看數據庫this
添加成功spa
2、經驗總結.net
首先,今天本來打算將添加與查詢所有帳目的部分,可是在開發過程當中遇到了一些問題,關於變量的類型,如date,是否設爲Date類型,但Date類型沒法強轉爲String類型,最終在layout佈局文件裏設置了inputType=date解決了問題;還有下拉框組件的點擊事件,本來使用的是setOnClickListener,實際應該要用mTypeSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 。
再就是關於選擇日期,彈出日曆組件,參考了下面的博客
https://blog.csdn.net/qq_40116418/article/details/84784593
3、部分原碼
AddBillActivity.java
package com.example.familybook; import android.app.Activity; import android.app.DatePickerDialog; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import androidx.annotation.Nullable; import com.example.familybook.dao.BillDaoImpl; import com.example.familybook.dao.IBillDao; import com.example.familybook.entity.Bill; import java.sql.Date; import java.util.Calendar; public class AddBillActivity extends Activity { private int mYear; private int mMonth; private int mDay; private Spinner mTypeSpin; private EditText mMoney; private EditText mDate; private EditText mRemark; private Button mAddBillBtn; private IBillDao mIBillDao; private String mTypeText; private String mUsername; private String TAG="AddBillActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addbill); // //獲取登陸者的用戶名 Intent intent =getIntent(); mUsername =intent.getStringExtra("username"); Log.e(TAG,"使用者:"+mUsername); //獲取時間 initDate(); //初始化控件 initView(); //添加監聽事件 initListener(); } private void initDate() { //獲取當前時間 Calendar ca = Calendar.getInstance(); mYear = ca.get(Calendar.YEAR); mMonth = ca.get(Calendar.MONTH); mDay = ca.get(Calendar.DAY_OF_MONTH); } private void initListener() { mTypeSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mTypeText=parent.getItemAtPosition(position).toString(); Toast.makeText(AddBillActivity.this,mTypeText,Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); mDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //調用時間選擇器 DatePickerDialog datePickerDialog = new DatePickerDialog(AddBillActivity.this, R.style.MyDatePickerDialogTheme, onDateSetListener, mYear, mMonth, mDay); //獲取時間戳 long timeStamp = System.currentTimeMillis(); //設置能夠顯示的最晚的時間 datePickerDialog.getDatePicker().setMaxDate(timeStamp); //彈框 datePickerDialog.show(); } }); mAddBillBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addBill(); } }); } /** * 日期選擇器對話監聽 */ private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; String days; if (mMonth + 1 < 10) { if (mDay < 10) { days = new StringBuffer().append(mYear).append("-").append("0"). append(mMonth + 1).append("-").append("0").append(mDay).append("").toString(); } else { days = new StringBuffer().append(mYear).append("-").append("0"). append(mMonth + 1).append("-").append(mDay).append("").toString(); } } else { if (mDay < 10) { days = new StringBuffer().append(mYear).append("-"). append(mMonth + 1).append("-").append("0").append(mDay).append("").toString(); } else { days = new StringBuffer().append(mYear).append("-"). append(mMonth + 1).append("-").append(mDay).append("").toString(); } } mDate.setText(days); } }; private void addBill() { Bill bill=null; //獲取帳目類型 if(mTypeText==null){ //說明用戶未點擊按鈕,選擇默認首項:飲食 mTypeText="飲食"; } //獲取帳目金額 String moneyText=mMoney.getText().toString().trim(); //獲取日期 String date= mDate.getText().toString().trim(); //獲取備註 String remarkText=mRemark.getText().toString().trim(); if(TextUtils.isEmpty(moneyText)){ //帳目金額爲空 Toast.makeText(this,"帳目金額不能夠爲空",Toast.LENGTH_SHORT).show(); return; }else if(TextUtils.isEmpty((CharSequence) date)){ //帳目日期爲空 Toast.makeText(this,"帳目日期不能夠爲空",Toast.LENGTH_SHORT).show(); return; }else { Log.e(TAG, "帳戶:" + mUsername); Log.e(TAG, "類型:" + mTypeText); Log.e(TAG, "金額:" + moneyText); Log.e(TAG, "日期:" + date); Log.e(TAG, "備註:" + remarkText); bill=new Bill(mUsername,mTypeText,moneyText,date,remarkText); int rs=(int)mIBillDao.AddBill(bill); Log.e(TAG,"rs:"+rs); if (rs > -1) { //添加成功,跳回首面 Intent intent = new Intent(); setResult(2, intent); finish(); } } } private void initView() { mTypeSpin =(Spinner) this.findViewById(R.id.type_spin); mMoney =(EditText)this.findViewById(R.id.bill_money); mDate =(EditText)this.findViewById(R.id.bill_date); mRemark=(EditText)this.findViewById(R.id.bill_remark); mAddBillBtn=(Button)this.findViewById(R.id.add_bill_btn); mIBillDao =new BillDaoImpl(AddBillActivity.this); } }
Bill.java
package com.example.familybook.entity; import java.sql.Date; /** * 帳單實體類 */ public class Bill { private int _id; private String username; private String type; private String money; private String date; private String remark; public Bill() { } public Bill(int _id, String username, String type, String money, String date, String remark) { this._id = _id; this.username = username; this.type = type; this.money = money; this.date = date; this.remark = remark; } public Bill(String username, String type, String money, String date, String remark) { this.username = username; this.type = type; this.money = money; this.date = date; this.remark = remark; } public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "Bill{" + "_id=" + _id + ", username='" + username + '\'' + ", type='" + type + '\'' + ", money=" + money + ", date=" + date + ", remark='" + remark + '\'' + '}'; } }
BillDaoImpl.java
package com.example.familybook.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.familybook.database.FamilyBookDatabaseHelper; import com.example.familybook.entity.Bill; import com.example.familybook.utils.Constants; import java.util.ArrayList; import java.util.List; public class BillDaoImpl implements IBillDao { private final FamilyBookDatabaseHelper mBillDatabaseHelper; public BillDaoImpl(Context context){ mBillDatabaseHelper=new FamilyBookDatabaseHelper(context); } /** * 添加帳目 * @param bill * @return */ @Override public long AddBill(Bill bill) { SQLiteDatabase db =mBillDatabaseHelper.getWritableDatabase(); long result =-1; try { ContentValues values =new ContentValues(); values.put(Constants.BILL_TABLE_FIELD_UNAME,bill.getUsername()); values.put(Constants.BILL_TABLE_FIELD_TYPE,bill.getType()); values.put(Constants.BILL_TABLE_FIELD_MONEY,bill.getMoney()); values.put(Constants.BILL_TABLE_FIELD_DATE,bill.getDate()); values.put(Constants.BILL_TABLE_FIELD_REMARK,bill.getRemark()); result=db.insert(Constants.BILL_TABLE_NAME,null,values); }catch (Exception e){ e.printStackTrace(); }finally { db.close(); } return result; } }
activity_addbill.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@mipmap/addbill" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:text="添加帳目" android:textSize="50sp" android:layout_gravity="center" android:layout_height="wrap_content"/> <RelativeLayout android:layout_width="match_parent" android:layout_marginTop="40dp" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="82dp" android:text="請選擇帳目類型" android:textSize="20sp" /> <Spinner android:id="@+id/type_spin" android:entries="@array/bill_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbarSize="25sp" android:layout_alignParentRight="true" android:layout_marginRight="60dp" /> </RelativeLayout> <EditText android:id="@+id/bill_money" android:layout_marginTop="20dp" android:layout_width="250dp" android:hint="請輸入帳目金額" android:inputType="number" android:layout_gravity="center" android:layout_height="wrap_content"/> <EditText android:id="@+id/bill_date" android:layout_marginTop="20dp" android:layout_width="250dp" android:hint="請輸入帳目日期" android:inputType="date" android:layout_gravity="center" android:layout_height="wrap_content"/> <EditText android:id="@+id/bill_remark" android:layout_width="250dp" android:layout_marginTop="20dp" android:layout_gravity="center" android:hint="在此處添加備註" android:layout_height="wrap_content"/> <Button android:id="@+id/add_bill_btn" android:layout_width="wrap_content" android:layout_marginTop="20dp" android:background="@android:color/transparent" android:text="添加" android:layout_gravity="center" android:layout_height="wrap_content"/> </LinearLayout>