个人作业——家庭记账本
主要代码:
MainActivity.java
package com.example.jizhangben; import android.content.DialogInterface; import android.database.Cursor; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.support.v7.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.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<CostBean> mCostBeanList; private DatabaseHelper mDatabaseHelper; private CostListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mDatabaseHelper=new DatabaseHelper(this); mCostBeanList=new ArrayList<>(); ListView costList= (ListView) findViewById(R.id.lv_main); initCostData(); adapter = new CostListAdapter(this, mCostBeanList); costList.setAdapter(adapter); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); LayoutInflater inflate=LayoutInflater.from(MainActivity.this); View viewDialog=inflate.inflate(R.layout.new_cost_data,null); final EditText tltle= (EditText) viewDialog.findViewById(R.id.et_cost_title); final EditText money= (EditText) viewDialog.findViewById(R.id.et_cost_money); final DatePicker date= (DatePicker) viewDialog.findViewById(R.id.dp_cost_date); builder.setView(viewDialog); builder.setTitle("添加记录"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { CostBean costBean=new CostBean(); costBean.CostTitle=tltle.getText().toString(); costBean.CostMoney=money.getText().toString(); costBean.CostDate=date.getYear()+"-"+(date.getMonth()+1)+"-"+ date.getDayOfMonth(); mDatabaseHelper.insertCost(costBean); mCostBeanList.add(costBean); adapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消",null); builder.create().show(); } }); } private void initCostData() { // mDatabaseHelper.deleteAllData(); // for (int i=0;i<6;i++) { // CostBean costBean=new CostBean(); // costBean.CostTitle=i+"mock"; // costBean.CostDate="11-11"; // costBean.CostMoney="21"; // mDatabaseHelper.insertCost(costBean); // } Cursor cursor=mDatabaseHelper.getAllCostData(); 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")); mCostBeanList.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) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
CostBean.java
package com.example.jizhangben; public class CostBean { public String CostTitle; public String CostDate; public String CostMoney; }
CostListAdapter.java
package com.example.jizhangben; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; /** * Created by 惠普 on 2021/2/18. */ public class CostListAdapter extends BaseAdapter { private List<CostBean> mList; private Context mContext; private LayoutInflater mLayoutInflater; public CostListAdapter(Context context,List<CostBean> list){ mContext=context; mList=list; mLayoutInflater=LayoutInflater.from(context); } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView==null){ viewHolder=new ViewHolder(); convertView=mLayoutInflater.inflate(R.layout.list_item,null); viewHolder.mTvCostTitle=(TextView)convertView.findViewById(R.id.tv_title); viewHolder.mTvCostDate=(TextView)convertView.findViewById(R.id.tv_date); viewHolder.mTvCostMoney=(TextView)convertView.findViewById(R.id.tv_cost); convertView.setTag(viewHolder); }else{ viewHolder= (ViewHolder) convertView.getTag(); } CostBean bean=mList.get(position); viewHolder.mTvCostTitle.setText(bean.CostTitle); viewHolder.mTvCostDate.setText(bean.CostDate); viewHolder.mTvCostMoney.setText(bean.CostMoney); return convertView; } private static class ViewHolder{ public TextView mTvCostTitle; public TextView mTvCostDate; public TextView mTvCostMoney; }
DataBaseHelper.java
package com.example.jizhangben; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by 惠普 on 2021/2/20. */ public class DatabaseHelper extends SQLiteOpenHelper { public static final String COST_MONEY = "cost_money"; public static final String COST_DATE = "cost_date"; public static final String COST_TITLE = "cost_title"; public static final String IMOOC_COST = "Homework"; public DatabaseHelper(Context context) { super(context, "homework", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists Homework(" + "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(COST_TITLE,costBean.CostTitle); cv.put(COST_DATE,costBean.CostDate); cv.put(COST_MONEY,costBean.CostMoney); database.insert(IMOOC_COST,null,cv); } public Cursor getAllCostData(){ SQLiteDatabase database=getWritableDatabase(); return database.query(IMOOC_COST, null, null, null, null, null, COST_DATE+" ASC"); } public void deleteAllData(){ SQLiteDatabase database=getWritableDatabase(); database.delete(IMOOC_COST,null,null); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.jizhangben"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jizhangben.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.homework.MainActivity" tools:showIn="@layout/activity_main"> <ListView android:id="@+id/lv_main" android:layout_width="336dp" android:layout_height="463dp" tools:layout_editor_absoluteY="8dp" tools:layout_editor_absoluteX="8dp" /> </android.support.constraint.ConstraintLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/tv_title" android:layout_width="100dp" android:layout_height="80dp" android:gravity="center" android:textSize="20sp" android:layout_marginLeft="15dp" android:textColor="#836F91" android:layout_toRightOf="@+id/tv_date" android:text="costTitle"/> <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="80dp" android:layout_marginLeft="10dp" android:layout_alignParentLeft="true" android:gravity="center" android:singleLine="true" android:textSize="25sp" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:textColor="#836F91" android:text="costDate"/> <TextView android:id="@+id/tv_cost" android:layout_width="wrap_content" android:layout_height="80dp" android:gravity="center" android:layout_alignParentRight="true" android:textSize="25sp" android:textColor="#836F91" android:layout_marginRight="20dp" android:text="30"/> </RelativeLayout>
new_cost_data.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/et_cost_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="消费原因"/> <EditText android:id="@+id/et_cost_money" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="消费金额"/> <DatePicker android:id="@+id/dp_cost_date" android:layout_margin="4dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>