一、成果展示
今天在昨天的基础上,做了一些完善,在数据库中添加了一列int型数据position,用于获取Spinner组件的value值对应的position值,加以保存读取,来实现将取值赋给Spinner组件的默认值,成功实现了下拉框的数据读取显示,进而实现了下拉框对应数据,也就是账目类型的修改,解决了一些bug,关于Activity的生命周期进行了调整,以避免了按返回键时会跳转回上一个画面,而不是它对应的上一级Activity,还解决了如果对数据进行多次查看详细信息,进行修改时,返回原Activity时账目不刷新,以及程序崩溃的问题。
修改信息
保存修改
再次查看
点击删除
二、明天的计划
加入筛选,模糊查询,需要学习Echart的使用,复习javaweb的知识。
三、原码展示
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.familybook"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="家庭记账本" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"> <activity android:name=".LoginActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".IndexActivity" android:launchMode="singleTask"/> <activity android:name=".RegisterActivity" android:launchMode="singleTask"/> <activity android:name=".AddBillActivity" android:launchMode="singleTask"/> <activity android:name=".QueryAllActivity" /> <activity android:name=".InfoActivity" android:launchMode="singleTask"/> </application> </manifest>
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.TextView; 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 int mPosition; private Spinner mTypeSpin; private EditText mMoney; private TextView 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) { mPosition=position; 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="饮食"; mPosition=0; } //获取账目金额 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, "类型:" + mPosition); Log.e(TAG, "金额:" + moneyText); Log.e(TAG, "日期:" + date); Log.e(TAG, "备注:" + remarkText); bill=new Bill(mUsername,mTypeText,mPosition,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 =(TextView) 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); } }
InfoActivity.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.Adapter; import android.widget.AdapterView; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; 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.util.Calendar; public class InfoActivity extends Activity { private Bill mBill; private String mUsername; private int mBillID; private int mYear; private int mMonth; private int mDay; private int mPosition; private Spinner mTypeSpin; private EditText mMoney; private TextView mDate; private EditText mRemark; private Button mUpdateBillBtn; private Button mDeleteBillBtn; private IBillDao mIBillDao; private String mTypeText; private String TAG="InfoActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_info); //获取登录者的用户名 Intent intent =getIntent(); mUsername =intent.getStringExtra("username"); mBillID =intent.getIntExtra("bill_id",mBillID); Log.e(TAG,"使用者:"+mUsername); Log.e(TAG,"账单号:"+mBillID); //获取时间 initDate(); //初始化控件 initView(); //从数据库获取信息,并完成信息初始化 mBill=mIBillDao.QueryBill(mBillID); initInfo(); //添加监听事件 initListener(); } private void initInfo() { mTypeSpin.setSelection(mBill.getTypeposition()); mMoney.setText(mBill.getMoney()); mDate.setText(mBill.getDate()); mRemark.setText(mBill.getRemark()); } 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) { mPosition=position; mTypeText=parent.getItemAtPosition(position).toString(); Toast.makeText(InfoActivity.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(InfoActivity.this, R.style.MyDatePickerDialogTheme, onDateSetListener, mYear, mMonth, mDay); //获取时间戳 long timeStamp = System.currentTimeMillis(); //设置可以显示的最晚的时间 datePickerDialog.getDatePicker().setMaxDate(timeStamp); //弹框 datePickerDialog.show(); } }); mDeleteBillBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deleteBill(); } }); mUpdateBillBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateBill(); } }); } /** * 进行修改 */ private void updateBill() { Bill bill=null; //获取账目类型 //获取账目金额 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, "类型:" + mPosition); Log.e(TAG, "金额:" + moneyText); Log.e(TAG, "日期:" + date); Log.e(TAG, "备注:" + remarkText); bill=new Bill(mUsername,mTypeText,mPosition, moneyText,date,remarkText); boolean rs=mIBillDao.Update(mBillID,bill); Log.e(TAG,"rs:"+rs); if (rs ) { //修改成功,跳回账页 Intent intent = new Intent(); intent.putExtra("username",mUsername); intent.setClass(this,QueryAllActivity.class); startActivity(intent); finish(); } } } /** * 进行删除 */ private void deleteBill() { boolean rs=mIBillDao.DeleteBill(mBillID); if(rs){ //删除成功,跳回账页 Intent intent = new Intent(); intent.putExtra("username",mUsername); intent.setClass(this,QueryAllActivity.class); startActivity(intent); finish(); } } /** * 日期选择器对话监听 */ 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 initView() { mTypeSpin =(Spinner) this.findViewById(R.id.type_spin); mMoney =(EditText)this.findViewById(R.id.bill_money); mDate =(TextView) this.findViewById(R.id.bill_date); mRemark=(EditText)this.findViewById(R.id.bill_remark); mUpdateBillBtn=(Button)this.findViewById(R.id.update_btn); mDeleteBillBtn=(Button)this.findViewById(R.id.delete_btn); mIBillDao =new BillDaoImpl(InfoActivity.this); } }
QueryAllActivity.java
package com.example.familybook; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ScrollView; import android.widget.TextView; 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 com.example.familybook.utils.ShowBillsAdapter; import com.example.familybook.utils.ViewHolder; import java.util.List; public class QueryAllActivity extends Activity { private String mUsername; private String TAG="QueryAllActivity"; private List <Bill> mShowBills; private IBillDao mIBillDao; private SQLiteOpenHelper mBillDatabaseHelper; private ListView mShowBillList; private int mInfoCode=1; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); this.setTitle("总览账目信息"); //获取登录者的用户名 Intent intent =getIntent(); mUsername =intent.getStringExtra("username"); Log.e(TAG,"使用者:"+mUsername); //初始化控件 initView(); //获取表格布局 mShowBillList=(ListView)findViewById(R.id.list_View); //获取从数据库获得的表单 mShowBills=mIBillDao.listAllBill(mUsername); //新建并配置Show ShowBillsAdapter ShowAdapter=new ShowBillsAdapter(mShowBills,this); mShowBillList.setAdapter(ShowAdapter); mShowBillList.deferNotifyDataSetChanged(); //添加事件监听 initListener(); } private void initListener() { mShowBillList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Bill bill =mShowBills.get(position); int bill_id=bill.get_id(); Intent intent=new Intent(); intent.putExtra("username",mUsername); intent.putExtra("bill_id",bill_id); intent.setClass(QueryAllActivity.this,InfoActivity.class); startActivity(intent); finish(); } }); } private void initView() { mIBillDao =new BillDaoImpl(QueryAllActivity.this); } }
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; private IBillDao mIBillDao; 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_TYPEP_OSITION,bill.getTypeposition()); 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; } @Override public List<Bill> listAllBill(String username) { List<Bill> bills= new ArrayList<>(); Bill bill=null; SQLiteDatabase db = mBillDatabaseHelper.getWritableDatabase(); try { String sql ="select * from "+Constants.BILL_TABLE_NAME+" where "+Constants.BILL_TABLE_FIELD_UNAME+"=? "; String[] str =new String[]{username}; Cursor cursor =db.rawQuery(sql,str); /** * 下面在库中进行具体查询,并将 */ while (cursor.moveToNext()){ /** * 将查到的数据逐一获取 */ //set id int billID = cursor.getInt(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_ID)); //set type String type=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_TYPE)); //set type_position int type_position=cursor.getInt(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_TYPEP_OSITION)); //set money String money=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_MONEY)); //set date String date=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_DATE)); //set remark String remark=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_REMARK)); /** * 将获取的数据封装到bill中 */ bill=new Bill(billID,username,type,type_position,money,date,remark); /** * 将封装好的数据bill封装到bills中 */ bills.add(bill); } }catch (Exception e){ e.printStackTrace(); }finally { db.close(); } return bills; } @Override public Bill QueryBill(int id) { Bill bill=null; SQLiteDatabase db = mBillDatabaseHelper.getWritableDatabase(); try { String sql ="select * from "+Constants.BILL_TABLE_NAME+" where "+Constants.BILL_TABLE_FIELD_ID+"=? "; String[] str =new String[]{String.valueOf(id)}; Cursor cursor=db.rawQuery(sql,str); /** * 下面在库中进行具体查询,并将 */ if(cursor.moveToNext()){ /** * 将查到的数据逐一获取 */ //set username String username=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_UNAME)); //set type String type=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_TYPE)); //set type_position int type_position=cursor.getInt(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_TYPEP_OSITION)); //set money String money=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_MONEY)); //set date String date=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_DATE)); //set remark String remark=cursor.getString(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_REMARK)); /** * 将获取的数据封装到bill中 */ bill=new Bill(id,username,type,type_position,money,date,remark); } }catch (Exception e){ e.printStackTrace(); }finally { db.close(); } return bill; } /** * 对Bill表单进行删除 * @param id * @return */ @Override public boolean DeleteBill(int id) { boolean flag=false; SQLiteDatabase db = mBillDatabaseHelper.getWritableDatabase(); String sql ="delete from "+Constants.BILL_TABLE_NAME+" where "+Constants.BILL_TABLE_FIELD_ID+"=? "; Object[] str =new Object[]{id}; db.execSQL(sql,str); db.close(); /** * 在删除结束后,查找该id对应的Bill是否存在,来判断删除是否成功 */ flag=true; return flag; } /** * 对Bill表单进行修改 * @param id * @param bill * @return */ @Override public boolean Update(int id,Bill bill) { boolean flag=false; SQLiteDatabase db = mBillDatabaseHelper.getWritableDatabase(); String sql ="update "+Constants.BILL_TABLE_NAME +" set "+Constants.BILL_TABLE_FIELD_UNAME+"=? ," +Constants.BILL_TABLE_FIELD_TYPE+"= ?," +Constants.BILL_TABLE_FIELD_TYPEP_OSITION+"=?," +Constants.BILL_TABLE_FIELD_MONEY+"=?," +Constants.BILL_TABLE_FIELD_DATE+"=?," +Constants.BILL_TABLE_FIELD_REMARK+"=?" +" where "+Constants.BILL_TABLE_FIELD_ID+"=? "; Object[] obj =new Object[]{bill.getUsername(),bill.getType(),bill.getTypeposition(),bill.getMoney(),bill.getDate(),bill.getRemark(),id}; db.execSQL(sql,obj); db.close(); /** * 在修改结束后,查找该id对应的Bill是否相同,来判断修改是否成功 */ flag=true; return flag; } }
原文地址:https://www.cnblogs.com/yeyueweiliang/p/12308355.html
时间: 2024-11-02 18:04:32