当前位置:   article > 正文

Android Studio--家庭记账本(一)_家庭记账本android代码

家庭记账本android代码

  今天通过观看视频,根据老师所讲内容课程介绍和项目创建_Android记账本-慕课网,编译代码。实现了Android Studio记账本里面的增加功能

 源代码如下:

CostBean.java:

  1. package com.example.family;
  2. import java.io.Serializable;
  3. public class CostBean implements Serializable{
  4. public String costTitle;
  5. public String costDate;
  6. public String costMoney;
  7. public String getCostTitle() {
  8. return costTitle;
  9. }
  10. public void setCostTitle(String costTitle) {
  11. this.costTitle = costTitle;
  12. }
  13. public String getCostDate() {
  14. return costDate;
  15. }
  16. public void setCostDate(String costDate) {
  17. this.costDate = costDate;
  18. }
  19. public String getCostMoney() {
  20. return costMoney;
  21. }
  22. public void setCostMoney(String costMoney) {
  23. this.costMoney = costMoney;
  24. }
  25. @Override
  26. public String toString() {
  27. return "CostBean{" +
  28. "costTitle='" + costTitle + '\'' +
  29. ", costDate='" + costDate + '\'' +
  30. ", costMoney='" + costMoney + '\'' +
  31. '}';
  32. }
  33. }

CostListAdapter.java:

  1. package com.example.family;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.TextView;
  8. import java.util.List;
  9. public class CostListAdapter extends BaseAdapter{
  10. private List<CostBean> mList;
  11. private Context mContext;
  12. private LayoutInflater mLayoutInflater;
  13. public CostListAdapter(Context context,List<CostBean> list){
  14. mContext=context;
  15. mList=list;
  16. mLayoutInflater=LayoutInflater.from(context);
  17. }
  18. @Override
  19. public int getCount() {
  20. return mList.size();
  21. }
  22. @Override
  23. public Object getItem(int position) {
  24. return mList.get(position);
  25. }
  26. @Override
  27. public long getItemId(int position) {
  28. return position;
  29. }
  30. @Override
  31. public View getView(int position, View convertView, ViewGroup parent) {
  32. ViewHolder viewHolder;
  33. if(convertView==null){
  34. viewHolder=new ViewHolder();
  35. convertView=mLayoutInflater.inflate(R.layout.list_item,null);
  36. viewHolder.mTvCostTitle=(TextView)convertView.findViewById(R.id.tv_title);
  37. viewHolder.mTvCostDate=(TextView)convertView.findViewById(R.id.tv_date);
  38. viewHolder.mTvCostMoney=(TextView)convertView.findViewById(R.id.tv_cost);
  39. convertView.setTag(viewHolder);
  40. }else{
  41. viewHolder=(ViewHolder) convertView.getTag();
  42. }
  43. CostBean bean=mList.get(position);
  44. viewHolder.mTvCostTitle.setText(bean.costTitle);
  45. viewHolder.mTvCostDate.setText(bean.costDate);
  46. viewHolder.mTvCostMoney.setText(bean.costMoney);
  47. return convertView;
  48. }
  49. private static class ViewHolder{
  50. public TextView mTvCostTitle;
  51. public TextView mTvCostDate;
  52. public TextView mTvCostMoney;
  53. }
  54. }

DatabaseHelper.java:

  1. package com.example.family;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import androidx.annotation.Nullable;
  8. public class DatabaseHelper extends SQLiteOpenHelper {
  9. public static final String COST_MONEY = "cost_money";
  10. public static final String COST_DATE = "cost_date";
  11. public static final String COST_TITLE = "cost_title";
  12. public static final String FAMILY = "family";
  13. public DatabaseHelper(@Nullable Context context) {
  14. super(context, FAMILY, null, 1);
  15. }
  16. @Override
  17. public void onCreate(SQLiteDatabase db) {
  18. db.execSQL("create table if not exists family("+
  19. "id integer primary key,"+
  20. "cost_title varchar,"+
  21. "cost_date varchar,"+
  22. "cost_money varchar)");
  23. }
  24. public void insertCost(CostBean costBean){
  25. SQLiteDatabase database=getWritableDatabase();
  26. ContentValues cv=new ContentValues();
  27. cv.put(COST_TITLE,costBean.costTitle);
  28. cv.put(COST_DATE,costBean.costDate);
  29. cv.put(COST_MONEY,costBean.costMoney);
  30. database.insert(FAMILY,null,cv);
  31. }
  32. public Cursor getAllCostData(){
  33. SQLiteDatabase database=getWritableDatabase();
  34. return database.query(FAMILY,null,null,null,null,null,COST_DATE+" ASC");
  35. }
  36. public void deleteAllData(){
  37. SQLiteDatabase database=getWritableDatabase();
  38. database.delete(FAMILY,null,null);
  39. }
  40. @Override
  41. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  42. }
  43. }

