码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之记账本开发第三天

时间:2020-02-10 18:14:21      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:listview   alert   content   date   今天   seh   onupgrade   statement   als   

一、说在前面

昨天主要完成前期的准备和计划,并且完成了适配器的配置和测试,今天完成了完成数据库的部署和对业务逻辑的初步整理,明天对业务逻辑进行进一步的完善。没有遇到问题。

二、今天完成的源代码

技术图片
package com.example.daliy;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<CostBean> costBeanList;
    private DatabaseHelper helper;
    CostAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        costBeanList=new ArrayList<>();
        helper=new DatabaseHelper(this);
        ListView costList=findViewById(R.id.lv_main);
        initCostData();

        mAdapter = new CostAdapter(this, costBeanList);
        costList.setAdapter(mAdapter);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
                View viewDialog =inflater.inflate(R.layout.new_cost,null);
                final EditText cost_title=viewDialog.findViewById(R.id.et_cost_title);
                final EditText cost_money=viewDialog.findViewById(R.id.et_cost_money);
                final DatePicker cost_date=viewDialog.findViewById(R.id.dp_cost_date);
                builder.setTitle("新账目");
                builder.setView(viewDialog);
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CostBean costBean=new CostBean();
                        costBean.costTitle=cost_title.getText().toString();
                        costBean.costMoney=cost_money.getText().toString();
                        costBean.costDate=cost_date.getYear()+"-"+(cost_date.getMonth()+1)+"-"+cost_date.getDayOfMonth();
                        helper.insertCost(costBean);
                        costBeanList.add(costBean);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("Cancel",null);
                builder.create().show();
            }
        });
    }

    private void initCostData() {
//        helper.deleteAllCost();
//        for(int i=0;i<6;i++) {
//
//            CostBean cb=new CostBean();
//            cb.costDate="12-12";
//            cb.costMoney="50";
//            cb.costTitle=i+"heih";
//            helper.insertCost(cb);
//        }
        Cursor cursor = helper.getAllCost();
        if(cursor!=null){
            while (cursor.moveToNext()){
                CostBean costBean=new CostBean();
                costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title"));
                costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date"));
                costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money"));
                costBeanList.add(costBean);
            }
            cursor.close();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_chart) {

            Intent intent = new Intent(MainActivity.this,ChartActivity.class);
            startActivity(intent);
            intent.putExtra("cost_list", (Serializable) costBeanList);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
MainActivity
技术图片
package com.example.daliy;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(@Nullable Context context) {
        super(context, Constants.TABLE_NAME, null, Constants.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists daily("+
                "id integer primary key,"+
                "cost_title varchar,"+
                "cost_date varchar,"+
                "cost_money varchar)");
    }


    public void insertCost(CostBean costBean){
        SQLiteDatabase database=getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(Constants.COST_TITLE,costBean.costTitle);
        cv.put(Constants.COST_DATE,costBean.costDate);
        cv.put(Constants.COST_MONEY,costBean.costMoney);
        database.insert(Constants.TABLE_NAME,null,cv);
    }

    public Cursor getAllCost(){

        SQLiteDatabase database=getWritableDatabase();
        return database.query(Constants.TABLE_NAME,null,null,null,null,null,"cost_date asc");
    }

    public void deleteAllCost(){
        SQLiteDatabase database=getWritableDatabase();
        database.delete(Constants.TABLE_NAME,null,null);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
DatabaseHelper
技术图片
package com.example.daliy;

public class Constants {
    public static final String TABLE_NAME="daily";
    public static final int DATABASE_VERSION=1;
    public static final String COST_TITLE = "cost_title";
    public static final String COST_DATE = "cost_date";
    public static final String COST_MONEY = "cost_money";
}
Costants
技术图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_cost_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:hint="Cost Title" />

    <EditText
        android:id="@+id/et_cost_money"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:hint="Cost Money" />

    <DatePicker
        android:id="@+id/dp_cost_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:datePickerMode="spinner"
        android:calendarViewShown="false"
        android:layout_margin="4dp" />

</LinearLayout>
new_cost

三、效果

技术图片

 

 

点击右下角的按键,就可以添加账目

 

技术图片

 

 输入信息之后

 

 

 技术图片

 

 点击Cancel取消,点击ok保存之后就保存到了数据库中并显示出来

技术图片

 

 

 

Android开发之记账本开发第三天

标签:listview   alert   content   date   今天   seh   onupgrade   statement   als   

原文地址:https://www.cnblogs.com/suanai/p/12291711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!