天下之事常成于困约,而败于奢靡。这篇文章主要讲述家庭记账本安卓版开发:第五天相关的知识,希望能为你提供帮助。
一、效果展示
文章图片
点击第一项查看详情
文章图片
修改其信息
文章图片
保存修改
【家庭记账本安卓版开发(第五天)】
文章图片
再次查看详情
文章图片
删除该账目
文章图片
二、问题
对于下拉框组件的值的从数据库获取并赋值并不清楚,还有点击事件的失效情况与布局之间的关联
三、部分原码
activity_info.xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="@mipmap/info" 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="wrap_content" android:layout_gravity="center" android:layout_marginTop="40dp" android:layout_height="wrap_content"> < TextView android:id="@+id/update_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账目类型" android:textSize="20sp" /> < TextView android:id="@+id/type_spin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/update_type" /> < /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"/> < TextView android:id="@+id/bill_date" android:layout_marginTop="20dp" android:layout_width="250dp" android:hint="请输入账目日期" android:inputType="date" android:textSize="18dp" 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"/> < RelativeLayout android:layout_width="wrap_content" android:layout_marginTop="20dp" android:layout_gravity="center" android:layout_height="wrap_content"> < Button android:id="@+id/update_btn" android:layout_width="wrap_content" android:text="保存修改" android:layout_gravity="left" android:layout_height="wrap_content"/> < Button android:id="@+id/delete_btn" android:layout_width="wrap_content" android:layout_toRightOf="@+id/update_btn" android:layout_marginLeft="20dp" android:text="删除该账目" android:layout_height="wrap_content"/> < /RelativeLayout> < /LinearLayout>
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 List < ViewHolder> mViewHolder; 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 ShowBillsAdapterShowAdapter=new ShowBillsAdapter(mShowBills,this); mShowBillList.setAdapter(ShowAdapter); //添加事件监听 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); startActivityForResult(intent,mInfoCode); } }); }private void initView() { mIBillDao =new BillDaoImpl(QueryAllActivity.this); } /** * 返回的结果在这里回调 * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); String resultContent = null; //当我们判断这个请求码的时候,就知道,这是哪个界面返回的结果 if (requestCode==mInfoCode) { if(resultCode==2){ //注册成功 Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show(); }else if(requestCode==3){ //删除成功 Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show(); } } } }
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 TextView 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.setText(mBill.getType()); 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() {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; //获取账目类型 mTypeText=mTypeSpin.getText().toString().trim(); //获取账目金额 String moneyText=mMoney.getText().toString().trim(); //获取日期 Stringdate= 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); boolean rs=mIBillDao.Update(mBillID,bill); Log.e(TAG,"rs:"+rs); if (rs ) { //修改成功,跳回账页 Intent intent = new Intent(); setResult(2, intent); finish(); } } }/** * 进行删除 */ private void deleteBill() { boolean rs=mIBillDao.DeleteBill(mBillID); if(rs){ //删除成功,跳回账页 Intent intent = new Intent(); setResult(3, 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 =(TextView) 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); } }
推荐阅读
- Android UI常用组件
- Unity 导出到Android Studio 打包流程
- 安卓手机微信自带浏览器点击事件失效解决
- Microsoft launches Open Application Model (OAM) and Dapr to ease developments in Kubernetes and micr
- create-react-app搭配react16+ts+less
- EACCES(在CentOS/RHEL 7上的权限被拒绝-已修复)
- 用于生产JAVA应用程序系统的10个重要JVM选项
- 如何在Ubuntu 18上安装Puppet 6()
- 如何在Ubuntu 18上安装Chef()