Android开发之记账本开发第三天
一、说在前面
昨天主要完成前期的准备和计划,并且完成了适配器的配置和测试,今天完成了完成数据库的部署和对业务逻辑的初步整理,明天对业务逻辑进行进一步的完善。没有遇到问题。
二、今天完成的源代码
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保存之后就保存到了数据库中并显示出来
相关推荐
Nostalgiachild 2020-11-13
韩伟佳 2020-10-09
wuleihenbang 2020-09-16
zzqLivecn 2020-07-09
chenjinlong 2020-06-10
yinbaoshiguang 2020-06-09
sgafdsg 2020-06-04
ustcrding 2020-06-03
chenjinlong 2020-06-03
AndroidGA 2020-06-01
安辉 2020-05-27
绿豆饼 2020-05-26
CNETNews 2020-05-26
xilove0 2020-05-12
绿豆饼 2020-05-12
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
hqulyc 2020-05-05
lyccsu 2020-04-30