家庭记账本安卓版开发:第七天

一、成果展示

先查看全部账目

家庭记账本安卓版开发:第七天

 

 返回主页

家庭记账本安卓版开发:第七天

 

 进行按需查询

家庭记账本安卓版开发:第七天

 

 我们查询教育、15日

家庭记账本安卓版开发:第七天

 

 点击查询

家庭记账本安卓版开发:第七天

 

 点击该条账目,进行详情查询

家庭记账本安卓版开发:第七天

 

 修改信息

家庭记账本安卓版开发:第七天

 

 重新查询

家庭记账本安卓版开发:第七天

 

 进行查询

家庭记账本安卓版开发:第七天

 

 进入第一条,查看信息

家庭记账本安卓版开发:第七天

 

 我们删除它并再次查询教育15日

家庭记账本安卓版开发:第七天

 

 返回到全部查询

家庭记账本安卓版开发:第七天

 

 二、总结

这次的开发要告一段落了,基本实现了对Sqlite数据库的全部增删改查,完善了Activity间的跳转,马上就要开学了,我们会去学习新的知识,以后会再对记账本的功能进行增加与优化。

三、部分源码

家庭记账本安卓版开发:第七天
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.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.Calendar;

public class QueryByConditionActivity extends Activity {
    private Spinner mSpinner;
    private int mPosition;
    private int mYear;
    private int mMonth;
    private int mDay;
    private TextView mDate;
    private String mTypeText;
    private String mDateText;
    private Button mGotoQueryByCondition;
    private String TAG="QueryByConditionActivity";
    private String mUsername;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_querybycondition);
        //获取登录者的用户名
        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() {
        mSpinner.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(QueryByConditionActivity.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(QueryByConditionActivity.this, R.style.MyDatePickerDialogTheme, onDateSetListener, mYear, mMonth, mDay);
                //获取时间戳
                long timeStamp = System.currentTimeMillis();
                //设置可以显示的最晚的时间
                datePickerDialog.getDatePicker().setMaxDate(timeStamp);
                //弹框
                datePickerDialog.show();
            }
        });
        mGotoQueryByCondition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showlist();
            }
        });
    }

    private void showlist() {
        mDateText=mDate.getText().toString().trim();
        //获取账目类型
        if(mTypeText==null){
            //说明用户未点击按钮,选择默认首项:饮食
            mTypeText="饮食";
            mPosition=0;
        }else if(TextUtils.isEmpty((mDateText) )){
            //账目日期为空
            Toast.makeText(this,"账目日期不可以为空",Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent=new Intent();
        intent.putExtra("username",mUsername);
        intent.putExtra("from","2");
        intent.putExtra("type",mTypeText);
        intent.putExtra("date",mDateText);
        intent.setClass(this,QueryAllActivity.class);
        startActivity(intent);
    }

    /**
     * 日期选择器对话监听
     */
    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() {
        mSpinner=(Spinner)findViewById(R.id.type_condition);
        mDate=(TextView)findViewById(R.id.condition_bill_date);
        mGotoQueryByCondition=(Button)findViewById(R.id.query_by_condition);
    }
}
QueryByConditionActivity.java
家庭记账本安卓版开发:第七天
package com.example.familybook;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

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 java.util.List;

public class QueryShowActivity extends Activity {
    private String mUsername;
    private String mTypeText;
    private String mDateText;
    private String mFrom;
    private String TAG="QueryShowActivity";
    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");
        mFrom=intent.getStringExtra("from");
        Log.e(TAG,"使用者:"+mUsername);

        //初始化控件
        initView();

        //获取表格布局
        mShowBillList=(ListView)findViewById(R.id.list_View);

        //获取从数据库获得的表单
        if(mFrom.equals("1")){
            //来自首页,直接查询全部
            mShowBills=mIBillDao.listAllBill(mUsername);
        }else if(mFrom.equals("2")){
            //来自按条件查询
            mTypeText=intent.getStringExtra("type");
            mDateText=intent.getStringExtra("date");
            mShowBills=mIBillDao.listConditionBill(mUsername,mTypeText,mDateText);
        }

        //新建并配置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(QueryShowActivity.this,InfoActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    private void initView() {
        mIBillDao =new BillDaoImpl(QueryShowActivity.this);

    }

}
QueryShowActivity.java
家庭记账本安卓版开发:第七天
package com.example.familybook.dao;

import com.example.familybook.entity.Bill;

import java.util.List;

/**
 * 这是Bill数据库接口层
 */
public interface IBillDao {

    long AddBill(Bill bill);
    List<Bill> listAllBill(String username);
    List<Bill> listConditionBill(String username,String type,String date);
    Bill QueryBill(int id);
    boolean DeleteBill(int id);
    boolean Update(int id,Bill bill);
}
IBillDao.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;
    }

    /**
     * 按需求查看账目
     * @param username
     * @param type
     * @param date
     * @return
     */
    @Override
    public List<Bill> listConditionBill(String username, String type, String date) {
        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+"=? and "
                    +Constants.BILL_TABLE_FIELD_TYPE+"=?and "
                    +Constants.BILL_TABLE_FIELD_DATE+"=?";
            String[] str  =new  String[]{username,type,date};
            Cursor cursor =db.rawQuery(sql,str);
            /**
             * 下面在库中进行具体查询,并将
             */
            while (cursor.moveToNext()){
                /**
                 * 将查到的数据逐一获取
                 */


                //set id
                int billID = cursor.getInt(cursor.getColumnIndex(Constants.BILL_TABLE_FIELD_ID));
                //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 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;
    }
}
BillDaoImpl

 

家庭记账本安卓版开发:第七天

上一篇:关于我的Androidstudio的再生


下一篇:Android客户端OkHttp的使用以及tomcat服务器的解析客户端发过来的数据