MainActivity.java:

  1. package com.example.family;
  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.database.Cursor;
  5. import android.os.Bundle;
  6. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  7. import com.google.android.material.snackbar.Snackbar;
  8. import androidx.appcompat.app.AlertDialog;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.appcompat.widget.Toolbar;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.widget.DatePicker;
  16. import android.widget.EditText;
  17. import android.widget.ListView;
  18. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. public class MainActivity extends AppCompatActivity {
  22. private List<CostBean> mCostBeanList;
  23. private DatabaseHelper mDatabaseHelper;
  24. private CostListAdapter mAdapter;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. Toolbar toolbar = findViewById(R.id.toolbar);
  30. setSupportActionBar(toolbar);
  31. mDatabaseHelper=new DatabaseHelper(this);
  32. mCostBeanList=new ArrayList<>();
  33. ListView costList=(ListView)findViewById(R.id.lv_main);
  34. initCostData();
  35. mAdapter =new CostListAdapter(this,mCostBeanList);
  36. costList.setAdapter(mAdapter);
  37. FloatingActionButton fab = findViewById(R.id.fab);
  38. fab.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View view) {
  41. AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
  42. LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
  43. View viewDialog=inflater.inflate(R.layout.new_cost_data,null);
  44. final EditText title=(EditText)viewDialog.findViewById(R.id.et_cost_title);
  45. final EditText money=(EditText)viewDialog.findViewById(R.id.et_cost_money);
  46. final DatePicker date=(DatePicker) viewDialog.findViewById(R.id.dp_cost_date);
  47. builder.setView(viewDialog);
  48. builder.setTitle("New Cost");
  49. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  50. @Override
  51. public void onClick(DialogInterface dialog, int which) {
  52. CostBean costBean=new CostBean();
  53. costBean.costTitle=title.getText().toString();
  54. costBean.costMoney=money.getText().toString();
  55. costBean.costDate=date.getYear() + "-" + (date.getMonth()+1) + "-" +
  56. date.getDayOfMonth();
  57. mDatabaseHelper.insertCost(costBean);
  58. mCostBeanList.add(costBean);
  59. mAdapter.notifyDataSetChanged();
  60. }
  61. });
  62. builder.setNegativeButton("Cancle",null);
  63. builder.create().show();
  64. }
  65. });
  66. }
  67. private void initCostData() {
  68. // mDatabaseHelper.deleteAllData();
  69. // for (int i=0;i<6;i++) {
  70. // CostBean costBean=new CostBean();
  71. // costBean.costTitle=i+"mock";
  72. // costBean.costDate="2020-2-4";
  73. // costBean.costMoney="20";
  74. // mDatabaseHelper.insertCost(costBean);
  75. // }
  76. Cursor cursor=mDatabaseHelper.getAllCostData();
  77. if(cursor!=null){
  78. while(cursor.moveToNext()){
  79. CostBean costBean=new CostBean();
  80. costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title"));
  81. costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date"));
  82. costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money"));
  83. mCostBeanList.add(costBean);
  84. }
  85. cursor.close();
  86. }
  87. }
  88. @Override
  89. public boolean onCreateOptionsMenu(Menu menu) {
  90. // Inflate the menu; this adds items to the action bar if it is present.
  91. getMenuInflater().inflate(R.menu.menu_main, menu);
  92. return true;
  93. }
  94. @Override
  95. public boolean onOptionsItemSelected(MenuItem item) {
  96. int id = item.getItemId();
  97. if (id == R.id.action_chart) {
  98. Intent intent=new Intent(MainActivity.this,ChartsActivity.class);
  99. intent.putExtra("cost_list", (Serializable) mCostBeanList);
  100. startActivity(intent);
  101. return true;
  102. }
  103. return super.onOptionsItemSelected(item);
  104. }
  105. }

content_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  9. tools:showIn="@layout/activity_main"
  10. tools:context=".MainActivity">
  11. <ListView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/lv_main"/>
  15. </RelativeLayout>

list_item.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="80dp">
  6. <TextView
  7. android:id="@+id/tv_title"
  8. android:layout_width="150dp"
  9. android:layout_height="80dp"
  10. android:layout_marginLeft="10dp"
  11. android:layout_alignParentLeft="true"
  12. android:gravity="center"
  13. android:singleLine="true"
  14. android:textSize="35sp"
  15. android:ellipsize="marquee"
  16. android:text="costTitle"/>
  17. <TextView
  18. android:id="@+id/tv_date"
  19. android:layout_width="100dp"
  20. android:layout_height="80dp"
  21. android:gravity="center"
  22. android:textSize="20sp"
  23. android:layout_marginLeft="15dp"
  24. android:layout_toRightOf="@+id/tv_title"
  25. android:text="costDate"/>
  26. <TextView
  27. android:id="@+id/tv_cost"
  28. android:layout_width="100dp"
  29. android:layout_height="80dp"
  30. android:gravity="center"
  31. android:layout_alignParentRight="true"
  32. android:layout_marginRight="20dp"
  33. android:textSize="30sp"
  34. android:text="30"/>
  35. </RelativeLayout>

new_cost_data.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:gravity="center"
  5. android:layout_height="match_parent">
  6. <EditText
  7. android:id="@+id/et_cost_title"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:layout_margin="4dp"
  11. android:hint="Cost Title"/>
  12. <EditText
  13. android:id="@+id/et_cost_money"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_margin="4dp"
  17. android:hint="Cost Money"/>
  18. <DatePicker
  19. android:id="@+id/dp_cost_date"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_margin="4dp"
  23. android:datePickerMode="spinner"
  24. android:calendarViewShown="false"/>
  25. </LinearLayout>

点击右下角“邮件”按钮实现增加。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/793139
推荐阅读
相关标签
  

闽ICP备14008679号